class Collection extends BaseTask implements CollectionInterface, CommandInterface, StateAwareInterface (View source)

Group tasks into a collection that run together. Supports rollback operations for handling error conditions.

This is an internal class. Clients should use a CollectionBuilder rather than direct use of the Collection class. CollectionBuilder.

Below, the example FilesystemStack task is added to a collection, and associated with a rollback task. If any of the operations in the FilesystemStack, or if any of the other tasks also added to the task collection should fail, then the rollback function is called. Here, taskDeleteDir is used to remove partial results of an unfinished task.

Traits

Task input/output methods. TaskIO is 'used' in BaseTask, so any task that extends this class has access to all of the methods here.

Task input/output methods. TaskIO is 'used' in BaseTask, so any task that extends this class has access to all of the methods here.

LoggerAwareTrait

Properties

protected ConfigInterface $config from  ConfigAwareTrait
protected OutputAdapterInterface $outputAdapter from  VerbosityThresholdTrait
protected int $verbosityThreshold from  VerbosityThresholdTrait
protected OutputInterface $output from  OutputAwareTrait
protected TimeKeeper|null $timer from  Timer
protected null|ProgressIndicator $progressIndicator from  ProgressIndicatorAwareTrait
protected Data $state from  StateAwareTrait
protected Element[] $taskList
protected TaskInterface[] $rollbackStack
protected TaskInterface[] $completionStack
protected CollectionInterface $parentCollection
protected callable[] $deferredCallbacks
protected string[] $messageStoreKeys

Methods

$this
setConfig(ConfigInterface $config)

Set the config management object.

ConfigInterface
getConfig()

Get the config management object.

static string
configPrefix()

Any class that uses ConfigAwareTrait SHOULD override this method , and define a prefix for its configuration items. This is usually done in a base class. When used, this method should return a string that ends with a "."; see BaseTask::configPrefix().

static 
configClassIdentifier($classname)

No description

static 
configPostfix()

No description

static 
configure(string $key, mixed $value, ConfigInterface|null $config = null)

No description

mixed|null
getConfigValue(string $key, mixed|null $default = null)

No description

$this
setVerbosityThreshold(int $verbosityThreshold)

Required verbosity level before any TaskIO output will be produced.

int
verbosityThreshold()

No description

bool
hasOutputAdapter()

No description

writeMessage(string $message)

Print a message if the selected verbosity level is over this task's verbosity threshold.

$this
setOutput(OutputInterface $output)

No description

OutputInterface
output()

No description

OutputInterface
stderr()

No description

OutputInterface
getOutput() deprecated

Backwards compatibility

null|LoggerInterface
logger() deprecated

No description

from  TaskIO
printTaskInfo(string $text, null|array $context = null)

Print information about a task in progress.

from  TaskIO
printTaskSuccess(string $text, null|array $context = null)

Provide notification that some part of the task succeeded.

from  TaskIO
printTaskWarning(string $text, null|array $context = null)

Provide notification that there is something wrong, but execution can continue.

from  TaskIO
printTaskError(string $text, null|array $context = null)

Provide notification that some operation in the task failed, and the task cannot continue.

from  TaskIO
printTaskDebug($text, null|array $context = null)

Provide debugging notification. These messages are only displayed if the log level is VERBOSITY_DEBUG.

from  TaskIO
printTaskOutput(string $level, string $text, null|array $context) deprecated

No description

from  TaskIO
bool
hideTaskProgress()

No description

from  TaskIO
showTaskProgress(bool $inProgress)

No description

from  TaskIO
string
formatBytes(int $size, int $precision = 2)

Format a quantity of bytes.

from  TaskIO
string
getPrintedTaskName(null|object $task = null)

Get the formatted task name for use in task output.

from  TaskIO
array
getTaskContext(null|array $context = null)

No description

from  TaskIO
startTimer()

No description

from  Timer
stopTimer()

No description

from  Timer
resetTimer()

No description

from  Timer
float|null
getExecutionTime()

No description

from  Timer
int
progressIndicatorSteps()

Return the count of steps in this collection

$this
setProgressIndicator(null|ProgressIndicator $progressIndicator)

No description

null|bool
hideProgressIndicator()

No description

