public static function write($text = '') { if (is_array($text)) { foreach ($text as $line) { CLI_Helper::write($line); } } else { fwrite(STDOUT, $text . PHP_EOL); } }
public static function factory($options) { if (isset($options[0])) { $task = $options[0]; } else { $task = ''; } $task = trim($task); $class = 'Task_' . ucfirst(strtolower($task)); if (!class_exists($class)) { CLI_Helper::write('Task "' . $class . '" not found'); exit(1); } $class = new $class(); if (!$class instanceof CLI_Task) { CLI_Helper::write('Task "' . $class . '" not found'); exit(1); } $class->set_options($options); return $class; }
<?php define('SYS', 1); require __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'System.php'; spl_autoload_register(array('System', 'auto_load')); require __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; if (PHP_SAPI == 'cli') { CLI_Task::factory(CLI_Helper::options())->execute(); } else { die('Only CLI support'); }
protected function _execute(array $params) { //var_dump($params); if (array_key_exists('without-documents', $params)) { $sel_type = 'without-documents'; } elseif (array_key_exists('with-documents', $params)) { $sel_type = 'with-documents'; } else { CLI_Helper::write('Specify selection type (--without-documents or --with-documents)'); exit(1); } $format = 'Y-m-d'; $i = 0; // 3 попытки ввода корректного значения while ($i < 3) { $start_d = CLI_Helper::read('Please enter start date'); $sd = DateTime::createFromFormat($format, $start_d); if ($sd && $sd->format($format) == $start_d) { break; } elseif ($i == 2) { exit(1); } $i++; } $i = 0; while ($i < 3) { $end_d = CLI_Helper::read('Please enter end date'); $sd = DateTime::createFromFormat($format, $end_d); if ($sd && $sd->format($format) == $end_d) { break; } elseif ($i == 2) { exit(1); } $i++; } $sql = ''; if ($sel_type == 'without-documents') { $sql .= ' SELECT COUNT(`p`.`id`) as `count`, SUM(`p`.`amount`) as `amount` FROM `payments` as `p` LEFT JOIN `documents` as `d` ON `p`.`id`=`d`.`entity_id` WHERE `d`.`entity_id` is null AND DATE(`p`.`create_ts`) >= :start AND DATE(`p`.`create_ts`) < :end GROUP BY `p`.`id` '; } if ($sel_type == 'with-documents') { $sql .= ' SELECT COUNT(`d`.`entity_id`) as `count`, SUM(`p`.`amount`) as `amount` FROM `payments` as `p` JOIN `documents` as `d` ON `p`.`id`=`d`.`entity_id` WHERE DATE(`p`.`create_ts`) >= :start AND DATE(`p`.`create_ts`) < :end GROUP BY `d`.`entity_id` '; } try { $quest = new Quest(); $db = $quest->getDb(); $st = $db->prepare($sql); $st->bindParam(':start', $start_d); $st->bindParam(':end', $end_d); $st->setFetchMode(PDO::FETCH_OBJ); $st->execute(); } catch (PDOException $e) { CLI_Helper::write($e->getMessage()); exit(1); } CLI_Helper::write('count | amount'); foreach ($st as $row) { CLI_Helper::write($row->count . ' | ' . $row->amount); } }