Text can be colorized (foreground only) by using a static method named after the color code. $text = Colors::colorize('hello world', Colors::RED) $text = Colors::colorize('hello world', 'red') $text = Colors::colorize('hello world', Colors::RED | Colors::BOLD) $text = Colors::colorize('hello world', 'red+bold') $text = Colors::red('hello world');
Example #1
0
 /**
  * Show a list of available options
  */
 protected function showOptions()
 {
     $this->writeln("Options:", Colors::YELLOW);
     $rows = array(array(Colors::colorize('--version', Colors::GREEN), "Show version number"), array(Colors::colorize('--working-dir=DIR', Colors::GREEN), "If specified, use the given directory as working directory"), array(Colors::colorize('--config=FILE', Colors::GREEN), "Use an alternative config file"));
     $formater = new TextFormater(array('indent' => 2));
     $table = new Table(null, $rows, array('border' => false, 'frame' => false));
     $this->writeln($formater->format($table->render()));
 }
 /**
  * Show available options and commands
  */
 protected function showCommands()
 {
     $formater = new TextFormater(array('indent' => 2));
     $this->writeln('Available commands:', Colors::YELLOW);
     $rows = [];
     foreach ($this->console->getCommands() as $name => $fqdn) {
         $help = Help::fromFQDN($fqdn);
         $rows[] = array(Colors::colorize($name, Colors::GREEN), $help->getShortDescription());
     }
     $table = new Widgets\Table(null, $rows, array('border' => false, 'frame' => false));
     $this->writeln($formater->format($table->render()));
 }
Example #3
0
 public function testExecuteFunction()
 {
     $this->expectOutputString("hello foobar!\n");
     $this->console->addCommand(function ($args, $opts, $console) {
         $console->writeln(Colors::colorize(sprintf("hello %s!", $args[0]), $opts['color']));
     }, 'test');
     $this->console->addCommand('test2', function ($args, $opts, $console) {
         return "success";
     });
     $this->console->execute('test', array('foobar'), array('color' => 'red'));
     $this->assertEquals("success", $this->console->execute('test2'));
 }
Example #4
0
 public function execute(array $args, array $options = array())
 {
     $return = array();
     # get site name
     $site_name = \Bitrix\Main\Config\Option::get("main", "site_name");
     # get site name
     $return[] = Colors::colorize('Site:', Colors::YELLOW) . " " . $site_name;
     # get bitrix version
     $MESS = array();
     include_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/lang/ru/interface/epilog_main_admin.php";
     $vendor = COption::GetOptionString("main", "vendor", "1c_bitrix");
     $info_text = $MESS["EPILOG_ADMIN_SM_" . $vendor] . " (" . SM_VERSION . ")";
     $return[] = Colors::colorize('Version:', Colors::YELLOW) . " " . $info_text;
     $url = "https://packagist.org/search.json?q=bim";
     $json = file_get_contents($url);
     $data = json_decode($json);
     $dataPack = null;
     foreach ($data->results as $item) {
         if ($item->name == "cjp2600/bim-core") {
             $dataPack = $item;
         }
     }
     if (is_null($dataPack)) {
         $info_text = PHP_EOL . 'Bitrix migration (BIM) ' . PHP_EOL . 'http://cjp2600.github.io/bim-core' . PHP_EOL;
     } else {
         $info_text = PHP_EOL . 'Bitrix migration (BIM)' . PHP_EOL;
         foreach ((array) $dataPack as $key => $val) {
             $info_text .= Colors::colorize(ucfirst($key), Colors::YELLOW) . ": " . $val . PHP_EOL;
         }
         $info_text .= PHP_EOL;
     }
     # edition
     $return[] = Colors::colorize('Edition:', Colors::YELLOW) . " " . $this->checkRedaction();
     $this->info("About bitrix project:");
     # display
     $this->padding(implode(PHP_EOL, $return));
     $this->info("About bim:");
     # for fun :)
     $this->padding($info_text);
 }