restoreProgressIndicator(bool $visible)

No description

bool
inProgress()

No description

advanceProgressIndicator(int $steps = 1)

No description

$this
inflect(InflectionInterface|mixed $parent)

Ask the provided parent class to inject all of the dependencies that it has and we need.

injectDependencies(mixed $child)

Take all dependencies availble to this task and inject any that are needed into the provided task. The general pattern is that, for every FooAwareInterface that this class implements, it should test to see if the child also implements the same interface, and if so, should call $child->setFoo($this->foo).

from  BaseTask
getState()

No description

setState(Data $state)

No description

setStateValue(int|string $key, mixed $value)

No description

updateState(Data $update)

No description

resetState()

No description

__construct()

Constructor.

setProgressBarAutoDisplayInterval(int $interval)

No description

$this
add(TaskInterface $task, int|string $name = self::UNNAMEDTASK)

Add a task or a list of tasks to our task collection. Each task will run via its 'run()' method once (and if) all of the tasks added before it complete successfully. If the task also implements RollbackInterface, then it will be rolled back via its 'rollback()' method ONLY if its 'run()' method completes successfully, and some task added after it fails.

$this
addCode(callable $code, int|string $name = self::UNNAMEDTASK)

Add arbitrary code to execute as a task.

$this
addIterable(CollectionInterface|array $iterable, callable $code)

Add arbitrary code that will be called once for every item in the provided array or iterable object. If the function result of the provided callback is a TaskInterface or Collection, then it will be executed.

$this
rollback(TaskInterface $rollbackTask)

Add a rollback task to our task collection. A rollback task will execute ONLY if all of the tasks added before it complete successfully, AND some task added after it fails.

$this
rollbackCode(callable $rollbackCode)

Add arbitrary code to execute as a rollback.

$this
completion(TaskInterface $completionTask)

Add a completion task to our task collection. A completion task will execute EITHER after all tasks succeed, OR immediatley after any task fails. Completion tasks never cause errors to be returned from Collection::run(), even if they fail.

$this
completionCode(callable $completionTask)

Add arbitrary code to execute as a completion.

$this
before(string $name, callable|TaskInterface $task, int|string $nameOfTaskToAdd = self::UNNAMEDTASK)

Add a task before an existing named task.

$this
after(string $name, callable|TaskInterface $task, int|string $nameOfTaskToAdd = self::UNNAMEDTASK)

Add a task after an existing named task.

$this
progressMessage(string $text, array $context = [], LogLevel|string $level = LogLevel::NOTICE)

Print a progress message after Collection::run() has executed all of the tasks that were added prior to the point when this method was called. If one of the previous tasks fail, then this message will not be printed.

$this
wrapAndRegisterRollback(TaskInterface $rollbackTask)

No description

$this
addBeforeOrAfter(string $method, string $name, callable|TaskInterface $task, string $nameOfTaskToAdd)

Add either a 'before' or 'after' function or task.

ignoreErrorsTaskWrapper(TaskInterface $task)

Wrap the provided task in a wrapper that will ignore any errors or exceptions that may be produced. This is useful, for example, in adding optional cleanup tasks at the beginning of a task collection, to remove previous results which may or may not exist.

ignoreErrorsCodeWrapper(callable $task)

No description

string[]
taskNames()

Return the list of task names added to this collection.

bool
hasTask(string $name)

Test to see if a specified task name exists.

namedTask(string $name)

Find an existing named task.

$this
addTaskList(array $tasks)

Add a list of tasks to our task collection.

$this
addToTaskList(string $name, TaskInterface $task)

Add the provided task to our task list.

$this
addCollectionElementToTaskList(int|string $name, Element $taskGroup)

No description

$this
setParentCollection(NestedCollectionInterface $parentCollection)

Set the parent collection. This is necessary so that nested collections' rollback and completion tasks can be added to the top-level collection, ensuring that the rollbacks for a collection will run if any later task fails.

getParentCollection()

Get the appropriate parent collection to use

null
registerRollback(TaskInterface $rollbackTask)

Register a rollback task to run if there is any failure.

null
registerCompletion(TaskInterface $completionTask)

Register a completion task to run once all other tasks finish.

string
getCommand()

