key.php 1.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php namespace Laravel\CLI\Tasks;

use Laravel\Str;
use Laravel\File;

class Key extends Task {

	/**
	 * The path to the application config.
	 *
	 * @var string
	 */
	protected $path;

	/**
	 * Create a new instance of the Key task.
	 *
	 * @return void
	 */
	public function __construct()
	{
Taylor Otwell committed
22
		$this->path = path('app').'config/application'.EXT;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	}

	/**
	 * Generate a random key for the application.
	 *
	 * @param  array  $arguments
	 * @return void
	 */
	public function generate($arguments = array())
	{
		// By default the Crypter class uses AES-256 encryption which uses
		// a 32 byte input vector, so that is the length of string we will
		// generate for the application token unless another length is
		// specified through the CLI.
		$key = Str::random(array_get($arguments, 0, 32));

Taylor Otwell committed
39 40 41
		$config = File::get($this->path);

		$config = str_replace("'key' => '',", "'key' => '{$key}',", $config, $count);
42 43 44 45 46 47 48 49 50 51 52

		File::put($this->path, $config);

		if ($count > 0)
		{
			echo "Configuration updated with secure key!";
		}
		else
		{
			echo "An application key already exists!";
		}
Taylor Otwell committed
53 54

		echo PHP_EOL;
55 56 57
	}

}