Sven\FlexEnv\Env::set PHP Méthode

set() public méthode

Set the value of the given key to the value supplied.
public set ( string $key, string $value, boolean $linebreak = false ) : Env
$key string
$value string
$linebreak boolean
Résultat Env
    public function set($key, $value, $linebreak = false)
    {
        $oldValue = $this->get($key);
        $new = $linebreak ? "\n{$key}={$value}" : "{$key}={$value}";
        if (!is_null($oldValue)) {
            return $this->replaceInFile("{$key}={$oldValue}", $new);
        }
        file_put_contents($this->getPath(), "\n{$new}", FILE_APPEND);
        return $this;
    }

Usage Example

Exemple #1
0
 /**
  * Execute the console command.
  *
  * @return void
  */
 public function handle()
 {
     $env = new Env(base_path('.env'));
     $key = strtoupper($this->argument('key'));
     $value = (string) $this->argument('value');
     $linebreak = (bool) $this->option('line-break');
     if (preg_match('/\\s/', $value)) {
         $value = "\"{$value}\"";
     }
     $result = $env->set($key, $value, $linebreak)->get($key);
     if ($result !== $value) {
         $env->rollback();
         return $this->error('Could not set the value in your .env file, reverting...');
     }
     return $this->comment("Successfully set [{$key}] to [{$value}] in your .env file.");
 }