A Collection of tasks can provide a command via getCommand() if it contains a single task, and that task implements CommandInterface.

run()

Run our tasks, and roll back if necessary.

$this
fail()

Force the rollback functions to run

$this
complete()

Force the completion functions to run

$this
reset()

Reset this collection, removing all tasks.

runRollbackTasks()

Run all of our rollback tasks.

doStateUpdates(TaskInterface $task, Data $taskResult)

No description

$this
storeState(TaskInterface $task, string $key, string $source = '')

No description

$this
deferTaskConfiguration(TaskInterface $task, string $functionName, string $stateKey)

No description

$this
defer(TaskInterface $task, callable $callback)

Defer execution of a callback function until just before a task runs. Use this time to provide more settings for the task, e.g. from the collection's shared state, which is populated with the results of previous test runs.

doDeferredInitialization(TaskInterface $task)

No description

runTaskListIgnoringFailures(array $taskList)

Run all of the tasks in a provided list, ignoring failures.

transferTasks(CollectionBuilder $builder)

Give all of our tasks to the provided collection builder.

Details

$this setConfig(ConfigInterface $config)

Set the config management object.

Parameters

ConfigInterface $config

Return Value

$this

ConfigInterface getConfig()

Get the config management object.

Return Value

ConfigInterface

static protected string configPrefix()

Any class that uses ConfigAwareTrait SHOULD override this method , and define a prefix for its configuration items. This is usually done in a base class. When used, this method should return a string that ends with a "."; see BaseTask::configPrefix().

Return Value

string

static protected configClassIdentifier($classname)

No description

Parameters

$classname

static protected configPostfix()

No description

static configure(string $key, mixed $value, ConfigInterface|null $config = null)

No description

Parameters

string $key
mixed $value
ConfigInterface|null $config

protected mixed|null getConfigValue(string $key, mixed|null $default = null)

No description

Parameters

string $key
mixed|null $default

Return Value

mixed|null

$this setVerbosityThreshold(int $verbosityThreshold)

Required verbosity level before any TaskIO output will be produced.

e.g. OutputInterface::VERBOSITY_VERBOSE

Parameters

int $verbosityThreshold

Return Value

$this

int verbosityThreshold()

No description

Return Value

int

setOutputAdapter(OutputAdapterInterface $outputAdapter)

No description

Parameters

OutputAdapterInterface $outputAdapter

bool hasOutputAdapter()

No description

Return Value

bool

bool verbosityMeetsThreshold()

No description

Return Value

bool

writeMessage(string $message)

Print a message if the selected verbosity level is over this task's verbosity threshold.

Parameters

string $message

$this setOutput(OutputInterface $output)

No description

Parameters

OutputInterface $output

Return Value

$this

See also

\Robo\Contract\OutputAwareInterface::setOutput()

protected OutputInterface output()

No description

Return Value

OutputInterface

protected OutputInterface stderr()

No description

Return Value

OutputInterface

protected OutputInterface getOutput() deprecated

deprecated

Backwards compatibility

Return Value

OutputInterface

null|LoggerInterface logger() deprecated

deprecated

No description

Return Value

null|LoggerInterface

protected printTaskInfo(string $text, null|array $context = null)

Print information about a task in progress.

With the Symfony Console logger, NOTICE is displayed at VERBOSITY_VERBOSE and INFO is displayed at VERBOSITY_VERY_VERBOSE.

Robo overrides the default such that NOTICE is displayed at VERBOSITY_NORMAL and INFO is displayed at VERBOSITY_VERBOSE.

n.b. We should probably have printTaskNotice for our ordinary output, and use printTaskInfo for less interesting messages.

Parameters

string $text
null|array $context

protected printTaskSuccess(string $text, null|array $context = null)

Provide notification that some part of the task succeeded.

With the Symfony Console logger, success messages are remapped to NOTICE, and displayed in VERBOSITY_VERBOSE. When used with the Robo logger, success messages are displayed at VERBOSITY_NORMAL.

Parameters

string $text
null|array $context

protected printTaskWarning(string $text, null|array $context = null)

Provide notification that there is something wrong, but execution can continue.

Warning messages are displayed at VERBOSITY_NORMAL.

Parameters

string $text
null|array $context

