-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependentExecutableIterator.php
More file actions
100 lines (88 loc) · 2.88 KB
/
DependentExecutableIterator.php
File metadata and controls
100 lines (88 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace FDevs\Executor;
use FDevs\Container\ServiceLocator;
use FDevs\Executor\Exception\CircularDependencyException;
use FDevs\Executor\Exception\ExecutableNotFoundException;
use FDevs\Executor\Exception\RuntimeException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class DependentExecutableIterator extends ServiceLocator implements DependentExecutableIteratorInterface
{
/**
* @var string[]
*/
private $executableIds;
/**
* @var string[]
*/
private $performedExecutables = [];
/**
* ExecutableIterator constructor.
*
* @param callable[] $factories
*/
public function __construct(array $factories = [])
{
parent::__construct($factories);
$this->executableIds = \array_keys($factories);
}
/**
* {@inheritdoc}
*/
public function getDependenciesIterator(array $executableIds = []): \Iterator
{
$this->performedExecutables = [];
if (empty($executableIds)) {
$executableIds = $this->executableIds;
}
foreach ($executableIds as $id) {
yield from $this->performExecutable($id);
}
}
/**
* @param string $id Identifier of FDevs\Executor\ExecutableInterface
* @param string[] $dependencyTrace Array of dependent identifiers [`id` => true]
*
* @throws CircularDependencyException
* @throws ExecutableNotFoundException
* @throws RuntimeException
*
* @return \Iterator Iterator of FDevs\Fixture\Fixture\FixtureInterface
*/
private function performExecutable(string $id, array $dependencyTrace = []): \Iterator
{
if (!isset($this->performedExecutables[$id])) {
if (isset($dependencyTrace[$id])) {
$trace = \array_keys($dependencyTrace);
throw new CircularDependencyException($trace);
}
$fixture = $this->getExecutable($id);
if ($fixture instanceof DependentExecutableInterface) {
$dependencyTrace[$id] = true;
foreach ($fixture->getDependencies() as $dependencyId) {
yield from $this->performExecutable($dependencyId, $dependencyTrace);
}
}
$this->performedExecutables[$id] = true;
yield $fixture;
}
}
/**
* @param string $id
*
* @throws ExecutableNotFoundException
* @throws RuntimeException
*
* @return ExecutableInterface
*/
private function getExecutable(string $id): ExecutableInterface
{
try {
return $this->get($id);
} catch (NotFoundExceptionInterface $e) {
throw new ExecutableNotFoundException($e->getMessage());
} catch (ContainerExceptionInterface $e) {
throw new RuntimeException($e->getMessage());
}
}
}