From 595c1b73ebb1f6e2289c4519cd037783cd06ba0a Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 19:37:03 +0400 Subject: [PATCH] Add CliController and iCli interface for cli applications. --- app/CliController.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ app/iCli.php | 18 ++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 app/CliController.php create mode 100644 app/iCli.php diff --git a/app/CliController.php b/app/CliController.php new file mode 100644 index 0000000..89f5e5d --- /dev/null +++ b/app/CliController.php @@ -0,0 +1,59 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage App + * @since 27.06.12 + * + */ + +/** + * @desc CliController (run cli_class, end profiler) + * @author Aleksandr Demidov + */ +class CliController +{ + /** + * @var CliController + */ + protected static $instance; + + protected function __construct() + { + ErrorHandler::init(); + } + + /** + * @static + * @return CliController + */ + public static function getInstance() + { + if (!isset(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * @param iCli $cli_class + * @throws ErrorException + */ + public function execute($cli_class) + { + try { + if (!in_array('iCli', class_implements($cli_class))) { + throw new ErrorException('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.'); + } + $cli_class->run(); + if (Config::get('PROFILER')) { + echo PHP_EOL . Profiler::getInstance()->getCli(); + } + } catch (Exception $e) { + $stderr = fopen('php://stderr', 'w'); + fputs($stderr, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL); + fputs($stderr, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL); + } + } +} \ No newline at end of file diff --git a/app/iCli.php b/app/iCli.php new file mode 100644 index 0000000..82b9644 --- /dev/null +++ b/app/iCli.php @@ -0,0 +1,18 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage App + * @since 10.07.12 + * + */ + +/** + * @desc Starter cli point need implement iCli + * @author Aleksandr Demidov + */ +interface iCli +{ + public function run(); +} \ No newline at end of file