Esempio n. 1
0
 public static function ListSettings($arguments)
 {
     LogCLI::Message('Listing available settings: ', 0);
     $configScopesNginx = ApplicationsDB::LoadApplication('nginx');
     //$settings = ApplicationsDB::GetSettingsList('nginx', 'server');
     $settingsNginx = ApplicationsDB::GetSettingsList('nginx');
     //        $settings = ArrayTools::GetMultiDimentionalElementsWithChildren(&$settingsNginx);
     $settings = ArrayTools::GetMultiDimentionalElements(&$settingsNginx, true);
     foreach ($settings as $setting) {
         //var_dump($setting);
         LogCLI::MessageResult(LogCLI::BLUE . $setting, 0, LogCLI::INFO);
     }
     LogCLI::Result(LogCLI::OK);
 }
Esempio n. 2
0
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $application = $input->getArgument('application');
     //        $output->writeln('Hello World!');
     LogCLI::Message('Listing available settings: ', 0);
     $configScopesNginx = ApplicationsDB::LoadApplication($application);
     //        $settingsNginx = ApplicationsDB::GetSettingsList('nginx', 'server');
     $settingsNginx = ApplicationsDB::GetSettingsList($application);
     $settings = ArrayTools::GetMultiDimentionalElements($settingsNginx, true, true);
     //        $settings = ArrayTools::GetMultiDimentionalElementsWithChildren(&$settingsNginx);
     foreach ($settings as $setting) {
         LogCLI::MessageResult(LogCLI::BLUE . $setting, 0, LogCLI::INFO);
     }
     LogCLI::Result(LogCLI::OK);
 }
Esempio n. 3
0
 public function AddFromFile($file, $scopeUser = false)
 {
     LogCLI::Message('Adding template file: ' . LogCLI::BLUE . $file . LogCLI::RESET, 4);
     try {
         $data = trim(file_get_contents($file));
         $scopeData = FileOperation::pathinfo_utf($file);
         $scopeName = $scopeUser === false ? $scopeData['filename'] : $scopeUser;
         $this->DB = ArrayTools::MergeArrays($this->DB, array($scopeName => $data));
         LogCLI::MessageResult('Templates DB updated with ' . LogCLI::BLUE . $scopeName . LogCLI::RESET . ' definition', 5, LogCLI::INFO);
         LogCLI::Result(LogCLI::OK, 4);
     } catch (Exception $e) {
         LogCLI::Result(LogCLI::FAIL);
         LogCLI::Fail($e->getMessage());
     }
 }
Esempio n. 4
0
 private function parseWithConfig($configuration, $result)
 {
     $output = null;
     foreach ($this->options as $option) {
         /*
          * if there are multiple paths for one setting
          */
         if (is_array($option->path)) {
             foreach ($option->path as $config => $path) {
                 //($setting = ArrayTools::accessArrayElementByPath($configuration, $path)) !== null ?: $setting = $option->default[$config];
                 $setting = ArrayTools::accessArrayElementByPath($configuration, $path);
                 if ($setting === null) {
                     if ($option->default[$config] !== null) {
                         LogCLI::MessageResult('Setting ' . $config . ' not set, defaulting to: ' . $option->default[$config], 7, LogCLI::OK);
                     }
                     $setting = $option->default[$config];
                 }
                 $values[$config] = $setting;
             }
             $this->_dispatchAction($option, $values, $result);
         } else {
             //($setting = ArrayTools::accessArrayElementByPath($configuration, $option->path)) !== null ?: $setting = $option->default;
             $setting = ArrayTools::accessArrayElementByPath($configuration, $option->path);
             if ($setting === null) {
                 $option->setDefaults();
                 if ($option->default !== null) {
                     LogCLI::MessageResult('Setting ' . $option->name . ' not set, defaulting to: ' . $option->default, 7, LogCLI::OK);
                 }
                 $setting = $option->default;
             }
             //var_dump($setting);
             /*
              *  If we got an array, how do we divide it?
              *  By default by ' ' (space), but sometimes we want eg. PHP_EOL, or comma.
              */
             $value = StringTools::makeList($setting, $option->divideBy);
             $this->_dispatchAction($option, $value, $result);
         }
     }
     //foreach(preg_split("/(\r?\n)/", $this->template) as $line)
     foreach (explode(PHP_EOL, $this->template) as $line) {
         //$parsedline = ParseTools::sprintfn($line, $result->options);
         $parsedline = ParseTools::parseStringWithReplacementList($line, $result->options);
         // if all we got is whitespace, don't add it
         if (strlen(rtrim($parsedline)) < 1) {
             continue;
         }
         // if we got a multiline responds we have to indent it
         // TODO: maybe explode "\n" or PHP_EOL would be better?
         //            if(count($lines = preg_split("/(\r?\n)/", $parsedline)) > 1)
         if (count($lines = explode(PHP_EOL, $parsedline)) > 1) {
             $indentedlines = array_shift($lines) . PHP_EOL;
             foreach ($lines as &$multiline) {
                 $indentedlines .= StringTools::indentLinesToMatchOther(trim($line), $line, $multiline) . PHP_EOL;
             }
             $parsedline = rtrim($indentedlines);
         }
         $output .= $parsedline . PHP_EOL;
     }
     return $output;
 }
