Browse Source

Add CliController and iCli interface for cli applications.

master
Alexander Demidov 12 years ago
parent
commit
595c1b73eb
  1. 59
      app/CliController.php
  2. 18
      app/iCli.php

59
app/CliController.php

@ -0,0 +1,59 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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);
}
}
}

18
app/iCli.php

@ -0,0 +1,18 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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();
}
Loading…
Cancel
Save