/** * Prints a string to STDOUT. * * @param string $string the string to print * @return int|boolean Number of bytes printed or false on error */ public static function stdout($string) { $args = func_get_args(); array_shift($args); $string = parent::ansiFormat($string, $args) . "\n"; return parent::stdout($string); }
/** * Prints a string to STDOUT. * * @param string $string the string to print * @return int|boolean Number of bytes printed or false on error */ public function stdout($string) { if ($this->isColorEnabled()) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args) . "\n"; } return Console::stdout($string); }
public function stdout($string) { if (Console::streamSupportsAnsiColors(STDOUT)) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); } return Console::stdout($string); }
/** * message * * @param string $string * @access private * @return integer|null */ private function message($string) { if (!empty($string) && !$this->quiet) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); return Console::stdout($string); } }
/** * Logs a message to console and then to yii\log\Logger. */ public function log($message, $level, $category = 'application') { if ($level <= $this->getSpamLevel()) { $style = self::$styles[$level]; if ($style) { $message = Console::ansiFormat($message, $style); } Console::stdout($message . "\n"); } parent::log($message, $level, $category); }
/** * * @param string $string * @return string */ public function output($string) { if ($this->isColorEnabled()) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); } $b = Console::stdout($string); echo PHP_EOL; return $b; }
public static function write($path, $content) { if (file_exists($path)) { $old = file_get_contents($path); if ($old === $content) { return; } } file_put_contents($path, $content); Console::stdout(Console::ansiFormat("written file: {$path}\n", [Console::FG_YELLOW])); }
/** * @inheritdoc */ public function export() { foreach ($this->messages as $message) { $string = $this->formatMessage($message) . "\n"; $level = $message[1]; if ($this->stderrIsNotStdout && in_array($level, $this->stderrLevels)) { if ($this->stderrSupportsColors) { Console::stderr(Console::ansiFormat($string, $this->colorMap[$level])); } else { Console::stderr($string); } } if ($this->stdoutSupportsColors) { Console::stdout(Console::ansiFormat($string, $this->colorMap[$level])); } else { Console::stdout($string); } } }
/** * Exports log [[messages]] to a specific destination. * Child classes must implement this method. */ public function export() { foreach ($this->messages as $message) { list($text, $level, $category, $timestamp) = $message; if (!is_string($text)) { // exceptions may not be serializable if in the call stack somewhere is a Closure if ($text instanceof \Throwable || $text instanceof \Exception) { $text = (string) $text; } else { $text = VarDumper::export($text); } } $string = "[{$level}][{$category}] {$text}"; if ($level == Logger::LEVEL_ERROR) { Console::stderr($string); } else { Console::stdout($string); } } }
public function callback(AMQPMessage $msg) { $routingKey = $msg->delivery_info['routing_key']; $method = 'read' . Inflector::camelize($routingKey); if (!isset($this->interpreters[$this->queue])) { $interpreter = $this; } elseif (class_exists($this->interpreters[$this->queue])) { $interpreter = new $this->interpreters[$this->queue](); if (!$interpreter instanceof AmqpInterpreter) { throw new Exception(sprintf("Class '%s' is not correct interpreter class.", $this->interpreters[$this->queue])); } } else { throw new Exception(sprintf("Interpreter class '%s' was not found.", $this->interpreters[$this->queue])); } if (method_exists($interpreter, $method)) { $info = ['exchange' => $msg->get('exchange'), 'queue' => $this->queue, 'routing_key' => $msg->get('routing_key'), 'reply_to' => $msg->has('reply_to') ? $msg->get('reply_to') : null]; try { $interpreter->{$method}(Json::decode($msg->body, true), $info); } catch (\Exception $exc) { $error_info = "consumer fail:" . $exc->getMessage() . PHP_EOL . "info:" . print_r($info, true) . PHP_EOL . "body:" . PHP_EOL . print_r($msg->body, true) . PHP_EOL . $exc->getTraceAsString(); \Yii::warning($error_info, __METHOD__); $format = [Console::FG_RED]; Console::stdout(Console::ansiFormat($error_info . PHP_EOL, $format)); Console::stdout(Console::ansiFormat($exc->getTraceAsString() . PHP_EOL, $format)); } } else { if (!isset($this->interpreters[$this->queue])) { $interpreter = new AmqpInterpreter(); } $error_info = "Unknown routing key '{$routingKey}' for exchange '{$this->queue}'."; $error_info .= PHP_EOL . $msg->body; \Yii::warning($error_info, __METHOD__); $interpreter->log(sprintf("Unknown routing key '%s' for exchange '%s'.", $routingKey, $this->queue), $interpreter::MESSAGE_ERROR); // debug the message $interpreter->log(print_r(Json::decode($msg->body, true), true), $interpreter::MESSAGE_INFO); } }
public function export() { foreach ($this->messages as $message) { $string = $this->formatMessage($message) . "\n"; $level = $message[1]; if ($level == Logger::LEVEL_INFO) { if (strncmp('BEGIN ', $message[0], 6) == 0) { $ansiColor = $this->_levelAnsiColorMap[Logger::LEVEL_PROFILE_BEGIN]; } elseif (strncmp('END ', $message[0], 4) == 0) { $ansiColor = $this->_levelAnsiColorMap[Logger::LEVEL_PROFILE_END]; } else { $ansiColor = $this->_levelAnsiColorMap[Logger::LEVEL_INFO]; } } elseif (array_key_exists($level, $this->_levelAnsiColorMap)) { $ansiColor = $this->_levelAnsiColorMap[$level]; } else { $ansiColor = $this->defaultAnsiColor; } if ($this->_stdoutIsTerminal) { if ($this->_stdoutSupportsAnsiColors && $ansiColor) { Console::stdout(Console::ansiFormat($string, $ansiColor)); } else { Console::stdout($string); } } else { Console::stdout($string); if ($this->_stderrIsTerminal && ($level == Logger::LEVEL_ERROR || $level == Logger::LEVEL_WARNING)) { if ($this->_stderrSupportsAnsiColors && $ansiColor) { Console::stderr(Console::ansiFormat($string, $ansiColor)); } else { Console::stderr($string); } } } } }
/** * Prints a string to STDOUT * @param string $string */ public function stdout($string) { if (Yii::$app->request->isConsoleRequest) { if (Console::streamSupportsAnsiColors(STDOUT)) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); } Console::stdout($string . "\n"); } }
/** * Logs info and error messages. * * @param $message * @param $type */ public static function log($message, $type = self::MESSAGE_INFO) { $format = [$type == self::MESSAGE_ERROR ? Console::FG_RED : Console::FG_BLUE]; Console::stdout(Console::ansiFormat($message . PHP_EOL, $format)); }
/** * Prints a string to STDOUT * * You may optionally format the string with ANSI codes by * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. * * Example: * * ~~~ * $this->stdout('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 stdout($string) { if (Yii::$app->id != 'app-console') { return false; } if ($this->isColorEnabled()) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); } return Console::stdout($string); }
/** * @param string $mode * @param string $path * @param bool $recursive * * @return bool */ public function chmod($mode, $path, $recursive = false) { switch ($this->_connType) { case SftpHelper::TYPE_SFTP: default: $res = $this->_connection->chmod($mode, $path, $recursive); break; case SftpHelper::TYPE_FTP: if ($recursive) { Console::stdout("\n"); Log::throwException('Recursive not supported for chmod in ftp mode'); } $res = @ftp_chmod($this->_connection, $mode, $path); break; } return $res; }
/** * Fix the code formatting throughout Cascade and its related libraries. */ public function actionFixProject() { $dirs = [Yii::getAlias('@cascade'), Yii::getAlias('@canis'), Yii::getAlias('@cascade/modules/core'), Yii::getAlias('@psesd/cascade'), Yii::getAlias('@canis/deferred'), Yii::getAlias('@canis/notification')]; $customStart = microtime(true); Console::stdout("Running custom fixes..." . PHP_EOL); foreach ($dirs as $dir) { $dirStart = microtime(true); $changed = 0; Console::stdout("\t" . $dir . "..."); $files = FileHelper::findFiles($dir, ['only' => ['*.php'], 'recursive' => true]); Console::stdout("found " . count($files) . " files..."); foreach ($files as $file) { if ($this->fixFile($file)) { $changed++; } } Console::stdout("changed {$changed} files in " . round(microtime(true) - $dirStart, 1) . "s!" . PHP_EOL); } Console::stdout("done in " . round(microtime(true) - $customStart, 1) . "s!" . PHP_EOL . PHP_EOL); $phpcsStart = microtime(true); Console::stdout("Running style fixes..." . PHP_EOL); foreach ($dirs as $dir) { $dirStart = microtime(true); $changed = 0; Console::stdout("\t" . $dir . "..."); $configFiles = []; $configFiles[] = $dir . DIRECTORY_SEPARATOR . '.php_cs'; $configFiles[] = dirname($dir) . DIRECTORY_SEPARATOR . '.php_cs'; $configFiles[] = dirname(dirname($dir)) . DIRECTORY_SEPARATOR . '.php_cs'; $foundConfig = false; foreach ($configFiles as $configFile) { if (file_exists($configFile)) { $foundConfig = $configFile; break; } } if (!$foundConfig) { Console::stdout("skipped!" . PHP_EOL); continue; } $phpcsBinary = Yii::getAlias('@vendor/bin/php-cs-fixer'); if (!file_exists($phpcsBinary)) { Console::stdout("no php-cs-fixer binary!" . PHP_EOL); continue; } $command = []; $command[] = PHP_BINARY; $command[] = $phpcsBinary; $command[] = '--no-interaction'; $command[] = '--config-file=' . $foundConfig; // $command[] = '--quiet'; $command[] = 'fix'; exec(implode(' ', $command), $output, $exitCode); Console::stdout("done in " . round(microtime(true) - $dirStart, 1) . "s!" . PHP_EOL); } Console::stdout("done in " . round(microtime(true) - $phpcsStart, 1) . "s!" . PHP_EOL . PHP_EOL); }
/** * Adding or deleting children if needed */ protected function manageRelations() { foreach ($this->_children as $p => $kids) { $parent = $this->_items[$p]; foreach ($kids as $k) { $kid = $this->_items[$k]; if (!$this->_auth->hasChild($parent, $kid)) { Console::stdout($kid->name . ' added as a child of ' . $parent->name . "\n"); $this->_auth->addChild($parent, $kid); } } foreach ($this->_auth->getChildren($p) as $current_kid) { if (!in_array($current_kid->name, $kids)) { Console::stdout(Console::ansiFormat('Relation between ' . $current_kid->name . ' and ' . $parent->name . " was removed!\n", [Console::FG_RED])); $this->_auth->removeChild($parent, $current_kid); } } } }
/** * Ends a cron job * @return boolean Success */ public function finish() { $this->running = 0; $this->success = 1; $this->last_execution_time = self::execution_time(); if ($this->save()) { Console::stdout(Console::ansiFormat("*** finished " . $this->controller . "/" . $this->action . " (time: " . sprintf("%.3f", $this->last_execution_time) . "s)\n\n", [Console::FG_GREEN])); return true; } else { Console::stdout(Console::ansiFormat("*** failed to finish " . $this->controller . "/" . $this->action . " (time: " . sprintf("%.3f", $this->last_execution_time) . "s)\n\n", [Console::FG_RED])); return false; } }
/** * For each version that is newer of the current DeploYii home version and that requires * changes, tells the user which changes need to be performed manually and if possible * updates it automatically. * * To update a file it is possible to amend it manually or to remove it; if the file is missing * it will be replaced with the original one from the home-dist folder. * * If some manual changes are required, it is possible to simply delete the VERSION file once * the changes have been applied. * * @param string $homeVersion The DeploYii home version * * @throws \yii\console\Exception if the home folder requires to be updated manually */ public static function checkHomeVersion($homeVersion) { $changeLog = ''; $requiredActions = ''; $home = Shell::getHomeDir(); $homeDist = __DIR__ . '/../home-dist'; $changes = array_reverse(self::$_homeChangeList); foreach ($changes as $version => $list) { if (version_compare($version, $homeVersion, '>')) { // files to be manually updated by the user: if (isset($list['mod'])) { foreach ($list['mod'] as $relFilePath => $logMessage) { // If the destination file does not exists, add it from the dist folder // instead of requesting the user to manually update it: $destFile = $home . DIRECTORY_SEPARATOR . $relFilePath; if (!file_exists($destFile)) { $list['add'][$relFilePath] = $logMessage; unset($list['mod'][$relFilePath]); } else { $requiredActions .= " - [{$version}] {$relFilePath}: {$logMessage}\n"; } } } // files/directories to be added: (if no manual actions are required) if (empty($requiredActions) && isset($list['add'])) { foreach ($list['add'] as $relPath => $logMessage) { $srcPath = $homeDist . DIRECTORY_SEPARATOR . $relPath; $destPath = $home . DIRECTORY_SEPARATOR . $relPath; if (!is_dir($srcPath) && !file_exists($destPath) && file_exists($srcPath)) { $changeLog .= " - [{$version}] Adding {$relPath} ({$logMessage})\n"; copy($srcPath, $destPath); } elseif (is_dir($srcPath) && !is_dir($destPath)) { $changeLog .= " - [{$version}] Adding directory: {$relPath} ({$logMessage})\n"; FileHelper::copyDirectory($srcPath, $destPath); } elseif (is_dir($srcPath) && is_dir($destPath)) { $requiredActions .= " - [{$version}] {$relPath}: {$logMessage}\n"; } } } // files/directories to be removed: (if no manual actions are required) if (empty($requiredActions) && isset($list['rem'])) { foreach ($list['rem'] as $relPath => $logMessage) { $destPath = $home . DIRECTORY_SEPARATOR . $relPath; if (!is_dir($destPath) && file_exists($destPath)) { $changeLog .= " - [{$version}] Removing {$relPath} ({$logMessage})\n"; unlink($destPath); } elseif (is_dir($destPath)) { $requiredActions .= " - [{$version}] {$relPath}: {$logMessage}\n"; } } } } } if (!empty($requiredActions)) { Log::throwException("Your DeploYii home folder needs to be manually updated to " . DEPLOYII_VERSION . ":\n" . $requiredActions . "When done delete the VERSION file and run DeploYii again.\n"); } elseif (!empty($changeLog)) { self::updateHomeVersion(); $message = "Your DeploYii home folder has been updated to: " . DEPLOYII_VERSION . ":\n" . $changeLog; Log::logger()->addInfo($message); Console::stdout("---------------------------------------------------------------\n" . $message . "---------------------------------------------------------------\n"); sleep(2); } }
/** * Prints a string to STDOUT * * You may optionally format the string with ANSI codes by * passing additional parameters using the constants defined in [[\yii\helpers\Console]]. * * Example: * * ~~~ * $this->stdout('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 stdout($string) { if ($this->_silentMode) { Yii::info($string, $this->getProcessName() . ' stdout'); return strlen($string); } else { Yii::info($string, $this->getProcessName() . ' say'); if ($this->_isColorEnabled()) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string . "\n", $args); } return Console::stdout($string); } }
private function begin($action) { Console::stdout(" > {$action} ..."); $this->beginTime = microtime(true); }