2 namespace Robo\Collection;
5 use Robo\Contract\TaskInterface;
7 interface CollectionInterface extends NestedCollectionInterface
10 * Unnamed tasks are assigned an arbitrary numeric index
11 * in the task list. Any numeric value may be used, but the
12 * UNNAMEDTASK constant is recommended for clarity.
16 const UNNAMEDTASK = 0;
19 * Add a task or a list of tasks to our task collection. Each task
20 * will run via its 'run()' method once (and if) all of the tasks
21 * added before it complete successfully. If the task also implements
22 * RollbackInterface, then it will be rolled back via its 'rollback()'
23 * method ONLY if its 'run()' method completes successfully, and some
24 * task added after it fails.
26 * @param TaskInterface $task
27 * The task to add to our collection.
28 * @param int|string $name
29 * An optional name for the task -- missing or UNNAMEDTASK for unnamed tasks.
30 * Names are used for positioning before and after tasks.
32 * @return CollectionInterface
34 public function add(TaskInterface $task, $name = self::UNNAMEDTASK);
37 * Add arbitrary code to execute as a task.
39 * @param callable $code Code to execute as a task
40 * @param int|string $name
41 * An optional name for the task -- missing or UNNAMEDTASK for unnamed tasks.
42 * Names are used for positioning before and after tasks.
46 public function addCode(callable $code, $name = self::UNNAMEDTASK);
49 * Add arbitrary code that will be called once for every item in the
50 * provided array or iterable object. If the function result of the
51 * provided callback is a TaskInterface or Collection, then it will be
54 * @param CollectionInterface|array $iterable A collection of things to iterate
55 * @param $code $code A callback function to call for each item in the collection.
59 public function addIterable($iterable, callable $code);
62 * Add a rollback task to our task collection. A rollback task
63 * will execute ONLY if all of the tasks added before it complete
64 * successfully, AND some task added after it fails.
66 * @param TaskInterface $rollbackTask
67 * The rollback task to add. Note that the 'run()' method of the
68 * task executes, not its 'rollback()' method. To use the 'rollback()'
69 * method, add the task via 'Collection::add()' instead.
73 public function rollback(TaskInterface $rollbackTask);
76 * Add arbitrary code to execute as a rollback.
78 * @param callable $rollbackTask Code to execute during rollback processing
82 public function rollbackCode(callable $rollbackTask);
85 * Add a completion task to our task collection. A completion task
86 * will execute EITHER after all tasks succeed, OR immediatley after
87 * any task fails. Completion tasks never cause errors to be returned
88 * from Collection::run(), even if they fail.
90 * @param TaskInterface $completionTask
91 * The completion task to add. Note that the 'run()' method of the
92 * task executes, just as if the task was added normally.
96 public function completion(TaskInterface $completionTask);
99 * Add arbitrary code to execute as a completion.
101 * @param callable $completionTask Code to execute after collection completes
105 public function completionCode(callable $completionTask);
108 * Add a task before an existing named task.
110 * @param string $name
111 * The name of the task to insert before. The named task MUST exist.
112 * @param callable|TaskInterface $task
114 * @param int|string $nameOfTaskToAdd
115 * The name of the task to add. If not provided, will be associated
116 * with the named task it was added before.
120 public function before($name, $task, $nameOfTaskToAdd = self::UNNAMEDTASK);
123 * Add a task after an existing named task.
125 * @param string $name
126 * The name of the task to insert before. The named task MUST exist.
127 * @param callable|TaskInterface $task
129 * @param int|string $nameOfTaskToAdd
130 * The name of the task to add. If not provided, will be associated
131 * with the named task it was added after.
135 public function after($name, $task, $nameOfTaskToAdd = self::UNNAMEDTASK);
138 * Print a progress message after Collection::run() has executed
139 * all of the tasks that were added prior to the point when this
140 * method was called. If one of the previous tasks fail, then this
141 * message will not be printed.
143 * @param string $text Message to print.
144 * @param array $context Extra context data for use by the logger. Note
145 * that the data from the collection state is merged with the provided context.
146 * @param \Psr\Log\LogLevel|string $level The log level to print the information at. Default is NOTICE.
150 public function progressMessage($text, $context = [], $level = LogLevel::NOTICE);