/** * Synchronize application data (packages metadata and database packages rows). * * @return void */ public function syncAction() { try { /** * Add missing packages. * Read packages files and find packages that is missing in db. * * $modulesWidgets - array of widgets that is located in modules [module => [widgets...n]]. * $notFoundWidgets - array of widgets as external packages [widgets...n]. * $packages - all packages names found at metadata files. * $widgets - all widgets names found at metadata files. */ list($modulesWidgets, $notFoundWidgets, $packages, $widgets) = $this->_checkMissingPackages(); /** * Add missing widgets from modules and from packages. */ $this->_checkMissingWidgets($modulesWidgets, $notFoundWidgets); /** * Remove unused packages. */ $this->_removeUnusedPackages($packages); /** * Remove unused widgets. */ $this->_removeUnusedWidgets($widgets); /** * Generate metadata. */ $manager = new Manager(Package::find(), $this->getDI()); $manager->generateMetadata(null, true); print ConsoleUtil::success('Application successfully synchronized.') . PHP_EOL; } catch (Exception $e) { print ConsoleUtil::error($e->getMessage()) . PHP_EOL; } }
/** * Update database schema according to models metadata. * * @param string|null $model Model name to update. Example: \Test\Model\Class. * @param bool $cleanup Cleanup database? Drop not related tables. * * @return void */ public function updateAction($model = null, $cleanup = false) { $schema = new Schema($this->getDI()); if ($model) { if (!class_exists($model)) { print ConsoleUtil::error('Model with class "' . $model . '" doesn\'t exists.') . PHP_EOL; return; } $count = current($schema->updateTable($model)); if ($count) { print ConsoleUtil::headLine('Table update for model: ' . $model); print ConsoleUtil::commandLine('Executed queries:', $count, ConsoleUtil::FG_CYAN); } else { print ConsoleUtil::success('Table is up to date'); } print PHP_EOL; } else { $queriesCount = $schema->updateDatabase($cleanup); if (!empty($queriesCount)) { print ConsoleUtil::headLine('Database update:'); foreach ($queriesCount as $model => $count) { print ConsoleUtil::commandLine($model . ':', $count, ConsoleUtil::FG_CYAN); } } else { print ConsoleUtil::success('Database is up to date'); } print PHP_EOL; } }
/** * Check passed parameters (required or no value). * * @param array $withoutValue Params without value. * * @return bool */ private function _checkParameters($withoutValue) { $action = $this->_parameters['action']; if (!empty($this->_actions[$action]['params'])) { foreach ($this->_actions[$action]['params'] as $actionParams) { // Check required param. if ($actionParams['defaultValueType'] == '<required>' && empty($this->_parameters[$actionParams['name']])) { print ConsoleUtil::error(sprintf('Parameter "%s" is required!', $actionParams['name'])); $this->getHelp($action); return false; } // Check required value of param. if ($actionParams['defaultValueType'] != 'boolean' && in_array($actionParams['name'], $withoutValue)) { print ConsoleUtil::error(sprintf('Parameter "%s" must have value!', $actionParams['name'])); $this->getHelp($action); return false; } } } return true; }
/** * Handle all data and output result. * * @throws Exception * @return mixed */ public function getOutput() { print ConsoleUtil::infoLine('================================================================', true, 0); print ConsoleUtil::infoLine("\n ___ __ __ ____\n / _ \\/ / ___ _/ _______ ___ / ____ _____\n / ___/ _ / _ `/ / __/ _ \\/ _ \\/ _// // / -_)\n /_/ /_//_\\_,_/_/\\__/\\___/_//_/___/\\_, /\\__/\n /___/\n Commands Manager", false, 1); print ConsoleUtil::infoLine('================================================================', false, 2); // Installation is required. if (!$this->_config->installed) { print ConsoleUtil::error('Please, install system first.') . PHP_EOL; die; } // Not arguments? if (!isset($_SERVER['argv'][1])) { $this->printAvailableCommands(); die; } // Check if 'help' command was used. if ($this->_helpIsRequired()) { return; } // Try to dispatch the command. if ($cmd = $this->_getRequiredCommand()) { return $cmd->dispatch(); } // Check for alternatives. $available = []; foreach ($this->_commands as $command) { $providedCommands = $command->getCommands(); foreach ($providedCommands as $command) { $soundex = soundex($command); if (!isset($available[$soundex])) { $available[$soundex] = []; } $available[$soundex][] = $command; } } // Show exception with/without alternatives. $soundex = soundex($_SERVER['argv'][1]); if (isset($available[$soundex])) { print ConsoleUtil::warningLine('Command "' . $_SERVER['argv'][1] . '" not found. Did you mean: ' . join(' or ', $available[$soundex]) . '?'); $this->printAvailableCommands(); } else { print ConsoleUtil::warningLine('Command "' . $_SERVER['argv'][1] . '" not found.'); $this->printAvailableCommands(); } }