1 | <?php |
||
10 | class TaskRunner |
||
11 | { |
||
12 | /** |
||
13 | * @var FilterInterface[] |
||
14 | */ |
||
15 | protected $filters; |
||
16 | |||
17 | /** |
||
18 | * @var Locator |
||
19 | */ |
||
20 | protected $locator; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $isDebug; |
||
26 | |||
27 | /** |
||
28 | * TaskRunner constructor. |
||
29 | * |
||
30 | * @param FilterInterface[] $filters |
||
31 | * @param Locator $locator |
||
32 | * @param bool $isDebug |
||
33 | */ |
||
34 | 1 | public function __construct(array $filters, Locator $locator, $isDebug = false) |
|
40 | |||
41 | /** |
||
42 | * Run a task, load its sources before and call the clean method on the filter. |
||
43 | * |
||
44 | * @param Task $task |
||
45 | * @throws Exception |
||
46 | */ |
||
47 | 1 | public function run(Task $task) |
|
48 | { |
||
49 | // get configured filters for this task |
||
50 | $filters = $task |
||
51 | 1 | ->getConfiguration() |
|
52 | 1 | ->getParameter('filters'); |
|
53 | |||
54 | // get sources files |
||
55 | 1 | $sources = $this->fetchSources($task); |
|
56 | 1 | $destinations = $this->fetchDestinations($task); |
|
57 | |||
58 | 1 | foreach ($filters as $filterName) { |
|
59 | // get current configured filter |
||
60 | 1 | $filter = $this->getFilter($filterName); |
|
61 | |||
62 | // filter the files supported by this filter |
||
63 | 1 | $filteredSources = $this->filterSources($sources, $filter); |
|
64 | |||
65 | // apply the filter |
||
66 | 1 | $updatedSources = $filter->run($filteredSources, $destinations); |
|
67 | |||
68 | // update new sources if exists |
||
69 | 1 | if ($updatedSources === null) { |
|
70 | 1 | $updatedSources = []; |
|
71 | } |
||
72 | 1 | $sources = $this->updateSources($sources, $filteredSources, $updatedSources); |
|
73 | } |
||
74 | |||
75 | 1 | if (!$this->isDebug) { |
|
76 | 1 | foreach ($filters as $filterName) { |
|
77 | // get current configured filter |
||
78 | 1 | $filter = $this->getFilter($filterName); |
|
79 | |||
80 | // clean the files generated by the filter |
||
81 | 1 | $filter->clean(); |
|
82 | } |
||
83 | } |
||
84 | 1 | } |
|
85 | |||
86 | /** |
||
87 | * Return a filter by its name. Throw an exception if it is not exists. |
||
88 | * |
||
89 | * @param string $name |
||
90 | * @return FilterInterface |
||
91 | * @throws Exception |
||
92 | */ |
||
93 | 1 | protected function getFilter($name) |
|
102 | |||
103 | /** |
||
104 | * Fetch the source files from the task and return and array of SplInfo. |
||
105 | * |
||
106 | * @param Task $task |
||
107 | * @return array |
||
108 | */ |
||
109 | 1 | protected function fetchSources(Task $task) |
|
120 | |||
121 | /** |
||
122 | * Fetch the destination files from the task and return and array of SplInfo. |
||
123 | * |
||
124 | * @param Task $task |
||
125 | * @return SplFileInfo[] |
||
126 | */ |
||
127 | 1 | protected function fetchDestinations(Task $task) |
|
138 | |||
139 | /** |
||
140 | * Filter only the sources supported by the current filter. |
||
141 | * |
||
142 | * @param SplFileInfo[] $sources |
||
143 | * @param FilterInterface $filter |
||
144 | * @return array |
||
145 | * @throws Exception |
||
146 | */ |
||
147 | 1 | protected function filterSources(array $sources, FilterInterface $filter) |
|
170 | |||
171 | /** |
||
172 | * Remove the filtered files from the sources, and merge with the new ones. |
||
173 | * |
||
174 | * @param SplFileInfo[] $originalSources |
||
175 | * @param SplFileInfo[] $filteredSources |
||
176 | * @param SplFileInfo[] $updatedSources |
||
177 | * @return SplFileInfo[] |
||
178 | * @throws Exception |
||
179 | */ |
||
180 | 1 | protected function updateSources(array $originalSources, array $filteredSources, array $updatedSources) |
|
209 | } |
||
210 |