protected function listCommands($directory) { if (is_dir($directory) === false) { return array(); } $finder = new Finder(); $finder->in($directory)->name('#(.*)\\.php#'); $group = array(); $out = array(); $extact = function ($uri) { $lines = file($uri); $description = ''; // Berk… for ($i = count($lines) - 1; $i >= 0; --$i) { if (strpos($lines[$i], '__halt_compiler();') === 0) { $description = trim(implode('', array_slice($lines, $i + 1))); break; } } unset($lines); return $description; }; foreach ($finder as $cmd) { /** * @var \SplFileInfo $cmd */ $path = str_replace('\\', '/', $cmd->getPath()); $category = substr($path, strrpos($path, '/') + 1, strlen($path)); $command = $cmd->getBasename('.php'); $description = $extact($cmd->getPathname()); $group[$category][$command] = $description; } foreach ($group as $category => $command) { $out[] = \Hoa\Console\Chrome\Text::colorize($category, 'fg(green)'); foreach ($command as $name => $description) { $out[] = array(' ', \Hoa\Console\Chrome\Text::colorize($name, 'fg(blue)'), $description); } } echo Text::columnize($out); }
/** * The entry method. * * @access public * @return int */ public function main() { $command = null; while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'h': case '?': return $this->usage(); break; } } $cache = 'hoa://Application/Cache/Route.php'; $route = 'hoa://Application/Config/Route.php'; $core = Core::getInstance(); $parameters = $core->getParameters(); $cwd = $parameters->getKeyword('cwd'); $parameters->setKeyword('cwd', $cwd . '/Public'); $framework = new Framework(); $router = $framework->getRouter(); $router->construct(); echo '# Router rules in ' . resolve($route) . "\n\n"; echo Text::columnize($router->dump()) . "\n\n"; return; }
/** * The entry method. * * @return int */ public function main() { $library = null; $verbose = Console::isDirect(STDOUT); while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'l': $library = $this->parser->parseSpecialValue($v); break; case 'V': $verbose = false; break; case 'h': case '?': return $this->usage(); case '__ambiguous': $this->resolveOptionAmbiguity($v); break; } } if (true === $verbose) { echo Console\Chrome\Text::colorize(' _ _' . "\n" . ' | | | | ___ __ _' . "\n" . ' | |_| |/ _ \\ / _` |' . "\n" . ' | _ | (_) | (_| |' . "\n" . ' |_| |_|\\___/ \\__,_|' . "\n", 'foreground(yellow)'), "\n\n", 'Welcome in the command-line interface of Hoa :-).', "\n\n", Console\Chrome\Text::colorize('List of available commands', 'foreground(green)'), "\n\n"; } if (null !== $library) { $library = array_map('mb_strtolower', $library); } $locations = resolve('hoa://Library', true, true); $iterator = new \AppendIterator(); foreach ($locations as $location) { $iterator->append(new \GlobIterator($location . DS . '*' . DS . 'Bin' . DS . '*.php')); } if (WITH_COMPOSER) { $iterator->append(new \GlobIterator(dirname(dirname(dirname(dirname(__DIR__)))) . DS . 'Bin' . DS . '*.php')); } $binaries = []; foreach ($iterator as $entry) { $pathname = $entry->getPathname(); $lib = mb_strtolower(basename(dirname(dirname($pathname)))); $bin = mb_strtolower(mb_substr($entry->getBasename(), 0, -4)); if (null !== $library && false === in_array($lib, $library)) { continue; } if ('core' === $lib && 'hoa' === $bin) { continue; } if (!isset($binaries[$lib])) { $binaries[$lib] = []; } $description = ''; if (true === $verbose) { $lines = file($pathname); // Berk… for ($i = count($lines) - 1; $i >= 0; --$i) { if ('__halt_compiler();' . "\n" === $lines[$i]) { $description = trim(implode('', array_slice($lines, $i + 1))); break; } } unset($lines); } $binaries[$lib][] = ['name' => $bin, 'description' => $description]; } if (true === $verbose) { $out = []; foreach ($binaries as $group => $commands) { $out[] = [mb_convert_case($group, MB_CASE_TITLE)]; foreach ($commands as $binary) { $out[] = [' ' . Console\Chrome\Text::colorize($binary['name'], 'foreground(blue)'), $binary['description']]; } } echo Console\Chrome\Text::columnize($out); } else { $out = null; foreach ($binaries as $group => $commands) { foreach ($commands as $binary) { $out .= $group . ':' . $binary['name'] . "\n"; } } echo $out; } return; }
/** * The entry method. * * @access public * @return int */ public function main() { $file = null; $debug = false; $dry = false; $directory = null; $output = 'out/'; $style_formatter = 'Cli'; while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'f': $file = $v; break; case 'd': $directory = $v; break; case 'o': $output = $v; break; case 's': $style_formatter = $v; break; case 'v': $debug = true; break; case 'p': $dry = true; break; case 'h': case '?': default: return $this->usage(); break; } } echo \Hoa\Console\Chrome\Text::colorize('Sohapi', 'fg(yellow)'), "\n\n"; $root = realpath(__DIR__ . '/../../../../'); $out = realpath($root . '/' . $output); // TODO : Detect relative avec absolute path ! $formatter = '\\Sohapi\\Formatter\\' . ucfirst($style_formatter); if ($file !== null) { $file = realpath($root . '/' . $file); } if ($directory !== null) { $directory = realpath($root . '/' . $directory); } if ($out === false) { $out = $root; } if ($debug === true) { //\Sohapi\Parser\Ast::enableDebug(); $a = [['ROOT', var_export($root, true)], ['Debug', var_export($debug, true)], ['Dry', var_export($dry, true)], ['Formatter', var_export($formatter, true)], ['Output', var_export($out, true)], ['File', var_export($file, true)], ['Directory', var_export($directory, true)]]; echo \Hoa\Console\Chrome\Text::columnize($a); } $files = array(); $this->searchLocalConfig($root, $files); if ($file !== null and !in_array($file, $files)) { $files = array($file); } if ($directory !== null) { if ($this->searchLocalConfig($directory, $files) === false) { $finder = new \Hoa\File\Finder(); $finder->in($directory)->files()->name('#\\.php$#'); foreach ($finder as $f) { if (!in_array($this->clean($f->getPathName()), $files)) { $files[] = $this->clean($f->getPathName()); } } } } echo 'Found ' . count($files) . ' Files' . "\n"; foreach ($files as $i => $file) { echo 'Parsing : [' . ($i + 1) . '/' . count($files) . '] ' . $file . "\n"; if ($dry === false) { (new \Sohapi\Parser\Reader(file_get_contents($file)))->build(); } } $options = []; dnew($formatter, ['options' => ['output' => $out, 'debug' => $debug, 'dry' => $dry, 'options' => $options]])->render(); return; }
/** * The entry method. * * @return void */ public function main() { $run = 'hoa://Data/Variable/Run'; while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'r': $run = $v; break; case 'h': case '?': return $this->usage(); break; case '__ambiguous': $this->resolveOptionAmbiguity($v); break; } } $run = resolve($run); Core::getInstance()->initialize(['protocol.Data/Variable/Run' => "\r" . $run . DS]); $outi = [['ID', 'PID', 'Socket', 'Uptime', 'Messages', 'Last']]; $outm = []; $now = new \DateTime(); $t = 0; echo Console\Chrome\Text::colorize('Shared worker informations', 'foreground(yellow)'), "\n\n"; foreach (glob($run . DS . '*.wid') as $wid) { $worker = new Worker\Shared(substr(basename($wid), 0, -4)); $infos = $worker->getInformations(); $uptime = new \DateTime(); $uptime->setTimestamp((int) $infos['start']); $last = new \DateTime(); $last->setTimestamp((int) $infos['last_message']); $outi[] = [$infos['id'], $infos['pid'], $infos['socket'], $uptime->diff($now)->format('%ad%H:%I:%S'), $infos['messages'], 0 === $infos['last_message'] ? '-' : $last->diff($now)->format('%ad%H:%I:%S')]; $outm[] = $infos; ++$t; } echo Console\Chrome\Text::columnize($outi, 0, 1, '|'), "\n"; $max_id = 0; $max_peak = 0; foreach ($outm as $m) { $max_id < strlen($m['id']) and $max_id = strlen($m['id']); $max_peak < $m['memory_peak'] and $max_peak = $m['memory_peak']; } foreach ($outm as $m) { $outmm = str_pad($m['id'], $max_id) . ' '; $max = (int) ($m['memory_peak'] * 39 / $max_peak); $peak = (int) ($m['memory_allocated_peak'] * 40 / $max_peak); $memory = (int) ($m['memory_allocated'] * 40 / $max_peak); for ($i = 0; $i < $memory - 1; ++$i) { $outmm .= Console\Chrome\Text::colorize('|', 'foreground(green)'); } for (; $i < $peak; ++$i) { $outmm .= Console\Chrome\Text::colorize('|', 'foreground(yellow)'); } for (; $i < $max; ++$i) { $outmm .= ' '; } $outmm .= Console\Chrome\Text::colorize('|', 'foreground(red)'); for (++$i; $i < 40; ++$i) { $outmm .= ' '; } $outmm .= ' ' . Console\Chrome\Text::colorize(number_format($m['memory_allocated'] / 1024) . 'Kb', 'foreground(green)') . ' ' . Console\Chrome\Text::colorize(number_format($m['memory_allocated_peak'] / 1024) . 'Kb', 'foreground(yellow)') . ' ' . Console\Chrome\Text::colorize(number_format($m['memory_peak'] / 1024) . 'Kb', 'foreground(red)'); echo $outmm . "\n"; } echo "\n", $t, ' shared worker', $t > 1 ? 's are' : ' is', ' running.', "\n"; return; }
/** * It is a helper to make the usage options list. * * @access public * @param array $definitions An associative arry: short or long option * associated to the definition. * @return string */ public function makeUsageOptionsList(array $definition = array()) { $out = array(); foreach ($this->options as $i => $options) { $out[] = array(' -' . $options[\Hoa\Console\GetOption::OPTION_VAL] . ', --' . $options[\Hoa\Console\GetOption::OPTION_NAME] . ($options[\Hoa\Console\GetOption::OPTION_HAS_ARG] === \Hoa\Console\GetOption::REQUIRED_ARGUMENT ? '=' : ($options[\Hoa\Console\GetOption::OPTION_HAS_ARG] === \Hoa\Console\GetOption::OPTIONAL_ARGUMENT ? '[=]' : '')), isset($definition[$options[\Hoa\Console\GetOption::OPTION_VAL]]) ? $definition[$options[\Hoa\Console\GetOption::OPTION_VAL]] : (isset($definition[$options[0]]) ? $definition[$options[\Hoa\Console\GetOption::OPTION_NAME]] : null)); } return \Hoa\Console\Chrome\Text::columnize($out, \Hoa\Console\Chrome\Text::ALIGN_LEFT, 0.5, 0, '|: '); }