Esempio n. 5
0
 /**
  * @param string $file          path to a YAML file
  * @param string|bool $path     start the merge at designated settings path
  * @param bool $applyDefaults
  * @param bool $mergeDefaults
  * @param bool $createNewIfNonexistant
  * @param bool $addFilename
  * @return void
  */
 public function mergeFromYAML($file, $path = false, $applyDefaults = false, $mergeDefaults = true, $createNewIfNonexistant = false, $addFilename = false)
 {
     LogCLI::Message('Loading file: ' . LogCLI::BLUE . $file . LogCLI::RESET, 1);
     if (file_exists($file)) {
         LogCLI::Result(LogCLI::OK);
         LogCLI::Message('Parsing YAML file: ' . LogCLI::BLUE . $file . LogCLI::RESET, 1);
         try {
             //$config = YAML::load($file);
             $config = Yaml::parse($file);
             //                 if the file is empty create an empty array:
             //                    $config = array();
             if (!empty($config)) {
                 if ($path === false) {
                     $this->DB = ArrayTools::MergeArrays($this->DB, $config);
                     if ($mergeDefaults === true) {
                         $this->mergeDefinitionsDB($config, $this->defaultsDefinitions, $applyDefaults, 'defaults');
                     }
                 } else {
                     if ($addFilename === true) {
                         $fileInfo = FileOperation::pathinfo_utf($file);
                         $config['filename'] = $fileInfo['filename'];
                         //$config['filename'] = $file;
                     }
                     if (!isset($config['template']) && (!isset($config['disabled']) || $config['disabled'] == false)) {
                         $iteration = $this->mergeOneIterativeByPath($path, $config);
                         //var_dump($this->defaultsDefinitions);
                         /**
                          * applying parents as defaults
                          */
                         if (isset($config['parent'])) {
                             foreach ((array) $config['parent'] as $parentName) {
                                 if (is_array($parentFiles = FileOperation::findFile($parentName, Paths::$db))) {
                                     if (count($parentFiles) > 1) {
                                         LogCLI::MessageResult('You have to be more specific in the way you specify the parent, found more than one file under this name: ' . $parentName, 0, LogCLI::INFO);
                                     } else {
                                         $parentFile =& current($parentFiles);
                                         LogCLI::Message('Parent definition found: ' . LogCLI::BLUE . $parentFile . LogCLI::RESET . ', parsing and merging.', 2);
                                         if (!isset($this->parentDefinitions[$parentFile])) {
                                             $parentConfig = array('parent' => Yaml::parse($parentFile));
                                             $this->parentDefinitions[$parentFile] = array();
                                             $this->mergeDefinitionsDB($parentConfig, $this->parentDefinitions[$parentFile], false, 'parent');
                                         }
                                         $this->applyDefaults($this->DB, $this->parentDefinitions[$parentFile], $path . '/' . $iteration, 'parent', true, false);
                                         LogCLI::Result(LogCLI::OK);
                                     }
                                     //var_dump($this->parentDefinitions);
                                 }
                             }
                         }
                         if ($applyDefaults === true) {
                             $this->applyDefaults($this->DB, $this->defaultsDefinitions, $path . '/' . $iteration, 'defaults', false, false);
                         }
                     }
                 }
                 LogCLI::MessageResult('Settings DB populated with new data!', 5, LogCLI::INFO);
             } else {
                 LogCLI::MessageResult('File empty, ignoring', 5, LogCLI::INFO);
             }
             LogCLI::Result(LogCLI::OK);
         } catch (\Exception $e) {
             LogCLI::Result(LogCLI::FAIL);
             LogCLI::Fail($e->getMessage());
         }
     } else {
         LogCLI::Result(LogCLI::FAIL);
         LogCLI::Fail("No such file: {$file}");
         if ($createNewIfNonexistant === true) {
             LogCLI::Message('Creating a new empty file.', 0);
             try {
                 //fclose(fopen($file, 'x'));
                 FileOperation::CreateEmptyFile($file);
                 $this->mergeFromYAML($file, $path, $applyDefaults, $mergeDefaults, false);
                 LogCLI::Result(LogCLI::OK);
             } catch (\Exception $e) {
                 LogCLI::Result(LogCLI::FAIL);
                 LogCLI::Fail($e->getMessage());
             }
         }
     }
 }
