AbstractBinary.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /*
  3. * This file is part of Alchemy\BinaryDriver.
  4. *
  5. * (c) Alchemy <info@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Alchemy\BinaryDriver;
  11. use Alchemy\BinaryDriver\Exception\ExecutableNotFoundException;
  12. use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
  13. use Alchemy\BinaryDriver\Listeners\Listeners;
  14. use Alchemy\BinaryDriver\Listeners\ListenerInterface;
  15. use Evenement\EventEmitter;
  16. use Monolog\Logger;
  17. use Monolog\Handler\NullHandler;
  18. use Psr\Log\LoggerInterface;
  19. use Symfony\Component\Process\ExecutableFinder;
  20. use Symfony\Component\Process\Process;
  21. abstract class AbstractBinary extends EventEmitter implements BinaryInterface
  22. {
  23. /** @var ConfigurationInterface */
  24. protected $configuration;
  25. /** @var ProcessBuilderFactoryInterface */
  26. protected $factory;
  27. /** @var ProcessRunner */
  28. private $processRunner;
  29. /** @var Listeners */
  30. private $listenersManager;
  31. public function __construct(ProcessBuilderFactoryInterface $factory, LoggerInterface $logger, ConfigurationInterface $configuration)
  32. {
  33. $this->factory = $factory;
  34. $this->configuration = $configuration;
  35. $this->processRunner = new ProcessRunner($logger, $this->getName());
  36. $this->listenersManager = new Listeners();
  37. $this->applyProcessConfiguration();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function listen(ListenerInterface $listener)
  43. {
  44. $this->listenersManager->register($listener, $this);
  45. return $this;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function unlisten(ListenerInterface $listener)
  51. {
  52. $this->listenersManager->unregister($listener, $this);
  53. return $this;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getConfiguration()
  59. {
  60. return $this->configuration;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. *
  65. * @return BinaryInterface
  66. */
  67. public function setConfiguration(ConfigurationInterface $configuration)
  68. {
  69. $this->configuration = $configuration;
  70. $this->applyProcessConfiguration();
  71. return $this;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getProcessBuilderFactory()
  77. {
  78. return $this->factory;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. *
  83. * @return BinaryInterface
  84. */
  85. public function setProcessBuilderFactory(ProcessBuilderFactoryInterface $factory)
  86. {
  87. $this->factory = $factory;
  88. $this->applyProcessConfiguration();
  89. return $this;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getProcessRunner()
  95. {
  96. return $this->processRunner;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function setProcessRunner(ProcessRunnerInterface $runner)
  102. {
  103. $this->processRunner = $runner;
  104. return $this;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function command($command, $bypassErrors = false, $listeners = null)
  110. {
  111. if (!is_array($command)) {
  112. $command = array($command);
  113. }
  114. return $this->run($this->factory->create($command), $bypassErrors, $listeners);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public static function load($binaries, LoggerInterface $logger = null, $configuration = array())
  120. {
  121. $finder = new ExecutableFinder();
  122. $binary = null;
  123. $binaries = is_array($binaries) ? $binaries : array($binaries);
  124. foreach ($binaries as $candidate) {
  125. if (file_exists($candidate) && is_executable($candidate)) {
  126. $binary = $candidate;
  127. break;
  128. }
  129. if (null !== $binary = $finder->find($candidate)) {
  130. break;
  131. }
  132. }
  133. if (null === $binary) {
  134. throw new ExecutableNotFoundException(sprintf(
  135. 'Executable not found, proposed : %s', implode(', ', $binaries)
  136. ));
  137. }
  138. if (null === $logger) {
  139. $logger = new Logger(__NAMESPACE__ . ' logger');
  140. $logger->pushHandler(new NullHandler());
  141. }
  142. $configuration = $configuration instanceof ConfigurationInterface ? $configuration : new Configuration($configuration);
  143. return new static(new ProcessBuilderFactory($binary), $logger, $configuration);
  144. }
  145. /**
  146. * Returns the name of the driver
  147. *
  148. * @return string
  149. */
  150. abstract public function getName();
  151. /**
  152. * Executes a process, logs events
  153. *
  154. * @param Process $process
  155. * @param Boolean $bypassErrors Set to true to disable throwing ExecutionFailureExceptions
  156. * @param ListenerInterface|array $listeners A listener or an array of listener to register for this unique run
  157. *
  158. * @return string The Process output
  159. *
  160. * @throws ExecutionFailureException in case of process failure.
  161. */
  162. protected function run(Process $process, $bypassErrors = false, $listeners = null)
  163. {
  164. if (null !== $listeners) {
  165. if (!is_array($listeners)) {
  166. $listeners = array($listeners);
  167. }
  168. $listenersManager = clone $this->listenersManager;
  169. foreach ($listeners as $listener) {
  170. $listenersManager->register($listener, $this);
  171. }
  172. } else {
  173. $listenersManager = $this->listenersManager;
  174. }
  175. return $this->processRunner->run($process, $listenersManager->storage, $bypassErrors);
  176. }
  177. private function applyProcessConfiguration()
  178. {
  179. if ($this->configuration->has('timeout')) {
  180. $this->factory->setTimeout($this->configuration->get('timeout'));
  181. }
  182. return $this;
  183. }
  184. }