protected function execute(Input $input, Output $output) { $classmapFile = <<<EOF <?php /** * 类库映射 */ return [ EOF; $namespacesToScan = [App::$namespace . '\\' => realpath(rtrim(APP_PATH)), 'think\\' => LIB_PATH . 'think', 'behavior\\' => LIB_PATH . 'behavior', 'traits\\' => LIB_PATH . 'traits', '' => realpath(rtrim(EXTEND_PATH))]; krsort($namespacesToScan); $classMap = []; foreach ($namespacesToScan as $namespace => $dir) { if (!is_dir($dir)) { continue; } $namespaceFilter = $namespace === '' ? null : $namespace; $classMap = $this->addClassMapCode($dir, $namespaceFilter, $classMap); } ksort($classMap); foreach ($classMap as $class => $code) { $classmapFile .= ' ' . var_export($class, true) . ' => ' . $code; } $classmapFile .= "];\n"; if (!is_dir(RUNTIME_PATH)) { @mkdir(RUNTIME_PATH, 0755, true); } file_put_contents(RUNTIME_PATH . 'classmap' . EXT, $classmapFile); $output->writeln('<info>Succeed!</info>'); }
/** * @param Output $output * @param Exception $e */ public function renderForConsole(Output $output, Exception $e) { if (Config::get('DEBUG')) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } $output->renderException($e); }
/** * @param Output $output * @param Exception $e */ public function renderForConsole(Output $output, Exception $e) { if (App::$debug) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } (new Console())->renderException($e, $output); }
/** * {@inheritdoc} */ protected function execute(Input $input, Output $output) { if (null === $this->command) { $this->command = $this->getConsole()->find($input->getArgument('command_name')); } $output->describe($this->command, ['raw_text' => $input->getOption('raw')]); $this->command = null; }
/** * 包装过程回调来添加调试输出 * @param Output $output * @param ThinkProcess $process * @param callable|null $callback * @return callable */ public function wrapCallback(Output $output, ThinkProcess $process, $callback = null) { /** @var Debug $formatter */ $formatter = $this->getHelperSet()->get('debug_formatter'); return function ($type, $buffer) use($output, $process, $callback, $formatter) { $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), ThinkProcess::ERR === $type)); if (null !== $callback) { call_user_func($callback, $type, $buffer); } }; }
protected function execute(Input $input, Output $output) { if ($input->hasOption('module')) { $module = $input->getOption('module') . DS; } else { $module = ''; } $content = '<?php ' . PHP_EOL . $this->buildCacheContent($module); if (!is_dir(RUNTIME_PATH . $module)) { @mkdir(RUNTIME_PATH . $module, 0755, true); } file_put_contents(RUNTIME_PATH . $module . 'init' . EXT, $content); $output->writeln('<info>Succeed!</info>'); }
protected function execute(Input $input, Output $output) { if ($input->hasOption('config')) { $build = (include $input->getOption('config')); } else { $build = (include APP_PATH . 'build.php'); } if (empty($build)) { $output->writeln("Build Config Is Empty"); return; } \think\Build::run($build); $output->writeln("Successed"); }
protected function execute(Input $input, Output $output) { if (!is_dir(RUNTIME_PATH . 'schema')) { @mkdir(RUNTIME_PATH . 'schema', 0755, true); } if ($input->hasOption('module')) { $module = $input->getOption('module'); // 读取模型 $list = scandir(APP_PATH . $module . DS . 'model'); $app = App::$namespace; foreach ($list as $file) { if ('.' == $file || '..' == $file) { continue; } $class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME); $this->buildModelSchema($class); } $output->writeln('<info>Succeed!</info>'); return; } else { if ($input->hasOption('table')) { $table = $input->getOption('table'); if (!strpos($table, '.')) { $dbName = Db::getConfig('database'); } $tables[] = $table; } elseif ($input->hasOption('db')) { $dbName = $input->getOption('db'); $tables = Db::getTables($dbName); } elseif (!\think\Config::get('app_multi_module')) { $app = App::$namespace; $list = scandir(APP_PATH . 'model'); foreach ($list as $file) { if ('.' == $file || '..' == $file) { continue; } $class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME); $this->buildModelSchema($class); } $output->writeln('<info>Succeed!</info>'); return; } else { $tables = Db::getTables(); } } $db = isset($dbName) ? $dbName . '.' : ''; $this->buildDataBaseSchema($tables, $db); $output->writeln('<info>Succeed!</info>'); }
protected function execute(Input $input, Output $output) { $name = trim($input->getArgument('name')); $classname = $this->getClassName($name); $pathname = $this->getPathName($classname); if (is_file($pathname)) { $output->writeln('<error>' . $this->type . ' already exists!</error>'); return false; } if (!is_dir(dirname($pathname))) { mkdir(strtolower(dirname($pathname)), 0755, true); } file_put_contents($pathname, $this->buildClass($classname)); $output->writeln('<info>' . $this->type . ' created successfully.</info>'); }
protected function execute(Input $input, Output $output) { $path = $input->getOption('path') ?: RUNTIME_PATH; $files = scandir($path); if ($files) { foreach ($files as $file) { if ('.' != $file && '..' != $file && is_dir($path . $file)) { array_map('unlink', glob($path . $file . '/*.*')); } elseif (is_file($path . $file)) { unlink($path . $file); } } } $output->writeln("<info>Clear Successed</info>"); }
/** * 显示问题的提示信息 */ protected function writePrompt() { $text = $this->question->getQuestion(); $default = $this->question->getDefault(); switch (true) { case null === $default: $text = sprintf(' <info>%s</info>:', $text); break; case $this->question instanceof Confirmation: $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no'); break; case $this->question instanceof Choice && $this->question->isMultiselect(): $choices = $this->question->getChoices(); $default = explode(',', $default); foreach ($default as $key => $value) { $default[$key] = $choices[trim($value)]; } $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default)); break; case $this->question instanceof Choice: $choices = $this->question->getChoices(); $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]); break; default: $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default); } $this->output->writeln($text); if ($this->question instanceof Choice) { $width = max(array_map('strlen', array_keys($this->question->getChoices()))); foreach ($this->question->getChoices() as $key => $value) { $this->output->writeln(sprintf(" [<comment>%-{$width}s</comment>] %s", $key, $value)); } } $this->output->write(' > '); }
public function renderException(\Exception $e) { $stderr = $this->openErrorStream(); $decorated = $this->hasColorSupport($stderr); $this->formatter->setDecorated($decorated); do { $title = sprintf(' [%s] ', get_class($e)); $len = $this->stringWidth($title); $width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX; if (defined('HHVM_VERSION') && $width > 1 << 31) { $width = 1 << 31; } $lines = []; foreach (preg_split('/\\r?\\n/', $e->getMessage()) as $line) { foreach ($this->splitStringByWidth($line, $width - 4) as $line) { $lineLength = $this->stringWidth(preg_replace('/\\[[^m]*m/', '', $line)) + 4; $lines[] = [$line, $lineLength]; $len = max($lineLength, $len); } } $messages = ['', '']; $messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len)); $messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title)))); foreach ($lines as $line) { $messages[] = sprintf('<error> %s %s</error>', $line[0], str_repeat(' ', $len - $line[1])); } $messages[] = $emptyLine; $messages[] = ''; $messages[] = ''; $this->write($messages, true, Output::OUTPUT_NORMAL, $stderr); if (Output::VERBOSITY_VERBOSE <= $this->output->getVerbosity()) { $this->write('<comment>Exception trace:</comment>', true, Output::OUTPUT_NORMAL, $stderr); // exception related properties $trace = $e->getTrace(); array_unshift($trace, ['function' => '', 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a', 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a', 'args' => []]); for ($i = 0, $count = count($trace); $i < $count; ++$i) { $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : ''; $function = $trace[$i]['function']; $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a'; $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a'; $this->write(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line), true, Output::OUTPUT_NORMAL, $stderr); } $this->write('', true, Output::OUTPUT_NORMAL, $stderr); $this->write('', true, Output::OUTPUT_NORMAL, $stderr); } } while ($e = $e->getPrevious()); }
protected function createMap($path, $namespace = null) { if (is_string($path)) { if (is_file($path)) { $path = [new \SplFileInfo($path)]; } elseif (is_dir($path)) { $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST); $path = []; /** @var \SplFileInfo $object */ foreach ($objects as $object) { if ($object->isFile() && $object->getExtension() == 'php') { $path[] = $object; } } } else { throw new \RuntimeException('Could not scan for classes inside "' . $path . '" which does not appear to be a file nor a folder'); } } $map = []; /** @var \SplFileInfo $file */ foreach ($path as $file) { $filePath = $file->getRealPath(); if (pathinfo($filePath, PATHINFO_EXTENSION) != 'php') { continue; } $classes = $this->findClasses($filePath); foreach ($classes as $class) { if (null !== $namespace && 0 !== strpos($class, $namespace)) { continue; } if (!isset($map[$class])) { $map[$class] = $filePath; } elseif ($map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class] . ' ' . $filePath, '\\', '/'))) { $this->output->writeln('<warning>Warning: Ambiguous class resolution, "' . $class . '"' . ' was found in both "' . $map[$class] . '" and "' . $filePath . '", the first will be used.</warning>'); } } } return $map; }
/** * 配置基于用户的参数和选项的输入和输出实例。 * @param Input $input 输入实例 * @param Output $output 输出实例 */ protected function configureIO(Input $input, Output $output) { if (true === $input->hasParameterOption(['--ansi'])) { $output->setDecorated(true); } elseif (true === $input->hasParameterOption(['--no-ansi'])) { $output->setDecorated(false); } if (true === $input->hasParameterOption(['--no-interaction', '-n'])) { $input->setInteractive(false); } if (true === $input->hasParameterOption(['--quiet', '-q'])) { $output->setVerbosity(Output::VERBOSITY_QUIET); } else { if ($input->hasParameterOption('-vvv') || $input->hasParameterOption('--verbose=3') || $input->getParameterOption('--verbose') === 3) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } elseif ($input->hasParameterOption('-vv') || $input->hasParameterOption('--verbose=2') || $input->getParameterOption('--verbose') === 2) { $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE); } elseif ($input->hasParameterOption('-v') || $input->hasParameterOption('--verbose=1') || $input->hasParameterOption('--verbose') || $input->getParameterOption('--verbose')) { $output->setVerbosity(Output::VERBOSITY_VERBOSE); } } }
/** * {@inheritdoc} */ protected function execute(Input $input, Output $output) { $output->describe($this->getConsole(), ['raw_text' => $input->getOption('raw'), 'namespace' => $input->getArgument('namespace')]); }
/** * 从用户获取隐藏的响应 * @param Output $output * @return string * @throws \RuntimeException */ private function getHiddenResponse(Output $output, $inputStream) { if ('\\' === DS) { $exe = __DIR__ . '/../bin/hiddeninput.exe'; if ('phar:' === substr(__FILE__, 0, 5)) { $tmpExe = sys_get_temp_dir() . '/hiddeninput.exe'; copy($exe, $tmpExe); $exe = $tmpExe; } $value = rtrim(shell_exec($exe)); $output->writeln(''); if (isset($tmpExe)) { unlink($tmpExe); } return $value; } if ($this->hasSttyAvailable()) { $sttyMode = shell_exec('stty -g'); shell_exec('stty -echo'); $value = fgets($inputStream, 4096); shell_exec(sprintf('stty %s', $sttyMode)); if (false === $value) { throw new \RuntimeException('Aborted'); } $value = trim($value); $output->writeln(''); return $value; } if (false !== ($shell = $this->getShell())) { $readCmd = $shell === 'csh' ? 'set mypassword = $<' : 'read -r mypassword'; $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd); $value = rtrim(shell_exec($command)); $output->writeln(''); return $value; } throw new \RuntimeException('Unable to hide the response.'); }
/** * 配置基于用户的参数和选项的输入和输出实例。 * @param Input $input 输入实例 * @param Output $output 输出实例 */ protected function configureIO(Input $input, Output $output) { if (true === $input->hasParameterOption(['--ansi'])) { $output->setDecorated(true); } elseif (true === $input->hasParameterOption(['--no-ansi'])) { $output->setDecorated(false); } if (true === $input->hasParameterOption(['--no-interaction', '-n'])) { $input->setInteractive(false); } elseif (function_exists('posix_isatty') && $this->getHelperSet()->has('question')) { $inputStream = $this->getHelperSet()->get('question')->getInputStream(); if (!@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) { $input->setInteractive(false); } } if (true === $input->hasParameterOption(['--quiet', '-q'])) { $output->setVerbosity(Output::VERBOSITY_QUIET); } else { if ($input->hasParameterOption('-vvv') || $input->hasParameterOption('--verbose=3') || $input->getParameterOption('--verbose') === 3) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } elseif ($input->hasParameterOption('-vv') || $input->hasParameterOption('--verbose=2') || $input->getParameterOption('--verbose') === 2) { $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE); } elseif ($input->hasParameterOption('-v') || $input->hasParameterOption('--verbose=1') || $input->hasParameterOption('--verbose') || $input->getParameterOption('--verbose')) { $output->setVerbosity(Output::VERBOSITY_VERBOSE); } } }
/** * 输出内容 * @param string $content * @param bool $decorated */ protected function write($content, $decorated = false) { $this->output->write($content, false, $decorated ? Output::OUTPUT_NORMAL : Output::OUTPUT_RAW); }
protected function execute(Input $input, Output $output) { file_put_contents(RUNTIME_PATH . 'route.php', $this->buildRouteCache()); $output->writeln('<info>Succeed!</info>'); }