You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage App
  7. * @since 27.06.12
  8. *
  9. */
  10. /**
  11. * @desc CliController (run cli_class, end profiler)
  12. * @author Aleksandr Demidov
  13. */
  14. class CliController
  15. {
  16. /**
  17. * @var CliController
  18. */
  19. protected static $instance;
  20. protected function __construct()
  21. {
  22. ErrorHandler::init();
  23. }
  24. /**
  25. * @static
  26. * @return CliController
  27. */
  28. public static function getInstance()
  29. {
  30. if (!isset(self::$instance)) {
  31. self::$instance = new self();
  32. }
  33. return self::$instance;
  34. }
  35. /**
  36. * @param iCli $cli_class
  37. * @throws ErrorException
  38. */
  39. public function execute($cli_class)
  40. {
  41. try {
  42. if (!in_array('iCli', class_implements($cli_class))) {
  43. throw new ErrorException('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.');
  44. }
  45. $cli_class->run();
  46. if (Config::get('PROFILER')) {
  47. echo PHP_EOL . Profiler::getInstance()->getCli();
  48. }
  49. } catch (Exception $e) {
  50. $stderr = fopen('php://stderr', 'w');
  51. fputs($stderr, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL);
  52. fputs($stderr, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
  53. }
  54. }
  55. }