From 740bd6b7f3909ac546f0dcc0653b6a71697d9b46 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Wed, 5 Mar 2014 19:11:11 +0400 Subject: [PATCH] Modify CliController for opportunity to extend this class. --- app/CliController.php | 58 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/app/CliController.php b/app/CliController.php index a7728b7..d36a15b 100644 --- a/app/CliController.php +++ b/app/CliController.php @@ -46,7 +46,7 @@ class CliController public static function getInstance() { if (!isset(self::$instance)) { - self::$instance = new self(); + self::$instance = new static(); } return self::$instance; } @@ -58,32 +58,42 @@ class CliController public function execute($cli) { try { - if (is_string($cli)) { - if (!class_exists($cli)) { - throw new GeneralException('Action class "' . $cli . '" not found.'); - } - $cli = new $cli; - } - if (!in_array('iCli', class_implements($cli))) { - throw new ErrorException('Runner "' . get_class($cli) . '" need implement of "iCli" interface.'); - } - $cli->run(); - if (Config::get('PROFILER')) { - $profile = Profiler::getInstance()->getCli(); - if (Config::get('LOGGING')) { - Logger::getInstance()->log($profile); - } else { - echo $profile; - } - } + $this->run($cli); } catch (Exception $e) { - $code = $e->getCode(); - if ($e instanceof ErrorException) { - $code = $e->getSeverity(); + $this->exception($e); + } + } + + protected function run($cli) + { + if (is_string($cli)) { + if (!class_exists($cli)) { + throw new GeneralException('Action class "' . $cli . '" not found.'); } - file_put_contents($this->error_stream, PHP_EOL . 'Error ' . '#' . $code . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND); - file_put_contents($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, FILE_APPEND); + $cli = new $cli; + } + if (!in_array('iCli', class_implements($cli))) { + throw new ErrorException('Runner "' . get_class($cli) . '" need implement of "iCli" interface.'); + } + $cli->run(); + if (Config::get('PROFILER')) { + $profile = Profiler::getInstance()->getCli(); + if (Config::get('LOGGING')) { + Logger::getInstance()->log($profile); + } else { + echo $profile; + } + } + } + + protected function exception($e) + { + $code = $e->getCode(); + if ($e instanceof ErrorException) { + $code = $e->getSeverity(); } + file_put_contents($this->error_stream, PHP_EOL . 'Error ' . '#' . $code . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND); + file_put_contents($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, FILE_APPEND); } } \ No newline at end of file