PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Cli/SlaveRequestHandler.php

https://github.com/christianjul/FLOW3-Composer
PHP | 138 lines | 66 code | 18 blank | 54 comment | 6 complexity | 7b89e7997ae03677b2231f1d25730d45 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Cli;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. use TYPO3\FLOW3\Core\Bootstrap;
  14. /**
  15. * A special request handler which handles "slave" command requests as used by
  16. * the interactive shell.
  17. *
  18. * @FLOW3\Proxy(false)
  19. * @FLOW3\Scope("singleton")
  20. */
  21. class SlaveRequestHandler implements \TYPO3\FLOW3\Core\RequestHandlerInterface {
  22. /**
  23. * @var \TYPO3\FLOW3\Core\Bootstrap
  24. */
  25. protected $bootstrap;
  26. /**
  27. * Constructor
  28. *
  29. * @param \TYPO3\FLOW3\Core\Bootstrap $bootstrap
  30. */
  31. public function __construct(Bootstrap $bootstrap) {
  32. $this->bootstrap = $bootstrap;
  33. }
  34. /**
  35. * This request handler can handle CLI requests.
  36. *
  37. * @return boolean If the request is a CLI request, TRUE otherwise FALSE
  38. */
  39. public function canHandleRequest() {
  40. return (PHP_SAPI === 'cli' && isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] === '--start-slave');
  41. }
  42. /**
  43. * Returns the priority - how eager the handler is to actually handle the
  44. * request.
  45. *
  46. * @return integer The priority of the request handler.
  47. */
  48. public function getPriority() {
  49. return 200;
  50. }
  51. /**
  52. * Creates an event loop which takes orders from the parent process and executes
  53. * them in runtime mode.
  54. *
  55. * @return void
  56. */
  57. public function handleRequest() {
  58. $sequence = $this->bootstrap->buildRuntimeSequence();
  59. $sequence->invoke($this->bootstrap);
  60. $objectManager = $this->bootstrap->getObjectManager();
  61. $systemLogger = $objectManager->get('TYPO3\FLOW3\Log\SystemLoggerInterface');
  62. $systemLogger->log('Running sub process loop.', LOG_DEBUG);
  63. echo "\nREADY\n";
  64. try {
  65. while (TRUE) {
  66. $commandLine = trim(fgets(STDIN));
  67. $systemLogger->log(sprintf('Received command "%s".', $commandLine), LOG_INFO);
  68. if ($commandLine === "QUIT\n") {
  69. break;
  70. }
  71. $request = $objectManager->get('TYPO3\FLOW3\Cli\RequestBuilder')->build($commandLine);
  72. $response = new \TYPO3\FLOW3\Cli\Response();
  73. if ($this->bootstrap->isCompiletimeCommand($request->getCommand()->getCommandIdentifier())) {
  74. echo "This command must be executed during compiletime.\n";
  75. } else {
  76. $objectManager->get('TYPO3\FLOW3\Mvc\Dispatcher')->dispatch($request, $response);
  77. $response->send();
  78. $this->emitDispatchedCommandLineSlaveRequest();
  79. }
  80. echo "\nREADY\n";
  81. }
  82. $systemLogger->log('Exiting sub process loop.', LOG_DEBUG);
  83. $this->bootstrap->shutdown('Runtime');
  84. exit($response->getExitCode());
  85. } catch (\Exception $exception) {
  86. $this->handleException($exception);
  87. }
  88. }
  89. /**
  90. * Emits a signal that a CLI slave request was dispatched.
  91. *
  92. * @return void
  93. * @FLOW3\Signal
  94. */
  95. protected function emitDispatchedCommandLineSlaveRequest() {
  96. $this->bootstrap->getSignalSlotDispatcher()->dispatch(__CLASS__, 'dispatchedCommandLineSlaveRequest', array());
  97. }
  98. /**
  99. * Displays a human readable, partly beautified version of the given exception
  100. * and stops the application, return a non-zero exit code.
  101. *
  102. * @param \Exception $exception
  103. * @return void
  104. */
  105. protected function handleException(\Exception $exception) {
  106. $response = new \TYPO3\FLOW3\Cli\Response();
  107. $exceptionMessage = '';
  108. $exceptionReference = "\n<b>More Information</b>\n";
  109. $exceptionReference .= " Exception code #" . $exception->getCode() . "\n";
  110. $exceptionReference .= " File " . $exception->getFile() . ($exception->getLine() ? ' line ' . $exception->getLine() : '') . "\n";
  111. $exceptionReference .= ($exception instanceof \TYPO3\FLOW3\Exception ? " Exception reference #" . $exception->getReferenceCode() . "\n" : '');
  112. foreach (explode(chr(10), wordwrap($exception->getMessage(), 73)) as $messageLine) {
  113. $exceptionMessage .= " $messageLine\n";
  114. }
  115. $response->setContent(sprintf("<b>Uncaught Exception</b>\n%s%s\n", $exceptionMessage, $exceptionReference));
  116. $response->send();
  117. exit(1);
  118. }
  119. }
  120. ?>