1 | <?php declare(strict_types = 1); |
||
10 | final class MongoFixturesLoader |
||
11 | { |
||
12 | /** @var array|MongoFixtureInterface[] */ |
||
13 | private $loadedClasses; |
||
14 | /** @var ContainerInterface */ |
||
15 | private $container; |
||
16 | |||
17 | /** |
||
18 | * MongoFixturesLoader constructor. |
||
19 | * |
||
20 | * @param ContainerInterface $container |
||
21 | */ |
||
22 | 2 | public function __construct(ContainerInterface $container) |
|
26 | |||
27 | /** |
||
28 | * @param string $dir |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | 1 | public function loadFromDirectory(string $dir) |
|
45 | |||
46 | /** |
||
47 | * @param \Iterator $iterator |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | 1 | private function loadFromIterator(\Iterator $iterator) |
|
52 | { |
||
53 | 1 | $includedFiles = array(); |
|
54 | 1 | foreach ($iterator as $file) { |
|
55 | 1 | if ($file->getBasename('.php') == $file->getBasename()) { |
|
56 | 1 | continue; |
|
57 | } |
||
58 | 1 | $sourceFile = realpath($file->getPathName()); |
|
59 | 1 | require_once $sourceFile; |
|
60 | 1 | $includedFiles[] = $sourceFile; |
|
61 | } |
||
62 | |||
63 | 1 | $declared = get_declared_classes(); |
|
64 | |||
65 | 1 | return array_reduce( |
|
66 | 1 | $declared, |
|
67 | 1 | function ($classList, string $className) use ($includedFiles) { |
|
68 | 1 | $reflClass = new \ReflectionClass($className); |
|
69 | 1 | $sourceFile = $reflClass->getFileName(); |
|
70 | |||
71 | if ( |
||
72 | 1 | in_array($sourceFile, $includedFiles) && |
|
73 | 1 | in_array(MongoFixtureInterface::class, array_keys($reflClass->getInterfaces())) |
|
74 | ) { |
||
75 | 1 | $instance = $this->buildFixture(new $className); |
|
76 | 1 | $this->addInstance($instance); |
|
77 | 1 | $classList[] = $instance; |
|
78 | } |
||
79 | |||
80 | 1 | return $classList; |
|
81 | 1 | }, |
|
82 | 1 | [] |
|
83 | ); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param mixed $instance |
||
88 | * |
||
89 | * @return MongoFixtureInterface |
||
90 | */ |
||
91 | 1 | private function buildFixture($instance): MongoFixtureInterface |
|
99 | |||
100 | /** |
||
101 | * @param MongoFixtureInterface $list |
||
102 | */ |
||
103 | 1 | public function addInstance(MongoFixtureInterface $list) |
|
111 | |||
112 | /** |
||
113 | * @param string $fileName |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function loadFromFile(string $fileName) |
||
127 | |||
128 | /** |
||
129 | * @return array|MongoFixtureInterface[] |
||
130 | */ |
||
131 | 2 | public function getLoadedClasses() |
|
135 | } |
||
136 |