protected printTaskError(string $text, null|array $context = null)

Provide notification that some operation in the task failed, and the task cannot continue.

Error messages are displayed at VERBOSITY_NORMAL.

Parameters

string $text
null|array $context

protected printTaskDebug($text, null|array $context = null)

Provide debugging notification. These messages are only displayed if the log level is VERBOSITY_DEBUG.

Parameters

$text
null|array $context

protected printTaskOutput(string $level, string $text, null|array $context) deprecated

deprecated

No description

Parameters

string $level

One of the \Psr\Log\LogLevel constant

string $text
null|array $context

protected bool hideTaskProgress()

No description

Return Value

bool

protected showTaskProgress(bool $inProgress)

No description

Parameters

bool $inProgress

protected string formatBytes(int $size, int $precision = 2)

Format a quantity of bytes.

Parameters

int $size
int $precision

Return Value

string

protected string getPrintedTaskName(null|object $task = null)

Get the formatted task name for use in task output.

This is placed in the task context under 'name', and used as the log label by Robo\Common\RoboLogStyle, which is inserted at the head of log messages by Robo\Common\CustomLogStyle::formatMessage().

Parameters

null|object $task

Return Value

string

protected array getTaskContext(null|array $context = null)

No description

Parameters

null|array $context

Return Value

array

Context information.

protected startTimer()

No description

protected stopTimer()

No description

protected resetTimer()

No description

protected float|null getExecutionTime()

No description

Return Value

float|null

int progressIndicatorSteps()

Return the count of steps in this collection

Return Value

int

$this setProgressIndicator(null|ProgressIndicator $progressIndicator)

No description

Parameters

null|ProgressIndicator $progressIndicator

Return Value

$this

protected null|bool hideProgressIndicator()

No description

Return Value

null|bool

protected showProgressIndicator()

No description

protected restoreProgressIndicator(bool $visible)

No description

Parameters

bool $visible

protected int getTotalExecutionTime()

No description

Return Value

int

protected startProgressIndicator()

No description

protected bool inProgress()

No description

Return Value

bool

protected stopProgressIndicator()

No description

protected disableProgressIndicator()

No description

protected detatchProgressIndicator()

No description

protected advanceProgressIndicator(int $steps = 1)

No description

Parameters

int $steps

$this inflect(InflectionInterface|mixed $parent)

Ask the provided parent class to inject all of the dependencies that it has and we need.

Parameters

InflectionInterface|mixed $parent

Return Value

$this

injectDependencies(mixed $child)

Take all dependencies availble to this task and inject any that are needed into the provided task. The general pattern is that, for every FooAwareInterface that this class implements, it should test to see if the child also implements the same interface, and if so, should call $child->setFoo($this->foo).

The benefits of this are pretty large. Any time an object that implements InflectionInterface is created, just call $child->inflect($this), and any available optional dependencies will be hooked up via setter injection.

The required dependencies of an object should be provided via constructor injection, not inflection.

Parameters

mixed $child

An object with one or more *AwareInterfaces implemented.

Data getState()

No description

Return Value

Data

setState(Data $state)

No description

Parameters

Data $state

setStateValue(int|string $key, mixed $value)

No description

Parameters

int|string $key
mixed $value

updateState(Data $update)

No description

Parameters

Data $update

resetState()

No description

__construct()

Constructor.

setProgressBarAutoDisplayInterval(int $interval)

No description

Parameters

int $interval

$this add(TaskInterface $task, int|string $name = self::UNNAMEDTASK)

Add a task or a list of tasks to our task collection. Each task will run via its 'run()' method once (and if) all of the tasks added before it complete successfully. If the task also implements RollbackInterface, then it will be rolled back via its 'rollback()' method ONLY if its 'run()' method completes successfully, and some task added after it fails.

Parameters

TaskInterface $task

The task to add to our collection.

int|string $name

An optional name for the task -- missing or UNNAMEDTASK for unnamed tasks. Names are used for positioning before and after tasks.

Return Value

$this

$this addCode(callable $code, int|string $name = self::UNNAMEDTASK)

Add arbitrary code to execute as a task.

Parameters

callable $code

Code to execute as a task

int|string $name