Example #5
0
 public function execute(array $args, array $options = array())
 {
     # setUserMigration Path
     $this->migrationPath = isset($options['migration_path']) ? $options['migration_path'] : null;
     $list = $this->getDirectoryTree($this->getMigrationPath(), "php");
     # get filename
     $file = isset($options['f']) ? true : false;
     $filter_apply = isset($options['a']) ? $options['a'] : false;
     $filter_new = isset($options['n']) ? $options['n'] : false;
     $filter_from = isset($options['from']) ? $options['from'] : false;
     $filter_to = isset($options['to']) ? $options['to'] : false;
     $filter_from = $filter_from ? strtotime($filter_from) : false;
     $filter_to = $filter_to ? strtotime($filter_to) : false;
     #check tag list
     $filer_tag = isset($options['tag']) ? $options['tag'] : false;
     if (!empty($list)) {
         $headers = array('№', 'id', 'Author', 'Date');
         if ($file) {
             $headers[] = 'File';
         }
         $headers[] = 'Description';
         $headers[] = 'Status';
         $table = new \cli\Table();
         $table->setHeaders($headers);
         $count = 0;
         $applied = 0;
         $new = 0;
         $i = 1;
         $return_array_new = array();
         $return_array_apply = array();
         #filter
         $is_filter = false;
         $this->prepareFilter($list, $filter_from, $filter_to, $filer_tag, $options, $is_filter);
         foreach ($list as $id => $data) {
             $count++;
             $row = $data['file'];
             $name = $data['name'];
             # check in db
             $is_new = !$this->checkInDb($id);
             $class_name = "Migration" . $id;
             include_once "" . $this->getMigrationPath() . $row . "";
             $color = ConsoleKit\Colors::GREEN;
             $status = ConsoleKit\Colors::colorize('apply', Colors::GREEN);
             # check in db
             if ($is_new) {
                 $new++;
                 $color = ConsoleKit\Colors::RED;
                 $status = ConsoleKit\Colors::colorize('new', Colors::RED);
             } else {
                 $applied++;
             }
             $rowArray = array(ConsoleKit\Colors::colorize($i, $color), ConsoleKit\Colors::colorize($id, $color), $data['author'], date("d.m.y G:h", $data['date']));
             if ($file) {
                 $rowArray[] = $row;
             }
             $rowArray[] = $data['description'];
             $rowArray[] = $status;
             if ($is_new) {
                 $return_array_new[] = $rowArray;
             } else {
                 $return_array_apply[] = $rowArray;
             }
             $i++;
         }
         if ($filter_new) {
             $table->setRows($return_array_new);
         } else {
             if ($filter_apply) {
                 $table->setRows($return_array_apply);
             } else {
                 $table->setRows(array_merge($return_array_apply, $return_array_new));
             }
         }
         $displayArray = $table->getDisplayLines();
         if (!empty($displayArray)) {
             $table->display();
         }
         if (!$is_filter) {
             # count info
             $return[] = Colors::colorize('New:', Colors::RED) . " " . $new;
             $return[] = Colors::colorize('Applied:', Colors::GREEN) . " " . $applied;
             $return[] = "Count: " . $count;
         }
         # display
         $this->padding(implode(PHP_EOL, $return));
     } else {
         $this->info('Empty migration');
     }
 }
