class Drush (View source)

Static Service Container wrapper.

This code is analogous to the \Drupal class.

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'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

protected 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 
sanitizeVersionString($version)

Convert internal Composer dev version to ".x"

static string
getMajorVersion()

No description

static string
getMinorVersion()

No description

static void
setContainer($container)

Sets a new global container.

static void
unsetContainer()

Unsets the global container.

static ContainerInterface
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 
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()

Return the Drush logger object.

static DrushConfig
config()

Return the configuration object

static SiteAliasManager
aliasManager()

No description

static ProcessManager
processManager()

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.

static ProcessBase
process(string|array $commandline, string|null $cwd = null, array|null $env = null, mixed|null $input = null, int|float|null $timeout = 60)

Run a bash fragment locally.

static ProcessBase
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()

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 bool
verbose()

Return 'true' if we are in verbose mode

static bool
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()

No description

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 sanitizeVersionString($version)

Convert internal Composer dev version to ".x"

Parameters

$version

static string getMajorVersion()

No description

Return Value

string

static string getMinorVersion()

No description

Return Value

string

static void setContainer($container)

Sets a new global container.

Parameters

$container

Return Value

void

static void unsetContainer()

Unsets the global container.

Return Value

void

static ContainerInterface getContainer()

Returns the currently active global container.

Return Value

ContainerInterface

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 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.

static bool hasService(string $id)

Indicates if a service is defined in the container.

Parameters

string $id

Return Value

bool

static AnnotatedCommandFactory commandFactory()

Return command factory

Return Value

AnnotatedCommandFactory

static LoggerInterface logger()

internal  Commands should use $this->logger() instead.
 

Return the Drush logger object.

Return Value

LoggerInterface

static DrushConfig config()

internal  Commands should use $this->config() instead.
 

Return the configuration object

Return Value

DrushConfig

static SiteAliasManager aliasManager()

internal  Commands should use $this->siteAliasManager() instead.
 

No description

Return Value

SiteAliasManager

static ProcessManager processManager()

internal  Commands should use $this->processManager() instead.
 

No description

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 processManager() 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.

Use \Drush\Drush::drush() instead of this method when calling Drush.

Tip: Commands can consider using $this->processManager() instead of this method.

Parameters

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

Return Value

ProcessBase

static ProcessBase process(string|array $commandline, string|null $cwd = null, array|null $env = null, mixed|null $input = null, int|float|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.

Parameters

string|array $commandline

The command line to run

string|null $cwd

The working directory or null to use the working dir of the current PHP process

array|null $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

ProcessBase

wrapper around Symfony Process.

static ProcessBase 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.

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

ProcessBase

wrapper around Symfony Process.

static simulate()

internal  Commands should 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 bool verbose()

Return 'true' if we are in verbose mode

Return Value

bool

static bool debug()

Return 'true' if we are in debug mode

Return Value

bool

static BootstrapManager bootstrapManager()

Return the Bootstrap Manager.

Return Value

BootstrapManager

static Boot bootstrap()

Return the Bootstrap object.

Return Value

Boot

static redispatchOptions($input = null)

No description

Parameters

$input