Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Site / ModeCommand.php
index c791f51c901ab4150e758904176db4bcacbf4064..a362340df9d102294765116f8e1bc14e4600a749 100644 (file)
@@ -4,17 +4,14 @@
  * @file
  * Contains \Drupal\Console\Command\Site\ModeCommand.
  */
+
 namespace Drupal\Console\Command\Site;
 
 use Symfony\Component\Console\Input\InputArgument;
-use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
 use Symfony\Component\Yaml\Yaml;
 use Symfony\Component\Console\Command\Command;
-use Symfony\Component\Filesystem\Filesystem;
-use Novia713\Maginot\Maginot;
 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
 use Drupal\Console\Core\Style\DrupalStyle;
 use Drupal\Console\Core\Utils\ConfigurationManager;
@@ -50,7 +47,7 @@ class ModeCommand extends Command
      *
      * @param ConfigFactory        $configFactory
      * @param ConfigurationManager $configurationManager
-     * @param $appRoot
+     * @param $appRoot,
      * @param ChainQueue           $chainQueue,
      */
     public function __construct(
@@ -63,30 +60,6 @@ class ModeCommand extends Command
         $this->configurationManager = $configurationManager;
         $this->appRoot = $appRoot;
         $this->chainQueue = $chainQueue;
-
-        $this->local = null;
-
-        $this->services_file =
-            $this->appRoot.'/sites/default/services.yml';
-
-        $this->local_services_file =
-            $this->appRoot.'/sites/development.services.yml';
-
-        $this->settings_file =
-            $this->appRoot.'/sites/default/settings.php';
-
-        $this->local_settings_file =
-            $this->appRoot.'/sites/default/settings.local.php';
-
-        $this->local_settings_file_original =
-            $this->appRoot.'/sites/example.settings.local.php';
-
-        $this->fs = new Filesystem();
-        $this->maginot = new Maginot();
-        $this->yaml = new Yaml();
-
-        $this->environment = null;
-
         parent::__construct();
     }
 
@@ -99,12 +72,6 @@ class ModeCommand extends Command
                 'environment',
                 InputArgument::REQUIRED,
                 $this->trans('commands.site.mode.arguments.environment')
-            )
-            ->addOption(
-                'local',
-                null,
-                InputOption::VALUE_NONE,
-                $this->trans('commands.site.mode.options.local')
             );
     }
 
