/** * 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); } }
/** * 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; }
/** * Prints a string to STDERR * * You may optionally format the string with ANSI codes by * passing additional parameters using the constants defined in [[\Leaps\Helper\Console]]. * * Example: * * ~~~ * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * ~~~ * * @param string $string the string to print * @return int|boolean Number of bytes printed or false on error */ public function stderr($string) { if ($this->isColorEnabled(\STDERR)) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); } return fwrite(\STDERR, $string); }
/** * 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]); }