An optional name for the task -- missing or UNNAMEDTASK for unnamed tasks. Names are used for positioning before and after tasks.

Return Value

$this

$this addIterable(CollectionInterface|array $iterable, callable $code)

Add arbitrary code that will be called once for every item in the provided array or iterable object. If the function result of the provided callback is a TaskInterface or Collection, then it will be executed.

Parameters

CollectionInterface|array $iterable

A collection of things to iterate.

callable $code

A callback function to call for each item in the collection.

Return Value

$this

$this rollback(TaskInterface $rollbackTask)

Add a rollback task to our task collection. A rollback task will execute ONLY if all of the tasks added before it complete successfully, AND some task added after it fails.

Parameters

TaskInterface $rollbackTask

The rollback task to add. Note that the 'run()' method of the task executes, not its 'rollback()' method. To use the 'rollback()' method, add the task via 'Collection::add()' instead.

Return Value

$this

$this rollbackCode(callable $rollbackCode)

Add arbitrary code to execute as a rollback.

Parameters

callable $rollbackCode

Return Value

$this

$this completion(TaskInterface $completionTask)

Add a completion task to our task collection. A completion task will execute EITHER after all tasks succeed, OR immediatley after any task fails. Completion tasks never cause errors to be returned from Collection::run(), even if they fail.

Parameters

TaskInterface $completionTask

The completion task to add. Note that the 'run()' method of the task executes, just as if the task was added normally.

Return Value

$this

$this completionCode(callable $completionTask)

Add arbitrary code to execute as a completion.

Parameters

callable $completionTask

Code to execute after collection completes

Return Value

$this

$this before(string $name, callable|TaskInterface $task, int|string $nameOfTaskToAdd = self::UNNAMEDTASK)

Add a task before an existing named task.

Parameters

string $name

The name of the task to insert before. The named task MUST exist.

callable|TaskInterface $task

The task to add.

int|string $nameOfTaskToAdd

The name of the task to add. If not provided, will be associated with the named task it was added before.

Return Value

$this

$this after(string $name, callable|TaskInterface $task, int|string $nameOfTaskToAdd = self::UNNAMEDTASK)

Add a task after an existing named task.

Parameters

string $name

The name of the task to insert before. The named task MUST exist.

callable|TaskInterface $task

The task to add.

int|string $nameOfTaskToAdd

The name of the task to add. If not provided, will be associated with the named task it was added after.

Return Value

$this

$this progressMessage(string $text, array $context = [], LogLevel|string $level = LogLevel::NOTICE)

Print a progress message after Collection::run() has executed all of the tasks that were added prior to the point when this method was called. If one of the previous tasks fail, then this message will not be printed.

Parameters

string $text

Message to print.

array $context

Extra context data for use by the logger. Note that the data from the collection state is merged with the provided context.

LogLevel|string $level

The log level to print the information at. Default is NOTICE.

Return Value

$this

protected $this wrapAndRegisterRollback(TaskInterface $rollbackTask)

No description

Parameters

TaskInterface $rollbackTask

Return Value

$this

protected $this addBeforeOrAfter(string $method, string $name, callable|TaskInterface $task, string $nameOfTaskToAdd)

Add either a 'before' or 'after' function or task.

Parameters

string $method
string $name
callable|TaskInterface $task
string $nameOfTaskToAdd

Return Value

$this

CallableTask ignoreErrorsTaskWrapper(TaskInterface $task)

Wrap the provided task in a wrapper that will ignore any errors or exceptions that may be produced. This is useful, for example, in adding optional cleanup tasks at the beginning of a task collection, to remove previous results which may or may not exist.

TODO: Provide some way to specify which sort of errors are ignored, so that 'file not found' may be ignored, but 'permission denied' reported?

Parameters

TaskInterface $task

Return Value

CallableTask

CallableTask ignoreErrorsCodeWrapper(callable $task)

No description

Parameters

callable $task

Return Value

CallableTask

string[] taskNames()

Return the list of task names added to this collection.

Return Value

string[]

bool hasTask(string $name)

Test to see if a specified task name exists.

n.b. before() and after() require that the named task exist; use this function to test first, if unsure.

Parameters

string $name

Return Value

bool

protected Element namedTask(string $name)

Find an existing named task.

