1 | <?php |
||
30 | class Builder |
||
|
|||
31 | { |
||
32 | protected $builddir; |
||
33 | protected $io; |
||
34 | protected $dispatcher; |
||
35 | |||
36 | /** |
||
37 | * @var Context |
||
38 | */ |
||
39 | protected $context; |
||
40 | |||
41 | /** |
||
42 | * @var \Webcreate\Conveyor\Task\TaskRunner |
||
43 | */ |
||
44 | protected $taskRunner; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | * @todo Can we refactor so we don't have to depend on the IOInterface? |
||
50 | * |
||
51 | * @param string $builddir destionation path for build |
||
52 | * @param array $tasks |
||
53 | * @param IOInterface $io |
||
54 | * @param EventDispatcherInterface $dispatcher |
||
55 | */ |
||
56 | 3 | public function __construct($builddir, array $tasks = array(), |
|
65 | |||
66 | /** |
||
67 | * Creates a task runner for builder tasks |
||
68 | * |
||
69 | * @param Task[] $tasks |
||
70 | * @return TaskRunner |
||
71 | */ |
||
72 | 3 | protected function createTaskRunner(array $tasks) |
|
73 | { |
||
74 | 3 | $self = $this; |
|
75 | |||
76 | 3 | $taskRunner = new TaskRunner($this->io, new EventDispatcher()); |
|
77 | 3 | $taskRunner->setTasks($tasks); |
|
78 | |||
79 | 3 | $taskRunner->getDispatcher()->addListener( |
|
80 | 3 | TaskRunnerEvents::TASKRUNNER_PRE_EXECUTE_TASK, |
|
81 | function (GenericEvent $event) use ($self) { |
||
82 | 2 | $task = $event->getSubject(); |
|
83 | 2 | $t = $event->getArgument('index'); |
|
84 | 2 | $total = $event->getArgument('total'); |
|
85 | |||
86 | 2 | $self->dispatch(BuilderEvents::BUILDER_PRE_TASK, |
|
87 | 2 | new GenericEvent($task, array('index' => $t, 'total' => $total)) |
|
88 | 2 | ); |
|
89 | 2 | } |
|
90 | 3 | ); |
|
91 | |||
92 | 3 | $taskRunner->getDispatcher()->addListener( |
|
93 | 3 | TaskRunnerEvents::TASKRUNNER_POST_EXECUTE_TASK, |
|
94 | function (GenericEvent $event) use ($self) { |
||
95 | 2 | $task = $event->getSubject(); |
|
96 | 2 | $t = $event->getArgument('index'); |
|
97 | 2 | $total = $event->getArgument('total'); |
|
98 | 2 | $result = $event->getArgument('result'); |
|
99 | |||
100 | 2 | if ($result instanceof ExecuteResult) { |
|
101 | $self->applyResultToFilelist($result); |
||
102 | } |
||
103 | |||
104 | 2 | $self->dispatch(BuilderEvents::BUILDER_POST_TASK, |
|
105 | 2 | new GenericEvent($task, array('index' => $t, 'total' => $total)) |
|
106 | 2 | ); |
|
107 | 2 | } |
|
108 | 3 | ); |
|
109 | |||
110 | 3 | return $taskRunner; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Add task to the build process |
||
115 | * |
||
116 | * @param Task $task |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function addTask(Task $task) |
||
125 | |||
126 | /** |
||
127 | * Return destination path for build |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 1 | public function getBuildDir() |
|
135 | |||
136 | /** |
||
137 | * @deprecated I rather not have the builder depend on the context, |
||
138 | * that's something for the BuildStage |
||
139 | * |
||
140 | * @param Context $context |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setContext(Context $context) |
||
149 | |||
150 | /** |
||
151 | * Run added tasks |
||
152 | * |
||
153 | * @param string $target |
||
154 | * @param Version $version |
||
155 | */ |
||
156 | 2 | public function build($target, Version $version) |
|
168 | |||
169 | protected function applyResultToFilelist(ExecuteResult $result) |
||
170 | { |
||
171 | $filesModified = $this->context->getFilesModified(); |
||
172 | |||
173 | foreach ($result->getDerived() as $pattern) { |
||
174 | $filesModified->add($pattern); |
||
175 | } |
||
176 | |||
177 | foreach ($result->getRemoved() as $pattern) { |
||
178 | $filesModified->remove($pattern); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Filters the tasks for given target |
||
184 | * |
||
185 | * @param string $target |
||
186 | * @param \Webcreate\Conveyor\Repository\Version $version |
||
187 | * @return Task[] task for the specific target |
||
188 | */ |
||
189 | 2 | protected function getSupportedTasks($target, Version $version) |
|
200 | |||
201 | /** |
||
202 | * Dispatch event when a dispatcher is available |
||
203 | * |
||
204 | * @param string $eventName |
||
205 | * @param Event $event |
||
206 | */ |
||
207 | 2 | protected function dispatch($eventName, Event $event = null) |
|
213 | } |
||
214 |