/** * Colorizes a message for console output. * * @param string $message the message to colorize. * @param array $format the message format. * @return string the colorized message. * @see Console::ansiFormat() for details on how to specify the message format. */ protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD]) { $stream = PHP_SAPI === 'cli' ? \STDERR : \STDOUT; // try controller first to allow check for --color switch if (Leaps::$app->controller instanceof \Leaps\Console\Controller && Leaps::$app->controller->isColorEnabled($stream) || Leaps::$app instanceof \Leaps\Console\Application && Console::streamSupportsAnsiColors($stream)) { $message = Console::ansiFormat($message, $format); } return $message; }
/** * Writes messages into PHP files * * @param array $messages * @param string $dirName name of the directory to write to * @param boolean $overwrite if existing file should be overwritten without backup * @param boolean $removeUnused if obsolete translations should be removed * @param boolean $sort if translations should be sorted * @param boolean $markUnused if obsolete translations should be marked */ protected function saveMessagesToPHP($messages, $dirName, $overwrite, $removeUnused, $sort, $markUnused) { foreach ($messages as $category => $msgs) { $file = str_replace("\\", '/', "{$dirName}/{$category}.php"); $path = dirname($file); FileHelper::createDirectory($path); $msgs = array_values(array_unique($msgs)); $coloredFileName = Console::ansiFormat($file, [Console::FG_CYAN]); $this->stdout("Saving messages to {$coloredFileName}...\n"); $this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort, $category, $markUnused); } }
/** * Displays the overall information of the command. * @param Controller $controller the controller instance */ protected function getCommandHelp($controller) { $controller->color = $this->color; $this->stdout("\nDESCRIPTION\n", Console::BOLD); $comment = $controller->getHelp(); if ($comment !== '') { $this->stdout("\n{$comment}\n\n"); } $actions = $this->getActions($controller); if (!empty($actions)) { $this->stdout("\nSUB-COMMANDS\n\n", Console::BOLD); $prefix = $controller->getUniqueId(); $maxlen = 5; foreach ($actions as $action) { $len = strlen($prefix . '/' . $action) + 2 + ($action === $controller->defaultAction ? 10 : 0); if ($maxlen < $len) { $maxlen = $len; } } foreach ($actions as $action) { $this->stdout('- ' . $this->ansiFormat($prefix . '/' . $action, Console::FG_YELLOW)); $len = strlen($prefix . '/' . $action) + 2; if ($action === $controller->defaultAction) { $this->stdout(' (default)', Console::FG_GREEN); $len += 10; } $summary = $controller->getActionHelpSummary($controller->createAction($action)); if ($summary !== '') { $this->stdout(str_repeat(' ', $maxlen - $len + 2) . Console::wrapText($summary, $maxlen + 2)); } $this->stdout("\n"); } $scriptName = $this->getScriptName(); $this->stdout("\nTo see the detailed information about individual sub-commands, enter:\n"); $this->stdout("\n {$scriptName} " . $this->ansiFormat('help', Console::FG_YELLOW) . ' ' . $this->ansiFormat('<sub-command>', Console::FG_CYAN) . "\n\n"); } }
/** * Returns full description from the docblock. * * @param \Reflector $reflection * @return string */ protected function parseDocCommentDetail($reflection) { $comment = strtr(trim(preg_replace('/^\\s*\\**( |\\t)?/m', '', trim($reflection->getDocComment(), '/'))), "\r", ''); if (preg_match('/^\\s*@\\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) { $comment = trim(substr($comment, 0, $matches[0][1])); } if ($comment !== '') { return rtrim(Console::renderColoredString(Console::markdownToAnsi($comment))); } return ''; }
/** * Loads the specified fixture data. * For example, * * ~~~ * # load the fixture data specified by User and UserProfile. * # any existing fixture data will be removed first * yii fixture/load User UserProfile * * # load all available fixtures found under 'tests\unit\fixtures' * yii fixture/load "*" * * # load all fixtures except User and UserProfile * yii fixture/load "*" -User -UserProfile * ~~~ * * @throws Exception if the specified fixture does not exist. */ public function actionLoad() { $fixturesInput = func_get_args(); if ($fixturesInput === []) { $this->stdout($this->getHelpSummary() . "\n"); $helpCommand = Console::ansiFormat("leaps help fixture", [Console::FG_CYAN]); $this->stdout("Use {$helpCommand} to get usage info.\n"); return self::EXIT_CODE_NORMAL; } $filtered = $this->filterFixtures($fixturesInput); $except = $filtered['except']; if (!$this->needToApplyAll($fixturesInput[0])) { $fixtures = $filtered['apply']; $foundFixtures = $this->findFixtures($fixtures); $notFoundFixtures = array_diff($fixtures, $foundFixtures); if ($notFoundFixtures) { $this->notifyNotFound($notFoundFixtures); } } else { $foundFixtures = $this->findFixtures(); } $fixturesToLoad = array_diff($foundFixtures, $except); if (!$foundFixtures) { throw new Exception("No files were found by name: \"" . implode(', ', $fixturesInput) . "\".\n" . "Check that files with these name exists, under fixtures path: \n\"" . $this->getFixturePath() . "\"."); } if (!$fixturesToLoad) { $this->notifyNothingToLoad($foundFixtures, $except); return static::EXIT_CODE_NORMAL; } if (!$this->confirmLoad($fixturesToLoad, $except)) { return static::EXIT_CODE_NORMAL; } $fixtures = $this->getFixturesConfig(array_merge($this->globalFixtures, $fixturesToLoad)); if (!$fixtures) { throw new Exception('No fixtures were found in namespace: "' . $this->namespace . '"' . ''); } $fixturesObjects = $this->createFixtures($fixtures); $this->unloadFixtures($fixturesObjects); $this->loadFixtures($fixturesObjects); $this->notifyLoaded($fixtures); return static::EXIT_CODE_NORMAL; }
/** * Renders the strike through feature. * * @param array $element * @return string */ protected function renderStrike($element) { return Console::ansiFormat($this->parseInline($this->renderAbsy($element[1])), [Console::CROSSED_OUT]); }