Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Shell.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework;
8 
10 
14 class Shell implements ShellInterface
15 {
21  protected $logger;
22 
26  private $commandRenderer;
27 
32  public function __construct(
33  CommandRendererInterface $commandRenderer,
34  \Psr\Log\LoggerInterface $logger = null
35  ) {
36  $this->logger = $logger;
37  $this->commandRenderer = $commandRenderer;
38  }
39 
48  public function execute($command, array $arguments = [])
49  {
50  $command = $this->commandRenderer->render($command, $arguments);
51  $this->log($command);
52 
53  $disabled = explode(',', str_replace(' ', ',', ini_get('disable_functions')));
54  if (in_array('exec', $disabled)) {
55  throw new Exception\LocalizedException(new \Magento\Framework\Phrase('The exec function is disabled.'));
56  }
57 
58  exec($command, $output, $exitCode);
59  $output = implode(PHP_EOL, $output);
60  $this->log($output);
61 
62  if ($exitCode) {
63  $commandError = new \Exception($output, $exitCode);
64  throw new Exception\LocalizedException(
65  new \Magento\Framework\Phrase("Command returned non-zero exit code:\n`%1`", [$command]),
66  $commandError
67  );
68  }
69  return $output;
70  }
71 
78  protected function log($message)
79  {
80  if ($this->logger) {
81  $this->logger->info($message);
82  }
83  }
84 }
exec($command, array &$output=null, &$return_var=null)
__construct(CommandRendererInterface $commandRenderer, \Psr\Log\LoggerInterface $logger=null)
Definition: Shell.php:32
execute($command, array $arguments=[])
Definition: Shell.php:48
$message
$arguments