Esempio n. 6
0
 /**
  * similar to makeTree, but also parses the tree and puts the actual elements in place
  *
  * @param string $scope
  * @param bool $parseResult
  * @param int $depth
  * @param bool $parentIterative
  * @param string $parent
  * @return array
  */
 public function parseTree($scope, $parseResult = false, $depth = 0, $parentIterative = false, $parent = '')
 {
     $parentDisplay = null;
     ++$depth;
     $return = array();
     if (!empty($parent)) {
         $fullScopePath = $parent . '/' . $scope;
         $parentDisplay = LogCLI::GREEN . $parent . LogCLI::RESET . ' => ';
     } else {
         $fullScopePath = $scope;
     }
     if ($parseResult === false) {
         $pregSubject =& $this->templates[$scope];
     } elseif (isset($this->results[$scope])) {
         $pregSubject =& $this->results[$scope];
     } else {
         return $return;
     }
     preg_match_all('/<<(?<name>\\w+)>>/', $pregSubject, $matches);
     preg_match_all('/<!<(?<name>\\w+)>!>/', $pregSubject, $matchesIterative);
     if (!empty($matches['name'])) {
         foreach ($matches['name'] as $match) {
             if (!$this->patterns[$match]) {
                 $this->patterns[$match] = '<<' . $match . '>>';
             }
             LogCLI::Message('(' . $depth . ') Non-iterative match: ' . $parentDisplay . LogCLI::BLUE . $scope . LogCLI::RESET . " => " . LogCLI::YELLOW . $match . LogCLI::RESET, 2);
             $children = $this->parseTree($match, false, $depth, $parentIterative, $scope);
             LogCLI::Result(LogCLI::INFO);
             if ($parentIterative === true) {
                 $path = $scope . '/' . $match;
                 if (!$this->parsers[$match]->loadConfiguration($this->config, $path)) {
                     LogCLI::MessageResult('No configuration data for: ' . LogCLI::BLUE . $scope . LogCLI::RESET, LogCLI::INFO);
                 }
                 LogCLI::Message("Ordering parsing of: " . LogCLI::BLUE . $path . LogCLI::RESET . " at depth = {$depth}", 3);
                 LogCLI::MessageResult('Parent scope is iterative: ' . LogCLI::BLUE . $scope . LogCLI::RESET, LogCLI::INFO);
                 $this->results[$path] = $this->parsers[$match]->getParsed();
                 LogCLI::Result(LogCLI::INFO);
             } elseif (!isset($this->results[$match])) {
                 $this->parsers[$match]->loadConfiguration($this->config, $match);
                 LogCLI::Message("Ordering parsing of: " . LogCLI::BLUE . $match . LogCLI::RESET . " at depth = {$depth}", 3);
                 LogCLI::MessageResult('Parent scope is not iterative: ' . LogCLI::BLUE . $scope . LogCLI::RESET, LogCLI::INFO);
                 $this->results[$match] = $this->parsers[$match]->getParsed();
                 foreach ($children as $child) {
                     //LogCLI::MessageResult("Inserting: $child to ".LogCLI::BLUE.$match.LogCLI::RESET." at depth = $depth", 5);
                     $this->results[$match] = $this->insertScope($child, $match, $this->patterns[$child]);
                 }
                 LogCLI::Result(LogCLI::INFO);
             }
         }
         $return = $matches['name'];
     }
     if (!empty($matchesIterative['name'])) {
         foreach ($matchesIterative['name'] as $match) {
             $path = $fullScopePath . '/' . $match;
             $this->patterns[$match] = '<!<' . $match . '>!>';
             $this->results[$match] = null;
             $this->results[$path] = null;
             /*
              * recursive iterative matching
              */
             //$currentConfig = $this->parsers[$match]->loadConfiguration(&$this->config, $path, $match);
             $currentConfig =& ArrayTools::accessArrayElementByPath($this->config, $path);
             //$parentConfig = ArrayTools::accessArrayElementByPath(&$this->config, $fullScopePath);
             // translation:
             if (!ArrayTools::isIterativeScope($currentConfig)) {
                 LogCLI::MessageResult('Non-iterative format, translating path: ' . LogCLI::BLUE . $path . LogCLI::RESET, LogCLI::INFO);
                 //ArrayTools::replaceArrayElementByPath(&$this->config, $path, ArrayTools::translateToIterativeScope($match, $currentConfig));
                 $currentConfig = ArrayTools::translateToIterativeScope($match, $currentConfig);
             }
             foreach (array_keys($currentConfig) as $id) {
                 LogCLI::Message('(' . $depth . ') ' . $parentDisplay . LogCLI::BLUE . $scope . LogCLI::RESET . " => " . LogCLI::YELLOW . $match . LogCLI::RESET . " => [" . LogCLI::GREEN_LIGHT . $id . LogCLI::RESET . "]", 2);
                 LogCLI::MessageResult("Ordering parsing of: " . LogCLI::BLUE . "{$match}" . LogCLI::RESET . " at depth = {$depth}", 3);
                 $iterativePath = "{$fullScopePath}/{$match}/{$id}";
                 $this->parsers[$match]->loadConfiguration($this->config, $iterativePath);
                 // at this moment it's still the same configuration, just cut out
                 $this->results[$iterativePath] = $this->parsers[$match]->getParsed();
                 /**
                  * let's parse all the children
                  */
                 LogCLI::Message('(' . $depth . ') ' . $parentDisplay . LogCLI::BLUE . $scope . LogCLI::RESET . " => " . LogCLI::YELLOW . $match . LogCLI::RESET . " => " . LogCLI::RED . '[iterative]' . LogCLI::RESET, 2);
                 $children = $this->parseTree($iterativePath, true, $depth, true, '');
                 //LogCLI::MessageResult("Child's path: ".LogCLI::YELLOW.$iterativePath.LogCLI::RESET, 7);
                 foreach ($children as $child) {
                     $this->results[$iterativePath] = $this->insertScope("{$iterativePath}/{$child}", $iterativePath, $this->patterns[$child], $this->results[$iterativePath]);
                 }
                 LogCLI::MessageResult("Adding up the iterative scope values: " . LogCLI::YELLOW . $iterativePath . LogCLI::RESET, 5);
                 $this->results[$path] .= trim($this->results[$iterativePath]) . PHP_EOL;
                 LogCLI::Result(LogCLI::INFO);
                 // end children parse
                 LogCLI::Result(LogCLI::INFO);
                 // end iteration
             }
             $this->results[$match] = rtrim($this->results[$path]);
         }
         $return = array_merge($return, $matchesIterative['name']);
     }
     if ($depth == 1) {
         $this->parsers[$scope]->loadConfiguration($this->config, $scope);
         $this->results[$scope] = $this->parsers[$scope]->getParsed();
         $all_matches = array_merge_recursive($matches, $matchesIterative);
         if (!empty($all_matches['name'])) {
             foreach ($all_matches['name'] as $match) {
                 //LogCLI::MessageResult("Inserting: $match to ".LogCLI::BLUE.$scope.LogCLI::RESET, 5);
                 $this->results[$scope] = $this->insertScope($match, $scope);
             }
         }
     }
     return $return;
 }
