Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save permadiwibisono/0ddbca8409cbc957e0ed38cf4b4c69de to your computer and use it in GitHub Desktop.
Save permadiwibisono/0ddbca8409cbc957e0ed38cf4b4c69de to your computer and use it in GitHub Desktop.

Revisions

  1. permadiwibisono revised this gist Aug 12, 2018. 1 changed file with 51 additions and 62 deletions.
    113 changes: 51 additions & 62 deletions KeyGenerateCommand.php
    Original file line number Diff line number Diff line change
    @@ -1,71 +1,60 @@
    <?php

    namespace App\Console\Commands;

    use Illuminate\Support\Str;
    use Illuminate\Console\Command;
    use Symfony\Component\Console\Input\InputOption;

    class KeyGenerateCommand extends Command
    {
    /**
    * The console command name.
    *
    * @var string
    */
    protected $name = 'key:generate';

    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = "Set the application key";

    /**
    * Execute the console command.
    *
    * @return void
    */
    public function fire()
    {
    $key = $this->getRandomKey();

    if ($this->option('show')) {
    return $this->line('<comment>'.$key.'</comment>');
    }

    $path = base_path('.env');

    if (file_exists($path)) {
    file_put_contents(
    $path,
    str_replace(env('APP_KEY'), $key, file_get_contents($path))
    );
    }

    $this->info("Application key [$key] set successfully.");
    /**
    * The console command name.
    *
    * @var string
    */
    protected $name = 'key:generate';
    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = "Set the application key";
    /**
    * Execute the console command.
    *
    * @return void
    */
    public function handle()
    {
    $key = $this->getRandomKey();
    if ($this->option('show')) {
    return $this->line('<comment>'.$key.'</comment>');
    }

    /**
    * Generate a random key for the application.
    *
    * @return string
    */
    protected function getRandomKey()
    {
    return Str::random(32);
    $path = base_path('.env');
    if (file_exists($path)) {
    file_put_contents(
    $path,
    str_replace('APP_KEY=' . env('APP_KEY'), 'APP_KEY=' . $key, file_get_contents($path))
    );
    }

    /**
    * Get the console command options.
    *
    * @return array
    */
    protected function getOptions()
    {
    return array(
    array('show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'),
    );
    }
    }
    $this->info("Application key [$key] set successfully.");
    }
    /**
    * Generate a random key for the application.
    *
    * @return string
    */
    protected function getRandomKey()
    {
    return Str::random(32);
    }
    /**
    * Get the console command options.
    *
    * @return array
    */
    protected function getOptions()
    {
    return array(
    array('show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'),
    );
    }
    }
  2. @krisanalfa krisanalfa revised this gist May 31, 2016. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions KeyGenerateCommand.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    <?php namespace App\Console\Commands;
    <?php

    namespace App\Console\Commands;

    use Illuminate\Support\Str;
    use Illuminate\Console\Command;
    @@ -29,22 +31,19 @@ public function fire()
    {
    $key = $this->getRandomKey();

    if ($this->option('show'))
    {
    if ($this->option('show')) {
    return $this->line('<comment>'.$key.'</comment>');
    }

    $path = base_path('.env');

    if (file_exists($path))
    {
    file_put_contents($path, str_replace(
    $this->laravel['config']['app.key'], $key, file_get_contents($path)
    ));
    if (file_exists($path)) {
    file_put_contents(
    $path,
    str_replace(env('APP_KEY'), $key, file_get_contents($path))
    );
    }

    $this->laravel['config']['app.key'] = $key;

    $this->info("Application key [$key] set successfully.");
    }

  3. @krisanalfa krisanalfa revised this gist May 20, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions KeyGenerateCommand.php
    Original file line number Diff line number Diff line change
    @@ -36,8 +36,6 @@ public function fire()

    $path = base_path('.env');

    dump($this->laravel['config']['app']);

    if (file_exists($path))
    {
    file_put_contents($path, str_replace(
  4. @krisanalfa krisanalfa created this gist May 20, 2015.
    74 changes: 74 additions & 0 deletions KeyGenerateCommand.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    <?php namespace App\Console\Commands;

    use Illuminate\Support\Str;
    use Illuminate\Console\Command;
    use Symfony\Component\Console\Input\InputOption;

    class KeyGenerateCommand extends Command
    {
    /**
    * The console command name.
    *
    * @var string
    */
    protected $name = 'key:generate';

    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = "Set the application key";

    /**
    * Execute the console command.
    *
    * @return void
    */
    public function fire()
    {
    $key = $this->getRandomKey();

    if ($this->option('show'))
    {
    return $this->line('<comment>'.$key.'</comment>');
    }

    $path = base_path('.env');

    dump($this->laravel['config']['app']);

    if (file_exists($path))
    {
    file_put_contents($path, str_replace(
    $this->laravel['config']['app.key'], $key, file_get_contents($path)
    ));
    }

    $this->laravel['config']['app.key'] = $key;

    $this->info("Application key [$key] set successfully.");
    }

    /**
    * Generate a random key for the application.
    *
    * @return string
    */
    protected function getRandomKey()
    {
    return Str::random(32);
    }

    /**
    * Get the console command options.
    *
    * @return array
    */
    protected function getOptions()
    {
    return array(
    array('show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'),
    );
    }
    }