class Drush (View source)

Static Service Container wrapper.

This code is analogous to the \Drupal class in Drupal 8.

We would like to move Drush towards the model of using constructor injection rather than globals. This class serves as a unified global accessor to arbitrary services for use by legacy Drush code.

Advice from Drupal 8's 'Drupal' class:

This class exists only to support legacy code that cannot be dependency injected. If your code needs it, consider refactoring it to be object oriented, if possible. When this is not possible, and your code is more than a few non-reusable lines, it is recommended to instantiate an object implementing the actual logic.

Constants

TIMEOUT

Number of seconds before timeout for subprocesses. Can be customized via setTimeout() method.

Properties

static protected string|FALSE $version The version of Drush from the drush.info file, or FALSE if not read yet.
static protected $majorVersion
static protected $minorVersion
static protected Runner $runner The Robo Runner -- manages and constructs all commandfile classes

Methods

static int
getTimeout()

No description

static 
getVersion()

Return the current Drush version.

static 
getMajorVersion()

No description

static 
getMinorVersion()

No description

static 
setContainer(ContainerInterface $container)

Sets a new global container.

static 
unsetContainer()

Unsets the global container.

static ContainerInterface|null
getContainer()

Returns the currently active global container.

static bool
hasContainer()

Returns TRUE if the container has been initialized, FALSE otherwise.

static Application
getApplication()

Get the current Symfony Console Application.

static Runner
runner()

Return the Robo runner.

static mixed
service(string $id)

Retrieves a service from the container.

static bool
hasService(string $id)

Indicates if a service is defined in the container.

static AnnotatedCommandFactory
commandFactory()

Return command factory

static LoggerInterface
logger() deprecated

Return the Drush logger object.

static DrushConfig
config() deprecated

Return the configuration object

static SiteAliasManager
aliasManager() deprecated

No description

static ProcessManager
processManager() deprecated

No description

static InputInterface
input()

Return the input object

static OutputInterface
output()

Return the output object

static SiteProcess
drush(SiteAliasInterface $siteAlias, string $command, array $args = [], array $options = [], array $options_double_dash = [])

Run a Drush command on a site alias (or @self).

static ProcessBase
siteProcess(SiteAliasInterface $siteAlias, array $args = [], array $options = [], array $options_double_dash = [])

Run a bash fragment on a site alias. U

static ProcessBase
process($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60)

Run a bash fragment locally.

static Process
shell(string $command, string|null $cwd = null, array $env = null, mixed|null $input = null, int|float|null $timeout = 60)

Create a Process instance from a commandline string.

static 
simulate() deprecated

Return 'true' if we are in simulated mode

static 
affirmative()

Return 'true' if we are in affirmative mode

static 
negative()

Return 'true' if we are in negative mode

static 
verbose()

Return 'true' if we are in verbose mode

static 
debug()

Return 'true' if we are in debug mode

bootstrapManager()

Return the Bootstrap Manager.

static Boot
bootstrap()

Return the Bootstrap object.

static 
redispatchOptions($input = null)

No description

Details

static int getTimeout()

Return Value

int

static getVersion()

Return the current Drush version.

n.b. Called before the DI container is initialized. Do not log, etc. here.

static getMajorVersion()

static getMinorVersion()

static setContainer(ContainerInterface $container)

Sets a new global container.

Parameters

ContainerInterface $container A new container instance to replace the current.

static unsetContainer()

Unsets the global container.

static ContainerInterface|null getContainer()

Returns the currently active global container.

Return Value

ContainerInterface|null

Exceptions

RuntimeException

static bool hasContainer()

Returns TRUE if the container has been initialized, FALSE otherwise.

Return Value

bool

static Application getApplication()

Get the current Symfony Console Application.

Return Value

Application

static Runner runner()

Return the Robo runner.

Return Value

Runner

static mixed service(string $id)

Retrieves a service from the container.

Use this method if the desired service is not one of those with a dedicated accessor method below. If it is listed below, those methods are preferred as they can return useful type hints.

Parameters

string $id The ID of the service to retrieve.

Return Value

mixed The specified service.

static bool hasService(string $id)

Indicates if a service is defined in the container.

Parameters

string $id The ID of the service to check.

Return Value

bool TRUE if the specified service exists, FALSE otherwise.

static AnnotatedCommandFactory commandFactory()

Return command factory

Return Value

AnnotatedCommandFactory

static LoggerInterface logger() deprecated

deprecated Use injected logger instead.

Return the Drush logger object.

