Platformsh\Cli\CliConfig::writeUserConfig PHP Méthode

writeUserConfig() public méthode

Update user configuration.
public writeUserConfig ( array $config )
$config array
    public function writeUserConfig(array $config)
    {
        $dir = $this->getUserConfigDir();
        if (!is_dir($dir) && !mkdir($dir, 0700, true)) {
            trigger_error('Failed to create user config directory: ' . $dir, E_USER_WARNING);
        }
        $existingConfig = $this->getUserConfig();
        $config = array_replace_recursive($existingConfig, $config);
        $configFile = $dir . '/config.yaml';
        $new = !file_exists($configFile);
        if (file_put_contents($configFile, Yaml::dump($config, 10)) === false) {
            trigger_error('Failed to write user config to: ' . $configFile, E_USER_WARNING);
        }
        // If the config file was newly created, then chmod to be r/w only by
        // the user.
        if ($new) {
            chmod($configFile, 0600);
        }
        $this->userConfig = $config;
    }

Usage Example

 /**
  * Check for updates.
  *
  * @param bool $reset
  */
 protected function checkUpdates($reset = false)
 {
     if (!$reset && self::$checkedUpdates) {
         return;
     }
     self::$checkedUpdates = true;
     // Check that this instance of the CLI was installed as a Phar.
     if (!extension_loaded('Phar') || !\Phar::running(false)) {
         return;
     }
     $timestamp = time();
     if (!self::$config->get('updates.check')) {
         return;
     } elseif (!$reset && self::$config->get('updates.last_checked') > $timestamp - self::$config->get('updates.check_interval')) {
         return;
     }
     self::$config->writeUserConfig(['updates' => ['check' => true, 'last_checked' => $timestamp]]);
     // Ensure classes are auto-loaded if they may be needed after the
     // update.
     /** @var \Platformsh\Cli\Helper\ShellHelper $shellHelper */
     $shellHelper = $this->getHelper('shell');
     /** @var \Platformsh\Cli\Helper\QuestionHelper $questionHelper */
     $questionHelper = $this->getHelper('question');
     $currentVersion = self::$config->get('application.version');
     $cliUpdater = new SelfUpdater($this->input, $this->output, self::$config, $questionHelper);
     $cliUpdater->setAllowMajor(true);
     $cliUpdater->setTimeout(10);
     try {
         $newVersion = $cliUpdater->update(null, $currentVersion);
     } catch (\RuntimeException $e) {
         if (strpos($e->getMessage(), 'Failed to download') !== false) {
             $this->stdErr->writeln('<error>' . $e->getMessage() . '</error>');
             $newVersion = false;
         } else {
             throw $e;
         }
     }
     // If the update was successful, and it's not a major version change,
     // then prompt the user to continue after updating.
     if ($newVersion !== false) {
         $exitCode = 0;
         list($currentMajorVersion, ) = explode('.', $currentVersion, 2);
         list($newMajorVersion, ) = explode('.', $newVersion, 2);
         if ($newMajorVersion === $currentMajorVersion && isset($GLOBALS['argv'])) {
             $originalCommand = implode(' ', array_map('escapeshellarg', $GLOBALS['argv']));
             $questionText = "\n" . 'Original command: <info>' . $originalCommand . '</info>' . "\n\n" . 'Continue?';
             if ($questionHelper->confirm($questionText)) {
                 $this->stdErr->writeln('');
                 $exitCode = $shellHelper->executeSimple($originalCommand);
             }
         }
         exit($exitCode);
     }
     $this->stdErr->writeln('');
 }