| @@ 7-72 (lines=66) @@ | ||
| 4 | ||
| 5 | use Illuminate\Console\Command; |
|
| 6 | ||
| 7 | class ApiTokenKey extends Command |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * The name and signature of the console command. |
|
| 11 | * |
|
| 12 | * @var string |
|
| 13 | */ |
|
| 14 | protected $signature = 'api:token-key {--show : Display the key instead of modifying files}'; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * The console command description. |
|
| 18 | * |
|
| 19 | * @var string |
|
| 20 | */ |
|
| 21 | protected $description = 'Set the api token key'; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Execute the console command. |
|
| 25 | * |
|
| 26 | * @return mixed |
|
| 27 | */ |
|
| 28 | public function handle() |
|
| 29 | { |
|
| 30 | $key = $this->generateRandomKey(); |
|
| 31 | ||
| 32 | if ($this->option('show')) { |
|
| 33 | return $this->comment($key); |
|
| 34 | } |
|
| 35 | ||
| 36 | $this->setKeyInEnvironmentFile($key); |
|
| 37 | ||
| 38 | $this->laravel['config']['support.api.token.key'] = $key; |
|
| 39 | ||
| 40 | $this->info("Api token key [$key] set successfully."); |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Generate a random key for the api token. |
|
| 45 | * |
|
| 46 | * @return string |
|
| 47 | */ |
|
| 48 | protected function generateRandomKey() |
|
| 49 | { |
|
| 50 | return str_random(32); |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Set the api token key in the environment file. |
|
| 55 | * |
|
| 56 | * @param string $key |
|
| 57 | */ |
|
| 58 | protected function setKeyInEnvironmentFile($key) |
|
| 59 | { |
|
| 60 | $content = file_get_contents($this->laravel->environmentFilePath()); |
|
| 61 | ||
| 62 | $text = 'API_TOKEN_KEY='.$key; |
|
| 63 | ||
| 64 | $content = preg_replace('#API_TOKEN_KEY=.*#', $text, $content, -1, $replaceCount); |
|
| 65 | ||
| 66 | if (0 === $replaceCount) { |
|
| 67 | $content .= $text.PHP_EOL; |
|
| 68 | } |
|
| 69 | ||
| 70 | file_put_contents($this->laravel->environmentFilePath(), $content); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| @@ 7-74 (lines=68) @@ | ||
| 4 | ||
| 5 | use Illuminate\Console\Command; |
|
| 6 | ||
| 7 | class Int2stringCharacters extends Command |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * The name and signature of the console command. |
|
| 11 | * |
|
| 12 | * @var string |
|
| 13 | */ |
|
| 14 | protected $signature = 'int2string:characters |
|
| 15 | {--show : Display the characters instead of modifying the config file} |
|
| 16 | {--c|characters=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ : Generate with custom characters}'; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * The console command description. |
|
| 20 | * |
|
| 21 | * @var string |
|
| 22 | */ |
|
| 23 | protected $description = 'Set characters for Helper::int2string.'; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Execute the console command. |
|
| 27 | * |
|
| 28 | * @return mixed |
|
| 29 | */ |
|
| 30 | public function handle() |
|
| 31 | { |
|
| 32 | $characters = $this->generateRandomCharacters($this->option('characters')); |
|
| 33 | ||
| 34 | if ($this->option('show')) { |
|
| 35 | return $this->comment($characters); |
|
| 36 | } |
|
| 37 | ||
| 38 | $this->setCharactersInEnvironmentFile($characters); |
|
| 39 | ||
| 40 | $this->laravel['config']['support.int2string'] = $characters; |
|
| 41 | ||
| 42 | $this->info("Characters [$characters] set successfully."); |
|
| 43 | } |
|
| 44 | ||
| 45 | /** |
|
| 46 | * Generate random characters. |
|
| 47 | * |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | protected function generateRandomCharacters($characters) |
|
| 51 | { |
|
| 52 | return str_shuffle(count_chars($characters, 3)); |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Set the characters in the environment file. |
|
| 57 | * |
|
| 58 | * @param string $characters |
|
| 59 | */ |
|
| 60 | protected function setCharactersInEnvironmentFile($characters) |
|
| 61 | { |
|
| 62 | $content = file_get_contents($this->laravel->environmentFilePath()); |
|
| 63 | ||
| 64 | $text = 'INT2STRING_CHARACTERS='.$characters; |
|
| 65 | ||
| 66 | $content = preg_replace('#INT2STRING_CHARACTERS=.*#', $text, $content, -1, $replaceCount); |
|
| 67 | ||
| 68 | if (0 === $replaceCount) { |
|
| 69 | $content .= $text.PHP_EOL; |
|
| 70 | } |
|
| 71 | ||
| 72 | file_put_contents($this->laravel->environmentFilePath(), $content); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||