Driver.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Shell;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * Shell driver encapsulates command execution and arguments escaping
  10. */
  11. class Driver
  12. {
  13. /**
  14. * @var \Magento\Framework\Shell\CommandRendererInterface
  15. */
  16. private $commandRenderer;
  17. /**
  18. * @param CommandRendererInterface $commandRenderer
  19. */
  20. public function __construct(CommandRendererInterface $commandRenderer)
  21. {
  22. $this->commandRenderer = $commandRenderer;
  23. }
  24. /**
  25. * Execute a command through the command line, passing properly escaped arguments, and return its output
  26. *
  27. * @param string $command Command with optional argument markers '%s'
  28. * @param string[] $arguments Argument values to substitute markers with
  29. * @return Response
  30. * @throws LocalizedException
  31. */
  32. public function execute($command, $arguments)
  33. {
  34. $disabled = explode(',', str_replace(' ', ',', ini_get('disable_functions')));
  35. if (in_array('exec', $disabled)) {
  36. throw new LocalizedException(new \Magento\Framework\Phrase('The exec function is disabled.'));
  37. }
  38. $command = $this->commandRenderer->render($command, $arguments);
  39. exec($command, $output, $exitCode);
  40. $output = implode(PHP_EOL, $output);
  41. return new Response(['output' => $output, 'exit_code' => $exitCode, 'escaped_command' => $command]);
  42. }
  43. }