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.

77 lines
1.9 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 $error_stream;
  21. protected function __construct()
  22. {
  23. ErrorHandler::init();
  24. $this->error_stream = Config::get('ErrorStream', 'php://stderr');
  25. }
  26. /**
  27. * Refuse cloning
  28. */
  29. private function __clone()
  30. {
  31. }
  32. /**
  33. * @static
  34. * @return CliController
  35. */
  36. public static function getInstance()
  37. {
  38. if (!isset(self::$instance)) {
  39. self::$instance = new self();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * @param iCli $cli_class
  45. * @throws ErrorException
  46. */
  47. public function execute($cli_class)
  48. {
  49. try {
  50. if (!in_array('iCli', class_implements($cli_class))) {
  51. throw new ErrorException('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.');
  52. }
  53. $cli_class->run();
  54. if (Config::get('PROFILER')) {
  55. $profile = Profiler::getInstance()->getCli();
  56. if (Config::get('LOGGING')) {
  57. Logger::getInstance()->log($profile);
  58. } else {
  59. echo $profile;
  60. }
  61. }
  62. }
  63. catch (Exception $e) {
  64. $code = $e->getCode();
  65. if ($e instanceof ErrorException) {
  66. $code = $e->getSeverity();
  67. }
  68. file_put_contents($this->error_stream, PHP_EOL . 'Error ' . '#' . $code . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
  69. file_put_contents($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, FILE_APPEND);
  70. }
  71. }
  72. }