protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $names = StringTools::Delimit($input->getArgument('name'), ','); $user = $input->getArgument('user'); $group = $input->getArgument('group'); $parent = $input->getOption('parent'); // TODO support if (!empty($parent)) { var_dump($parent); } foreach ($names as $website) { // adding website //$website = $name['text']; LogCLI::Message('Adding website: ' . $website, 0); LogCLI::MessageResult('Group and user: '******'/' . $user, 2, LogCLI::INFO); $path = Paths::$db . Paths::$separator . $group . Paths::$separator . $user . Paths::$separator; if (file_exists($path)) { if (!file_exists($path . $website . '.yml') && Paths::getFullPath($website) === false) { FileOperation::CreateEmptyFile($path . $website . '.yml'); LogCLI::Result(LogCLI::OK); } else { LogCLI::Fail('Website ' . $website . ', under ' . $group . '/' . $user . ' already exists!'); LogCLI::Result(LogCLI::FAIL); } } else { LogCLI::Fail('Group and/or user ' . $group . '/' . $user . ' does not exist!'); LogCLI::Result(LogCLI::FAIL); } } }
/** * version of sprintf for cases where named arguments are desired (python syntax) * * with sprintf: sprintf('second: %2$s ; first: %1$s', '1st', '2nd'); * * with sprintfn: sprintfn('second: %(second)s ; first: %(first)s', array( * 'first' => '1st', * 'second'=> '2nd' * )); * * @param string $format sprintf format string, with any number of named arguments * @param array $args array of [ 'arg_name' => 'arg value', ... ] replacements to be made * @return string|false result of sprintf call, or bool false on error */ public static function sprintfn($format, array $args = array()) { //var_dump($format); if (is_string($format)) { LogCLI::Message('Parsing: ' . LogCLI::GREEN_LIGHT . $format . LogCLI::RESET, 5); // map of argument names to their corresponding sprintf numeric argument value $arg_nums = array_slice(array_flip(array_keys(array(0 => 0) + $args)), 1); // find the next named argument. each search starts at the end of the previous replacement. for ($pos = 0; preg_match('/(?<=%)\\(([a-zA-Z_]\\w*)\\)/', $format, $match, PREG_OFFSET_CAPTURE, $pos);) { $arg_pos = $match[0][1]; $arg_len = strlen($match[0][0]); $arg_key = $match[1][0]; LogCLI::Message('Parsing argument: ' . LogCLI::YELLOW . $arg_key . LogCLI::RESET, 6); // programmer did not supply a value for the named argument found in the format string if (!array_key_exists($arg_key, $arg_nums) || $args[$arg_key] === false) { //var_dump($arg_nums); //$arg_nums[$arg_key] = false; ////array_push($arg_nums, $arg_key); //var_dump($arg_nums); LogCLI::MessageResult('Not set: ' . LogCLI::YELLOW . $arg_key . LogCLI::RESET . ', skipping', 4, LogCLI::INFO); //user_error("sprintfn(): Missing argument '${arg_key}'", E_USER_NOTICE); //return false; $format = substr_replace($format, $replace = '', $arg_pos - 1, $arg_len + 2); } else { //$posLeft = strlen($format)-strpos(strrev($format), '[[', strlen($format)-$arg_pos); $posLeft = strlen($format) - strpos(strrev($format), '[[', strlen($format) - $arg_pos); $posRight = strpos($format, ']]', $arg_pos); LogCLI::MessageResult('Original left position: ' . LogCLI::BLUE . $arg_pos . LogCLI::RESET, 6, LogCLI::INFO); LogCLI::MessageResult('Found left position: ' . LogCLI::YELLOW . $posLeft . LogCLI::RESET, 6, LogCLI::INFO); LogCLI::MessageResult('Found right position: ' . LogCLI::YELLOW . $posRight . LogCLI::RESET, 6, LogCLI::INFO); $format = substr_replace($format, '', $posRight, 2); $format = substr_replace($format, '', $posLeft - 2, 2); $arg_pos = $posLeft - 2 + ($arg_pos - $posLeft); //$format = str_replace(']]', 'a', $format); // replace the named argument with the corresponding numeric one $format = substr_replace($format, $replace = $arg_nums[$arg_key] . '$', $arg_pos, $arg_len); } $pos = $arg_pos + strlen($replace); // skip to end of replacement for next iteration LogCLI::Result(LogCLI::INFO); } $format = preg_replace('#\\[\\[.*?\\]\\]#', '', $format); $return = vsprintf($format, array_values($args)); if ($return == $format) { //LogCLI::MessageResult('No dynamic content.', 6, LogCLI::INFO); LogCLI::Result(LogCLI::OK); } else { LogCLI::MessageResult('Parsed content: ' . LogCLI::BLUE . $return . LogCLI::RESET, 6, LogCLI::INFO); LogCLI::Result(LogCLI::OK); } return $return; } else { LogCLI::Fail('Input template is not a string'); } }
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()); } }
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { //var_dump($input->getArgument('name')); $names = StringTools::Delimit($input->getArgument('name'), ','); $group = $input->getArgument('group'); foreach ($names as $username) { // adding user //LogCLI::MessageResult('Exclamation: '.$name['exclamation'], 2, LogCLI::INFO); //$username = $name['text']; LogCLI::Message('Adding user: '******'Creating directory: ' . $structure, 2, LogCLI::INFO); if (@mkdir($structure, 0755, true)) { LogCLI::Result(LogCLI::OK); } else { LogCLI::Fail('User ' . $username . ' already exists!'); LogCLI::Result(LogCLI::FAIL); } } }
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $names = StringTools::TypeList($input->getArgument('name'), '@', ','); $userorgroup = $input->getArgument('userorgroup'); // $group = $input->getArgument('group'); // $group = // $directories = $input->getArgument('directories'); // $files = array(); //ApplicationsDB::LoadAll(); foreach ($names as $name) { if ($name['exclamation'] !== false) { // adding user LogCLI::MessageResult('Exclamation: ' . $name['exclamation'], 2, LogCLI::INFO); $username = $name['text']; LogCLI::Message('Adding user: '******'Creating directory: ' . $structure, 2, LogCLI::INFO); if (@mkdir($structure, 0755, true)) { LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('User ' . $username . ' already exists!'); //LogCLI::Fail($e->getMessage()); } } else { // adding website $website = $name['text']; LogCLI::Message('Adding website: ' . $website, 0); $username = !empty($userorgroup) ? $userorgroup : Paths::$defaultUser; $group = Paths::$defaultGroup; LogCLI::MessageResult('User and group: ' . $username . '/' . $group, 2, LogCLI::INFO); $path = Paths::$db . Paths::$separator . $group . Paths::$separator . $username . Paths::$separator; if (file_exists($path)) { if (!file_exists($path . $website . '.yml') && Paths::getFullPath($website) === false) { FileOperation::CreateEmptyFile($path . $website . '.yml'); LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Website ' . $website . ', under ' . $group . '/' . $username . ' already exists!'); } } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Group and/or user ' . $group . '/' . $username . ' does not exist!'); } } } }
public static function Add($arguments) { foreach (StringTools::TypeList($arguments['name'], '@') as $argument) { $name = $argument['text']; if ($argument['exclamation'] !== false) { LogCLI::MessageResult('Exclamation: ' . $argument['exclamation'], 2, LogCLI::INFO); $username = $name; LogCLI::Message('Adding user: '******'name2']) ? $arguments['name2'] : Paths::$defaultGroup; $structure = Paths::$db . Paths::$separator . $group . Paths::$separator . $username; LogCLI::MessageResult('Creating directory: ' . $structure, 2, LogCLI::INFO); if (@mkdir($structure, 0755, true)) { LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('User ' . $username . ' already exists!'); //LogCLI::Fail($e->getMessage()); } } else { // adding website $website = $name; LogCLI::Message('Adding website: ' . $website, 0); $username = isset($arguments['name2']) ? $arguments['name2'] : Paths::$defaultUser; $group = Paths::$defaultGroup; LogCLI::MessageResult('Username and group: ' . $username . '/' . $group, 2, LogCLI::INFO); $path = Paths::$db . Paths::$separator . $group . Paths::$separator . $username . Paths::$separator; if (file_exists($path)) { if (!file_exists($path . $website . '.yml') && Paths::getFullPath($website) === false) { FileOperation::CreateEmptyFile($path . $website . '.yml'); LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Website ' . $website . ', under ' . $group . '/' . $username . ' already exists!'); } } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Group and/or user ' . $group . '/' . $username . ' does not exist!'); } } } }
/** * @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()); } } } }
public function makeTree($makePathInstance = null, $scope, $depth = 0, $parentIterative = false, $parent = '') { $return = array(); if (isset($this->templates[$scope])) { ++$depth; preg_match_all('/<<(?<name>\\w+)>>/', $this->templates[$scope], $matches); preg_match_all('/@@(?<name>\\w+)@@/', $this->templates[$scope], $matchesDynamic); $matches = array_merge_recursive($matches, $matchesDynamic); preg_match_all('/<!<(?<name>\\w+)>!>/', $this->templates[$scope], $matchesIterative); /* preg_match_all('/@!@(?<name>\w+)@!@/', $this->templates[$scope], $matchesIterativeDynamic); $matchesIterative = array_merge_recursive($matchesIterative, $matchesIterativeDynamic); */ $parentDisplay = strlen($parent) > 0 ? LogCLI::GREEN . $parent . LogCLI::RESET . ' => ' : null; if (isset($makePathInstance)) { $makePathInstance->begin($scope); } if (!empty($matches['name'])) { foreach ($matches['name'] as $match) { //if(isset($makePathInstance)) $makePathInstance->begin($match); LogCLI::Message('(' . $depth . ') ' . $parentDisplay . LogCLI::BLUE . $scope . LogCLI::RESET . " => " . LogCLI::YELLOW . $match . LogCLI::RESET, 2); $children = $this->makeTree($makePathInstance, $match, $depth, $parentIterative, $scope); LogCLI::Result(LogCLI::INFO); //if(isset($makePathInstance)) $makePathInstance->end(); } $return = $matches['name']; } if (!empty($matchesIterative['name'])) { foreach ($matchesIterative['name'] as $match) { //if(isset($makePathInstance)) $makePathInstance->begin($match); LogCLI::Message('(' . $depth . ') ' . $parentDisplay . LogCLI::BLUE . $scope . LogCLI::RESET . " => " . LogCLI::YELLOW . $match . LogCLI::RESET . " => " . LogCLI::RED . '[iterative]' . LogCLI::RESET, 2); // some debugging: /* LogCLI::MessageResult('MakePathInstance = '.$makePathInstance, LogCLI::INFO); LogCLI::MessageResult('Match = '.$match, LogCLI::INFO); LogCLI::MessageResult('Depth = '.$depth, LogCLI::INFO); LogCLI::MessageResult('Scope = '.$scope, LogCLI::INFO); */ $children = $this->makeTree($makePathInstance, $match, $depth, true, $scope); LogCLI::Result(LogCLI::INFO); //if(isset($makePathInstance)) $makePathInstance->end(); $this->settingsList[$match]['iterative'] = true; } $return = array_merge($return, $matchesIterative['name']); } if (isset($makePathInstance)) { $makePathInstance->end(); } // $scope if (isset($return)) { return $return; } } else { LogCLI::Fail('No matching stem for: ' . $scope . '! Please verify your configuration file.'); } return array(); }
public function ParseFromYAMLs(array $files) { $this->SetAutomatedValues(); //Stems::LoadDefinitions('nginx'); $last = false; $total = count($files) - 1; $sites_defaults = array(); foreach ($files as $i => $file) { if ($total == $i) { $last = true; } LogCLI::Message('Loading file: ' . LogCLI::BLUE . $file . LogCLI::RESET, 1); if (file_exists($file)) { LogCLI::Result(LogCLI::OK); LogCLI::Message('Parsing file: ' . LogCLI::BLUE . $file . LogCLI::RESET, 1); try { $config = YAML::load($file); LogCLI::Result(LogCLI::OK); } catch (Exception $e) { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail($e->getMessage()); } if (isset($config['nginx'])) { // adding to main DB $this->settingsDB = System::MergeArrays($this->settingsDB, $config); foreach ($this->usableScopes as $arrayPath) { //LogCLI::MessageResult($arrayPath); //var_dump(self::getArrayElementByPath($config['nginx'], $arrayPath, 1)); /* foreach(self::accessArrayElementByPath($config['nginx'], $arrayPath) as $scope => $scopeConfig) { echo("skope ".$scope.":\n"); } */ //LogCLI::MessageResult('Path: '.LogCLI::BLUE.$arrayPath.LogCLI::RESET, 6, LogCLI::INFO); foreach (self::getArrayElementByPath($config['nginx'], $arrayPath, true) as $scope => $scopeConfig) { if ($scopeConfig !== false) { //echo("skope ".$scope.":\n"); //var_dump($scopeConfig); if (is_array($scopeConfig) && $scope != 'sites') { //LogCLI::MessageResult("Number of dimentions in scope [$scope]: ".self::array_dimen_count($scopeConfig), 3); //self::array_dimen_count($scopeConfig); //var_dump($scopeConfig); //LogCLI::MessageResult($scope); NginxConfig::setAll(self::accessArrayElementByPath($config['nginx'], $arrayPath), self::accessArrayElementByPath($this->nginx, $arrayPath)); if ($last) { //LogCLI::MessageResult($scope); LogCLI::Message("Generating scope: [{$arrayPath}]", 3); NginxScope::addAllStems(self::accessArrayElementByPath($this->nginx, $arrayPath), $this->confScope); LogCLI::Result(LogCLI::INFO); } } } } } // sites scopes if (isset($config['nginx']['sites']['_defaults'])) { $config['nginx']['sites']['_defaults'] = self::ConformizeConfigSite($config['nginx']['sites']['_defaults']); $sites_defaults = System::MergeArrays($sites_defaults, $config['nginx']['sites']['_defaults']); } if (isset($config['nginx']['sites'])) { foreach ($config['nginx']['sites'] as $key => $site) { if ($key != '_defaults') { $site = self::ConformizeConfigSite($site); $siteScope[$key] = new NginxScope(); LogCLI::Message("Pushing defaults for subscope [server]: {$key}", 3); NginxConfig::resetAll($this->nginx['sites']); if (isset($sites_defaults)) { NginxConfig::setAll($sites_defaults, $this->nginx['sites']); } LogCLI::Result(LogCLI::INFO); LogCLI::Message("Setting in subscope [server]: {$key}", 3); //$siteScope[$key] = new NginxScope; NginxConfig::setAll($site, $this->nginx['sites']); LogCLI::Result(LogCLI::INFO); LogCLI::Message("Adding subscope [server]: {$key}", 3); NginxScope::addAllStems($this->nginx['sites'], $siteScope[$key]); LogCLI::Result(LogCLI::INFO); } } } if (isset($siteScope)) { foreach ($siteScope as $scope) { LogCLI::Message("Adding scope: [server]", 3); $this->confScope->addStem(array('scope' => 'http', 'output' => $scope->returnScopes(), 'level' => 1)); LogCLI::Result(LogCLI::INFO); } } $this->confScope->orderScopes(array('_ROOT', 'events', 'http')); } } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fatal("No such file: {$file}"); } } //return false; }
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']); } } } }