Example #6
0
 /**
  * Writes an error message to stderr
  *
  * @param \Exception $e
  * @return Console
  */
 public function writeException(\Exception $e)
 {
     if ($this->verboseException) {
         $text = sprintf("[%s]\n%s\nIn %s at line %s\n%s", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
     } else {
         $text = sprintf("\n[%s]\n%s\n", get_class($e), $e->getMessage());
     }
     $box = new Widgets\Box($this->textWriter, $text, '');
     $out = Colors::colorizeLines($box, Colors::WHITE, Colors::RED);
     $out = TextFormater::apply($out, array('indent' => 2));
     $this->textWriter->writeln($out);
     return $this;
 }
Example #7
0
 /**
  * @return string
  */
 public function render()
 {
     $output = "{$this->description}\n\n";
     if (!empty($this->usage)) {
         $output .= "Usage: {$this->usage}\n\n";
     }
     if (!empty($this->args)) {
         $output .= Colors::colorize("Arguments:\n", Colors::BLACK | Colors::BOLD);
         foreach ($this->args as $name => $desc) {
             $output .= sprintf("  %s\t%s\n", $name, $desc);
         }
         $output .= "\n";
     }
     if (!empty($this->options)) {
         $output .= Colors::colorize("Available options:\n", Colors::BLACK | Colors::BOLD);
         foreach ($this->options as $name => $desc) {
             $output .= sprintf("  --%s\t%s\n", $name, $desc);
         }
         $output .= "\n";
     }
     if (!empty($this->flags)) {
         $output .= Colors::colorize("Available flags:\n", Colors::BLACK | Colors::BOLD);
         foreach ($this->flags as $name => $desc) {
             $output .= sprintf("  -%s\t%s\n", $name, $desc);
         }
         $output .= "\n";
     }
     if (!empty($this->subCommands)) {
         $output .= Colors::colorize("Available sub commands:\n", Colors::BLACK | Colors::BOLD);
         foreach ($this->subCommands as $name) {
             $output .= " - {$name}\n";
         }
         $output .= "\n";
     }
     return trim($output, "\n ");
 }
Example #8
0
 public function step($message, $callback, $maxMessageLength = null)
 {
     $maxMessageLength = $maxMessageLength ?: $this->maxMessageLength;
     $this->textWriter->write(sprintf("%-{$maxMessageLength}s", $message));
     if (call_user_func($callback)) {
         $this->textWriter->write(Colors::colorize($this->successText, $this->successColor));
     } else {
         $this->textWriter->write(Colors::colorize($this->errorText, $this->errorColor));
     }
     $this->textWriter->write("\n");
 }
Example #9
0
 /**
  * green
  * @param $text
  * @return string
  */
 public function color($text, $color)
 {
     return Colors::colorize($text, $color);
 }
Example #10
0
 /**
  * Show a lists with all updates and their status
  */
 public function showComplete()
 {
     $updates = $this->dbvc()->getUpdatesAvailable();
     if (empty($updates)) {
         if ($this->verbosity) {
             $this->writeln("No updates available");
         }
         return;
     }
     $done = $this->dbvc()->getUpdatesDone();
     $prefix = "";
     if ($this->verbosity) {
         $this->writeln("Available updates:", Colors::YELLOW);
     }
     if ($this->short) {
         $prefix = $this->verbosity ? "  " : '';
         foreach ($updates as $update) {
             $status = !in_array($update, $done) ? 'o' : 'x';
             $this->writeln($prefix . $status . ' ' . $update);
         }
     } else {
         $rows = array();
         foreach ($updates as $update) {
             $status = !in_array($update, $done) ? Colors::colorize('open', Colors::YELLOW) : Colors::colorize('done', Colors::GREEN);
             $rows[] = array($update, $status);
         }
         $formater = new \ConsoleKit\TextFormater(array('indent' => $this->verbosity ? 2 : 0));
         $table = new Table(null, $rows, array('border' => false, 'frame' => false));
         $this->writeln($formater->format($table->render()));
     }
 }
Example #11
0
 public function execute(array $args, array $options = array())
 {
     global $DB;
     # get logging options
     $logging = isset($options['logging']) ? true : false;
     $logging_output = array();
     # setUserMigration Path
     $this->migrationPath = isset($options['migration_path']) ? $options['migration_path'] : null;
     $list = $this->getDirectoryTree($this->getMigrationPath(), "php");
     krsort($list);
     #по убыванию
     if (!empty($list)) {
         foreach ($list as $id => $data) {
             $row = $data['file'];
             $name = $data['name'];
             # check in db
             $is_new = !$this->checkInDb($id);
             $class_name = "Migration" . $id;
             if ($is_new) {
                 $return_array_new[$id] = array($class_name, "" . $this->getMigrationPath() . $row . "", $name, $data['tags']);
             } else {
                 $return_array_apply[$id] = array($class_name, "" . $this->getMigrationPath() . $row . "", $name, $data['tags']);
             }
         }
         # filer
         $is_filter = false;
         $f_id = false;
         if (isset($options['id'])) {
             if (is_string($options['id'])) {
                 $f_id = $options['id'];
             } else {
                 $dialog = new \ConsoleKit\Widgets\Dialog($this->console);
                 $f_id = $dialog->ask('Type migration id:', $f_id);
             }
         } else {
             if (isset($args[0])) {
                 if (is_string($args[0])) {
                     $f_id = $args[0];
                 }
             }
         }
         #check tag list
         $filer_tag = isset($options['tag']) ? $options['tag'] : false;
         if ($f_id) {
             if (isset($return_array_apply[$f_id])) {
                 $is_filter = true;
                 $return_array_apply = array($f_id => $return_array_apply[$f_id]);
             } else {
                 if (isset($return_array_apply[$f_id])) {
                     $logging_output[] = "Migration " . $f_id . " - is already applied";
                     throw new Exception("Migration " . $f_id . " - is already applied");
                 } else {
                     $logging_output[] = "Migration " . $f_id . " - is not found in applied list";
                     throw new Exception("Migration " . $f_id . " - is not found in applied list");
                 }
             }
         }
         # check to tag list
         if ($filer_tag) {
             $this->padding("down migration for tag : " . $filer_tag);
             $newArrayList = array();
             foreach ($return_array_apply as $id => $mig) {
                 if (!empty($mig[3])) {
                     if (in_array(strtolower($filer_tag), $mig[3])) {
                         $newArrayList[$id] = $mig;
                     }
                 }
             }
             if (!empty($newArrayList)) {
                 $is_filter = true;
                 $return_array_apply = $newArrayList;
             } else {
                 $return_array_apply = array();
             }
         }
         if (!$is_filter) {
             $this->askDoOperation(isset($options['force']), "Are you sure you want to remove all applied migration");
         }
         if (empty($return_array_apply)) {
             $logging_output[] = "Applied migrations list is empty.";
             $this->info("Applied migrations list is empty.");
             if ($logging) {
                 $this->logging($logging_output, "down");
             }
             return false;
         }
         $time_start = microtime(true);
         $this->info(" <- Start revert migration:");
         $this->writeln('');
         foreach ($return_array_apply as $id => $mig) {
             include_once "" . $mig[1] . "";
             if (method_exists($mig[0], "down")) {
                 try {
                     # start transaction
                     $DB->StartTransaction();
                     if (false !== $mig[0]::down()) {
                         if (Bim\Db\Entity\MigrationsTable::isExistsInTable($id)) {
                             if (Bim\Db\Entity\MigrationsTable::delete($id)) {
                                 # commit transaction
                                 $DB->Commit();
                                 $this->writeln($this->color("     - revert   : " . $mig[2], Colors::GREEN));
                                 $logging_output[] = "revert   : " . $mig[2];
                             } else {
                                 # rollback transaction
                                 $DB->Rollback();
                                 $logging_output[] = "error   : " . $mig[2] . " - Error delete in migration table";
                                 throw new Exception("Error delete in migration table");
                             }
                         }
                     } else {
                         $this->writeln(Colors::colorize("     - error : " . $mig[2], Colors::RED) . " " . Colors::colorize("(Method Down return false)", Colors::YELLOW));
                         $logging_output[] = "error : " . $mig[2] . " - Method Down return false";
                     }
                 } catch (Exception $e) {
                     if (isset($options['debug'])) {
                         $debug = "[" . $e->getFile() . ">" . $e->getLine() . "] ";
                     } else {
                         $debug = "";
                     }
                     # rollback transaction
                     $DB->Rollback();
                     $this->writeln(Colors::colorize("     - error : " . $mig[2], Colors::RED) . " " . Colors::colorize("( " . $debug . "" . $e->getMessage() . " )", Colors::YELLOW));
                     $logging_output[] = "error : " . $mig[2] . " " . $debug . $e->getMessage();
                 }
             }
         }
         $time_end = microtime(true);
         $time = $time_end - $time_start;
         $this->writeln('');
         $this->info(" <- " . round($time, 2) . "s");
         $logging_output[] = "End time - " . round($time, 2) . "s";
         if ($logging) {
             $this->logging($logging_output, "down");
         }
     } else {
         $this->info('Empty migration');
     }
 }
Example #12
0
 /**
  * Render examples
  * 
  * @return string
  */
 protected function renderExamples()
 {
     if (empty($this->examples)) {
         return null;
     }
     $output = Colors::colorize("Examples:\n", Colors::YELLOW);
     foreach ($this->examples as $example) {
         $output .= "  {$example['desc']}:\n" . Colors::colorize("    {$example['code']}", Colors::GREEN) . "\n\n";
     }
     return $output;
 }
Example #13
0
 public function testGetBgColorString()
 {
     $this->assertEquals("", Colors::getBgColorString(Colors::MAGENTA));
 }