Parameters

string $name

The name of the task to insert before. The named task MUST exist.

Return Value

Element

The task group for the named task. Generally this is only used to call 'before()' and 'after()'.

$this addTaskList(array $tasks)

Add a list of tasks to our task collection.

Parameters

array $tasks

An array of tasks to run with rollback protection

Return Value

$this

protected $this addToTaskList(string $name, TaskInterface $task)

Add the provided task to our task list.

Parameters

string $name
TaskInterface $task

Return Value

$this

protected $this addCollectionElementToTaskList(int|string $name, Element $taskGroup)

No description

Parameters

int|string $name
Element $taskGroup

Return Value

$this

$this setParentCollection(NestedCollectionInterface $parentCollection)

Set the parent collection. This is necessary so that nested collections' rollback and completion tasks can be added to the top-level collection, ensuring that the rollbacks for a collection will run if any later task fails.

Parameters

NestedCollectionInterface $parentCollection

Return Value

$this

CollectionInterface|$this getParentCollection()

Get the appropriate parent collection to use

Return Value

CollectionInterface|$this

null registerRollback(TaskInterface $rollbackTask)

Register a rollback task to run if there is any failure.

Clients are free to add tasks to the rollback stack as desired; however, usually it is preferable to call Collection::rollback() instead. With that function, the rollback function will only be called if all of the tasks added before it complete successfully, AND some later task fails.

One example of a good use-case for registering a callback function directly is to add a task that sends notification when a task fails.

Parameters

TaskInterface $rollbackTask

The rollback task to run on failure.

Return Value

null

null registerCompletion(TaskInterface $completionTask)

Register a completion task to run once all other tasks finish.

Completion tasks run whether or not a rollback operation was triggered. They do not trigger rollbacks if they fail.

The typical use-case for a completion function is to clean up temporary objects (e.g. temporary folders). The preferred way to do that, though, is to use Temporary::wrap().

On failures, completion tasks will run after all rollback tasks. If one task collection is nested inside another task collection, then the nested collection's completion tasks will run as soon as the nested task completes; they are not deferred to the end of the containing collection's execution.

Parameters

TaskInterface $completionTask

The completion task to run at the end of all other operations.

Return Value

null

string getCommand()

A Collection of tasks can provide a command via getCommand() if it contains a single task, and that task implements CommandInterface.

Return Value

string

Exceptions

TaskException

Result run()

Run our tasks, and roll back if necessary.

Return Value

Result

$this fail()

Force the rollback functions to run

Return Value

$this

$this complete()

Force the completion functions to run

Return Value

$this

$this reset()

Reset this collection, removing all tasks.

Return Value

$this

protected runRollbackTasks()

Run all of our rollback tasks.

Note that Collection does not implement RollbackInterface, but it may still be used as a task inside another task collection (i.e. you can nest task collections, if desired).

protected doStateUpdates(TaskInterface $task, Data $taskResult)

No description

Parameters

TaskInterface $task
Data $taskResult

$this storeState(TaskInterface $task, string $key, string $source = '')

No description

Parameters

TaskInterface $task
string $key
string $source

Return Value

$this

$this deferTaskConfiguration(TaskInterface $task, string $functionName, string $stateKey)

No description

Parameters

TaskInterface $task
string $functionName
string $stateKey

Return Value

$this

$this defer(TaskInterface $task, callable $callback)

Defer execution of a callback function until just before a task runs. Use this time to provide more settings for the task, e.g. from the collection's shared state, which is populated with the results of previous test runs.

Parameters

TaskInterface $task
callable $callback

Return Value

$this

protected doDeferredInitialization(TaskInterface $task)

No description

Parameters

TaskInterface $task

protected setParentCollectionForTask(TaskInterface|NestedCollectionInterface|WrappedTaskInterface $task, CollectionInterface $parentCollection)

No description

protected runTaskListIgnoringFailures(array $taskList)

Run all of the tasks in a provided list, ignoring failures.

You may force a failure by throwing a ForcedException in your rollback or completion task or callback.

This is used to roll back or complete.

Parameters

array $taskList

transferTasks(CollectionBuilder $builder)

Give all of our tasks to the provided collection builder.

Parameters

CollectionBuilder $builder