Esempio n. 7
0
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $name = $input->getArgument('name');
     $settingPath = $input->getArgument('path');
     $values = $input->getArgument('values');
     //        $multiline = $input->getOption('multiline');
     if (empty($values)) {
         // enable multiline input
         $dialog = $this->getHelperSet()->get('dialog');
         if (!$dialog->askConfirmation($output, '<question>Would you like to provide multiline input? Answering no, quits the command. (y/n) </question> ', false)) {
             return;
         } else {
             $input = array();
             $output->writeln('Type <info>EOT</info> in a new line when you finished inputting all the options.');
             while (1) {
                 $input[] = $dialog->ask($output, null, 'foo');
                 if (end($input) == 'EOT') {
                     array_pop($input);
                     break;
                 }
             }
             $values[] = implode(PHP_EOL, $input);
             //                $values = $input;
             //var_dump($values);
         }
     }
     ApplicationsDB::LoadAll();
     foreach (StringTools::TypeList($name) as $argument) {
         if ($argument['exclamation'] !== false) {
             // TODO: actually handle the exclamators
             LogCLI::MessageResult('Exclamation: ' . $argument['exclamation'], 4, LogCLI::INFO);
         } else {
             //siteYML
             $file = Paths::getFullPath($argument['text']);
             if ($file !== false) {
                 $application = 'nginx';
                 $basicScope = 'server';
                 $settings = ApplicationsDB::GetSettingsList($application, $basicScope);
                 $value = ArrayTools::dearraizeIfNotRequired($values);
                 //$chain = StringTools::Delimit($chain, '.');
                 //$settingPath = implode('/', $chain);
                 LogCLI::MessageResult('Path of the setting: ' . $settingPath, 4, LogCLI::INFO);
                 if ($path = SettingsDB::findPathForSetting($settings, $settingPath, $basicScope)) {
                     $settingsDB = new ConfigScopes\SettingsDB();
                     // load the original file first
                     $settingsDB->mergeFromYAML($file);
                     $currentSetting = $settingsDB->returnOneByPath($settingPath);
                     /**
                      * if there is a difference
                      * TODO: probably dearraize not required here
                      */
                     if (ArrayTools::dearraizeIfNotRequired($currentSetting) != $value) {
                         $formatter = $this->getHelperSet()->get('xformatter');
                         $displayValues = array();
                         foreach ($values as $valueLine) {
                             $displayValues = array_merge($displayValues, StringTools::multilineStringToArray($valueLine));
                         }
                         $displayCurrentSetting = array();
                         foreach ((array) $currentSetting as $valueLine) {
                             $displayCurrentSetting = array_merge($displayCurrentSetting, StringTools::multilineStringToArray($valueLine));
                         }
                         $toFormat = array(array('messages' => $displayCurrentSetting, 'style' => 'error'), array('messages' => array('> >'), 'style' => 'fg=yellow;bg=black;other=blink;other=bold', 'large' => false), array('messages' => $displayValues, 'style' => 'fg=black;bg=yellow;other=bold'));
                         //array_merge(array('With the following data:'), (array) $values)
                         $output->writeln($formatter->formatMultipleBlocks($toFormat, ' ', true));
                         $dialog = $this->getHelperSet()->get('dialog');
                         if (!$dialog->askConfirmation($output, 'Are you sure that you want to make this change? (type "y" to confirm) ', false)) {
                             return;
                         }
                         // make the tree
                         $setting = Tree::addToTreeAndSet(explode('/', $path), $value);
                         //var_dump($setting);
                         // add/replace the setting
                         $settingsDB->mergeFromArray($setting);
                         // save the file with the new setting
                         $settingsDB->returnYAML($file);
                     } else {
                         /**
                          * nothing to do, it's all the same
                          */
                         $output->writeln('<fg=yellow;other=bold>No need to change, the values are already identical!</fg=yellow;other=bold>');
                     }
                 } else {
                     // TODO
                 }
             } else {
                 LogCLI::Fail('Sorry, no site by name: ' . $argument['text']);
             }
         }
     }
 }