1: <?php namespace Laravel\CLI\Tasks;
2:
3: use Laravel\Str;
4: use Laravel\File;
5:
6: class Key extends Task {
7:
8: /**
9: * The path to the application config.
10: *
11: * @var string
12: */
13: protected $path;
14:
15: /**
16: * Create a new instance of the Key task.
17: *
18: * @return void
19: */
20: public function __construct()
21: {
22: $this->path = path('app').'config/application'.EXT;
23: }
24:
25: /**
26: * Generate a random key for the application.
27: *
28: * @param array $arguments
29: * @return void
30: */
31: public function generate($arguments = array())
32: {
33: // By default the Crypter class uses AES-256 encryption which uses
34: // a 32 byte input vector, so that is the length of string we will
35: // generate for the application token unless another length is
36: // specified through the CLI.
37: $key = Str::random(array_get($arguments, 0, 32));
38:
39: $config = File::get($this->path);
40:
41: $config = str_replace("'key' => '',", "'key' => '{$key}',", $config, $count);
42:
43: File::put($this->path, $config);
44:
45: if ($count > 0)
46: {
47: echo "Configuration updated with secure key!";
48: }
49: else
50: {
51: echo "An application key already exists!";
52: }
53:
54: echo PHP_EOL;
55: }
56:
57: }