@@ -112,24 +79,22 @@ class ModeCommand extends Command
     {
         $io = new DrupalStyle($input, $output);
 
-        $this->environment = $input->getArgument('environment');
-        $this->local = $input->getOption('local');
+        $environment = $input->getArgument('environment');
 
-        $loadedConfigurations = [];
-        if (in_array($this->environment, ['dev', 'prod'])) {
-            $loadedConfigurations = $this->loadConfigurations($this->environment);
-        } else {
+        if (!in_array($environment, ['dev', 'prod'])) {
             $io->error($this->trans('commands.site.mode.messages.invalid-env'));
+            return 1;
         }
 
+        $loadedConfigurations = $this->loadConfigurations($environment);
+
         $configurationOverrideResult = $this->overrideConfigurations(
-            $loadedConfigurations['configurations'],
-            $io
+            $loadedConfigurations['configurations']
         );
 
         foreach ($configurationOverrideResult as $configName => $result) {
             $io->info(
-                $this->trans('commands.site.mode.messages.configuration').':',
+                $this->trans('commands.site.mode.messages.configuration') . ':',
                 false
             );
             $io->comment($configName);
@@ -143,7 +108,8 @@ class ModeCommand extends Command
             $io->table($tableHeader, $result);
         }
 
-        $servicesOverrideResult = $this->overrideServices(
+        $servicesOverrideResult = $this->processServicesFile(
+            $environment,
             $loadedConfigurations['services'],
             $io
         );
@@ -165,7 +131,7 @@ class ModeCommand extends Command
         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
     }
 
-    protected function overrideConfigurations($configurations, $io)
+    protected function overrideConfigurations($configurations)
     {
         $result = [];
         foreach ($configurations as $configName => $options) {
@@ -173,11 +139,11 @@ class ModeCommand extends Command
             foreach ($options as $key => $value) {
                 $original = $config->get($key);
                 if (is_bool($original)) {
-                    $original = $original ? 'true' : 'false';
+                    $original = $original? 'true' : 'false';
                 }
                 $updated = $value;
                 if (is_bool($updated)) {
-                    $updated = $updated ? 'true' : 'false';
+                    $updated = $updated? 'true' : 'false';
                 }
 
                 $result[$configName][] = [
@@ -190,103 +156,10 @@ class ModeCommand extends Command
             $config->save();
         }
 
-        $line_include_settings =
-            '<?php include __DIR__ . "/settings.local.php"; ?>';
-
-        if ($this->environment == 'dev') {
-
-            // copy sites/example.settings.local.php sites/default/settings.local.php
-            $this->fs->copy($this->local_settings_file_original, $this->local_settings_file, true);
-
-            // uncomment cache bins in settings.local.php
-            $this->maginot->unCommentLine(
-                '# $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
-                $this->local_settings_file
-            );
-
-            $this->maginot->unCommentLine(
-                '// $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
-                $this->local_settings_file
-            );
-
-            $this->maginot->unCommentLine(
-                '# $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
-                $this->local_settings_file
-            );
-
-            $this->maginot->unCommentLine(
-                '// $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
-                $this->local_settings_file
-            );
-
-            // include settings.local.php in settings.php
-            // -- check first line if it is already this
-            if ($this->maginot->getFirstLine($this->settings_file)!= $line_include_settings
-            ) {
-                chmod($this->settings_file, (int)0775);
-                $this->maginot->setFirstLine(
-                    $line_include_settings,
-                    $this->settings_file
-                );
-            }
-
-            $io->commentBlock(
-                sprintf(
-                    '%s',
-                    $this->trans('commands.site.mode.messages.cachebins')
-                )
-            );
-        }
-        if ($this->environment == 'prod') {
-            if (!$this->local) {
-
-                // comment local.settings.php in settings.php
-                if ($this->maginot->getFirstLine($this->settings_file)==$line_include_settings
-                ) {
-                    $this->maginot->deleteFirstLine(
-                        $this->settings_file
-                    );
-                }
-
-
-                try {
-                    $this->fs->remove(
-                        $this->local_settings_file
-                    );
-                    //@TODO: msg user "local.settings.php deleted"
-                } catch (IOExceptionInterface $e) {
-                    echo $e->getMessage();
-                }
-            } else {
-
-                // comment cache bins in local.settings.php,
-                // we still use local.settings.php for testing PROD
-                // settings in local
-
-                $this->maginot->CommentLine(
-                    ' $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
-                    $this->local_settings_file
-                );
-
-                $this->maginot->CommentLine(
-                    ' $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
-                    $this->local_settings_file
-                );
-            }
-        }
-
-        /**
-         * would be better if this were replaced by $config->save?
-         */
-        //@TODO: 0444 should be a better permission for settings.php
-        chmod($this->settings_file, (int)0644);
-        //@TODO: 0555 should be a better permission for sites/default
-        chmod($this->appRoot.'/sites/default/', (int)0755);
-
         return $result;
     }
 
-    protected function overrideServices($servicesSettings, DrupalStyle $io)
+    protected function processServicesFile($environment, $servicesSettings, DrupalStyle $io)
     {
         $directory = sprintf(
             '%s/%s',
@@ -294,10 +167,11 @@ class ModeCommand extends Command
             \Drupal::service('site.path')
         );
 
-        $settingsServicesFile = $directory.'/services.yml';
+        $settingsServicesFile = $directory . '/services.yml';
+
         if (!file_exists($settingsServicesFile)) {
             // Copying default services
-            $defaultServicesFile = $this->appRoot.'/sites/default/default.services.yml';
+            $defaultServicesFile = $this->appRoot . '/sites/default/default.services.yml';
             if (!copy($defaultServicesFile, $settingsServicesFile)) {
                 $io->error(
                     sprintf(
@@ -311,7 +185,9 @@ class ModeCommand extends Command
             }
         }
 
-        $services = $this->yaml->parse(file_get_contents($settingsServicesFile));
+        $yaml = new Yaml();
+
+        $services = $yaml->parse(file_get_contents($settingsServicesFile));
 
         $result = [];
         foreach ($servicesSettings as $service => $parameters) {
@@ -338,7 +214,7 @@ class ModeCommand extends Command
             }
         }
 
-        if (file_put_contents($settingsServicesFile, $this->yaml->dump($services))) {
+        if (file_put_contents($settingsServicesFile, $yaml->dump($services))) {
             $io->commentBlock(
                 sprintf(
                     $this->trans('commands.site.mode.messages.services-file-overwritten'),
@@ -358,7 +234,6 @@ class ModeCommand extends Command
         }
 
         sort($result);
-
         return $result;
     }
 
@@ -372,11 +247,11 @@ class ModeCommand extends Command
         if (!file_exists($configFile)) {
             $configFile = sprintf(
                 '%s/config/dist/site.mode.yml',
-                $this->configurationManager->getApplicationDirectory().DRUPAL_CONSOLE_CORE
+                $this->configurationManager->getApplicationDirectory() . DRUPAL_CONSOLE_CORE
             );
         }
 
-        $siteModeConfiguration = $this->yaml->parse(file_get_contents($configFile));
+        $siteModeConfiguration = Yaml::parse(file_get_contents($configFile));
         $configKeys = array_keys($siteModeConfiguration);
 
         $configurationSettings = [];