EventDispatcher.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\EventDispatcher;
  12. use Composer\IO\IOInterface;
  13. use Composer\Composer;
  14. use Composer\DependencyResolver\Operation\OperationInterface;
  15. use Composer\Script;
  16. use Composer\Script\CommandEvent;
  17. use Composer\Script\PackageEvent;
  18. use Composer\Util\ProcessExecutor;
  19. /**
  20. * The Event Dispatcher.
  21. *
  22. * Example in command:
  23. * $dispatcher = new EventDispatcher($this->getComposer(), $this->getApplication()->getIO());
  24. * // ...
  25. * $dispatcher->dispatch(ScriptEvents::POST_INSTALL_CMD);
  26. * // ...
  27. *
  28. * @author François Pluchino <francois.pluchino@opendisplay.com>
  29. * @author Jordi Boggiano <j.boggiano@seld.be>
  30. * @author Nils Adermann <naderman@naderman.de>
  31. */
  32. class EventDispatcher
  33. {
  34. protected $composer;
  35. protected $io;
  36. protected $loader;
  37. protected $process;
  38. /**
  39. * Constructor.
  40. *
  41. * @param Composer $composer The composer instance
  42. * @param IOInterface $io The IOInterface instance
  43. * @param ProcessExecutor $process
  44. */
  45. public function __construct(Composer $composer, IOInterface $io, ProcessExecutor $process = null)
  46. {
  47. $this->composer = $composer;
  48. $this->io = $io;
  49. $this->process = $process ?: new ProcessExecutor($io);
  50. }
  51. /**
  52. * Dispatch an event
  53. *
  54. * @param string $eventName An event name
  55. * @param Event $event
  56. */
  57. public function dispatch($eventName, Event $event = null)
  58. {
  59. if (null == $event) {
  60. $event = new Event($eventName);
  61. }
  62. $this->doDispatch($event);
  63. }
  64. /**
  65. * Dispatch a script event.
  66. *
  67. * @param string $eventName The constant in ScriptEvents
  68. * @param Event $event
  69. */
  70. public function dispatchScript($eventName, Script\Event $event = null)
  71. {
  72. if (null == $event) {
  73. $event = new Script\Event($eventName, $this->composer, $this->io);
  74. }
  75. $this->doDispatch($event);
  76. }
  77. /**
  78. * Dispatch a package event.
  79. *
  80. * @param string $eventName The constant in ScriptEvents
  81. * @param boolean $devMode Whether or not we are in dev mode
  82. * @param OperationInterface $operation The package being installed/updated/removed
  83. */
  84. public function dispatchPackageEvent($eventName, $devMode, OperationInterface $operation)
  85. {
  86. $this->doDispatch(new PackageEvent($eventName, $this->composer, $this->io, $devMode, $operation));
  87. }
  88. /**
  89. * Dispatch a command event.
  90. *
  91. * @param string $eventName The constant in ScriptEvents
  92. * @param boolean $devMode Whether or not we are in dev mode
  93. */
  94. public function dispatchCommandEvent($eventName, $devMode)
  95. {
  96. $this->doDispatch(new CommandEvent($eventName, $this->composer, $this->io, $devMode));
  97. }
  98. /**
  99. * Triggers the listeners of an event.
  100. *
  101. * @param Event $event The event object to pass to the event handlers/listeners.
  102. * @throws \RuntimeException
  103. * @throws \Exception
  104. */
  105. protected function doDispatch(Event $event)
  106. {
  107. $listeners = $this->getListeners($event);
  108. foreach ($listeners as $callable) {
  109. if ((is_array($callable) && $is_callable($callable)) || $callable instanceof Closure) {
  110. $callable($event);
  111. } elseif ($this->isPhpScript($callable)) {
  112. $className = substr($callable, 0, strpos($callable, '::'));
  113. $methodName = substr($callable, strpos($callable, '::') + 2);
  114. if (!class_exists($className)) {
  115. $this->io->write('<warning>Class '.$className.' is not autoloadable, can not call '.$event->getName().' script</warning>');
  116. continue;
  117. }
  118. if (!is_callable($callable)) {
  119. $this->io->write('<warning>Method '.$callable.' is not callable, can not call '.$event->getName().' script</warning>');
  120. continue;
  121. }
  122. try {
  123. $this->executeEventPhpScript($className, $methodName, $event);
  124. } catch (\Exception $e) {
  125. $message = "Script %s handling the %s event terminated with an exception";
  126. $this->io->write('<error>'.sprintf($message, $callable, $event->getName()).'</error>');
  127. throw $e;
  128. }
  129. } else {
  130. if (0 !== ($exitCode = $this->process->execute($callable))) {
  131. $event->getIO()->write(sprintf('<error>Script %s handling the %s event returned with an error</error>', $callable, $event->getName()));
  132. throw new \RuntimeException('Error Output: '.$this->process->getErrorOutput(), $exitCode);
  133. }
  134. }
  135. if ($event->isPropagationStopped()) {
  136. break;
  137. }
  138. }
  139. }
  140. /**
  141. * @param string $className
  142. * @param string $methodName
  143. * @param Event $event Event invoking the PHP callable
  144. */
  145. protected function executeEventPhpScript($className, $methodName, Event $event)
  146. {
  147. $className::$methodName($event);
  148. }
  149. protected function addListener($eventName, $listener, $priority = 0)
  150. {
  151. $this->listeners[$eventName][$priority][] = $listener;
  152. }
  153. public function addSubscriber(EventSubscriberInterface $subscriber)
  154. {
  155. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  156. if (is_string($params)) {
  157. $this->addListener($eventName, array($subscriber, $params));
  158. } elseif (is_string($params[0])) {
  159. $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
  160. } else {
  161. foreach ($params as $listener) {
  162. $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
  163. }
  164. }
  165. }
  166. }
  167. protected function getListeners(Event $event)
  168. {
  169. $scriptListeners = $this->getScriptListeners($event);
  170. if (!isset($this->listeners[$event->getName()][0])) {
  171. $this->listeners[$event->getName()][0] = array();
  172. }
  173. krsort($this->listeners[$event->getName()]);
  174. $listeners = $this->listeners;
  175. $listeners[$event->getName()][0] = array_merge($listeners[$event->getName()][0], $scriptListeners);
  176. return call_user_func_array('array_merge', $listeners[$event->getName()]);
  177. }
  178. /**
  179. * @param Event $event Event object
  180. * @return array Listeners
  181. */
  182. protected function getScriptListeners(Event $event)
  183. {
  184. $package = $this->composer->getPackage();
  185. $scripts = $package->getScripts();
  186. if (empty($scripts[$event->getName()])) {
  187. return array();
  188. }
  189. if ($this->loader) {
  190. $this->loader->unregister();
  191. }
  192. $generator = $this->composer->getAutoloadGenerator();
  193. $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages();
  194. $packageMap = $generator->buildPackageMap($this->composer->getInstallationManager(), $package, $packages);
  195. $map = $generator->parseAutoloads($packageMap, $package);
  196. $this->loader = $generator->createLoader($map);
  197. $this->loader->register();
  198. return $scripts[$event->getName()];
  199. }
  200. /**
  201. * Checks if string given references a class path and method
  202. *
  203. * @param string $callable
  204. * @return boolean
  205. */
  206. protected function isPhpScript($callable)
  207. {
  208. return false === strpos($callable, ' ') && false !== strpos($callable, '::');
  209. }
  210. }