Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Shell Class Reference
Inheritance diagram for Shell:
ShellInterface

Public Member Functions

 __construct (CommandRendererInterface $commandRenderer, \Psr\Log\LoggerInterface $logger=null)
 
 execute ($command, array $arguments=[])
 

Protected Member Functions

 log ($message)
 

Protected Attributes

 $logger
 

Detailed Description

Shell command line wrapper encapsulates command execution and arguments escaping

Definition at line 14 of file Shell.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CommandRendererInterface  $commandRenderer,
\Psr\Log\LoggerInterface  $logger = null 
)
Parameters
CommandRendererInterface$commandRenderer
\Psr\Log\LoggerInterface$loggerLogger instance to be used to log commands and their output

Definition at line 32 of file Shell.php.

35  {
36  $this->logger = $logger;
37  $this->commandRenderer = $commandRenderer;
38  }

Member Function Documentation

◆ execute()

execute (   $command,
array  $arguments = [] 
)

Execute a command through the command line, passing properly escaped arguments, and return its output

Parameters
string$commandCommand with optional argument markers 's'
string[]$arguments Argument values to substitute markers with
Returns
string Output of an executed command
Exceptions

Implements ShellInterface.

Definition at line 48 of file Shell.php.

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  }
exec($command, array &$output=null, &$return_var=null)
$arguments

◆ log()

log (   $message)
protected

Log a message, if a logger is specified

Parameters
string$message
Returns
void

Definition at line 78 of file Shell.php.

79  {
80  if ($this->logger) {
81  $this->logger->info($message);
82  }
83  }
$message

Field Documentation

◆ $logger

$logger
protected

Definition at line 21 of file Shell.php.


The documentation for this class was generated from the following file: