key.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 = File::get($this->path);
  34. $config = str_replace("'key' => '',", "'key' => '{$key}',", $config, $count);
  35. File::put($this->path, $config);
  36. if ($count > 0)
  37. {
  38. echo "Configuration updated with secure key!";
  39. }
  40. else
  41. {
  42. echo "An application key already exists!";
  43. }
  44. echo PHP_EOL;
  45. }
  46. }