Return Value

LoggerInterface

static DrushConfig config() deprecated

deprecated Use injected configuration instead.

Return the configuration object

Return Value

DrushConfig

static SiteAliasManager aliasManager() deprecated

deprecated Use injected alias manager instead. @see Drush::drush()

Return Value

SiteAliasManager

static ProcessManager processManager() deprecated

deprecated Use injected process manager instead. @see Drush::drush()

Return Value

ProcessManager

static InputInterface input()

Return the input object

Return Value

InputInterface

static OutputInterface output()

Return the output object

Return Value

OutputInterface

static SiteProcess drush(SiteAliasInterface $siteAlias, string $command, array $args = [], array $options = [], array $options_double_dash = [])

Run a Drush command on a site alias (or @self).

Tip: Use injected process manager instead of this method. See below.

A class should use ProcessManagerAwareInterface / ProcessManagerAwareTrait in order to have the Process Manager injected by Drush's DI container. For example:

use Consolidation\SiteProcess\ProcessManagerAwareTrait;
use Consolidation\SiteProcess\ProcessManagerAwareInterface;

abstract class DrushCommands implements ProcessManagerAwareInterface ...
{
    use ProcessManagerAwareTrait;
}

Since DrushCommands already uses ProcessManagerAwareTrait, all Drush commands may use the process manager to call other Drush commands. Other classes will need to ensure that the process manager is injected as shown above.

Note, however, that an alias record is required to use the drush method. The alias manager will provide an alias record, but the alias manager is not injected by default into Drush commands. In order to use it, it is necessary to use SiteAliasManagerAwareTrait:

use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;

class SiteInstallCommands extends DrushCommands implements SiteAliasManagerAwareInterface
{
    use SiteAliasManagerAwareTrait;

    public function install(array $profile, ...)
    {
        $selfRecord = $this->siteAliasManager()->getSelf();
        $args = ['system.site', ...];
        $options = ['yes' => true];
        $process = $this->processManager()->drush(selfRecord, 'config-set', $args, $options);
        $process->mustRun();
    }
}

Objects that are fetched from the DI container, or any Drush command will automatically be given a reference to the alias manager if SiteAliasManagerAwareTrait is used. Other objects will need to be manually provided with a reference to the alias manager once it is created (call $obj->setAliasManager($aliasManager);).

Clients that are using Drush::drush(), and need a reference to the alias manager may use Drush::aliasManager().

Parameters

SiteAliasInterface $siteAlias
string $command
array $args
array $options
array $options_double_dash

Return Value

SiteProcess

static ProcessBase siteProcess(SiteAliasInterface $siteAlias, array $args = [], array $options = [], array $options_double_dash = [])

Run a bash fragment on a site alias. U

Use Drush::drush() instead of this method when calling Drush. Tip: Consider using injected process manager instead of this method. see \Drush\Drush::drush().

Parameters

SiteAliasInterface $siteAlias
array $args
array $options
array $options_double_dash

Return Value

ProcessBase

static ProcessBase process($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60)

Run a bash fragment locally.

The timeout parameter on this method doesn't work. It exists for compatibility with parent. Call this method to get a Process and then call setters as needed.

Tip: Consider using injected process manager instead of this method. see \Drush\Drush::drush().

Parameters

$commandline
$cwd
array $env
$input
$timeout

Return Value

ProcessBase A wrapper around Symfony Process.

static Process shell(string $command, string|null $cwd = null, array $env = null, mixed|null $input = null, int|float|null $timeout = 60)

Create a Process instance from a commandline string.

Tip: Consider using injected process manager instead of this method. see \Drush\Drush::drush().

Parameters

string $command The commandline string to run
string|null $cwd The working directory or null to use the working dir of the current PHP process
array $env The environment variables or null to use the same environment as the current PHP process
mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input
int|float|null $timeout The timeout in seconds or null to disable

Return Value

Process

static simulate() deprecated

deprecated Inject configuration and use $this->getConfig()->simulate().

Return 'true' if we are in simulated mode

static affirmative()

Return 'true' if we are in affirmative mode

static negative()

Return 'true' if we are in negative mode

static verbose()

Return 'true' if we are in verbose mode

static debug()

Return 'true' if we are in debug mode

static BootstrapManager bootstrapManager()

Return the Bootstrap Manager.

Return Value

BootstrapManager

static Boot bootstrap()

Return the Bootstrap object.

Return Value

Boot

static redispatchOptions($input = null)

Parameters

$input