key.php 1.1 KB

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