1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\Fixtures; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MongoFixturesLoader. |
9
|
|
|
*/ |
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) |
23
|
|
|
{ |
24
|
2 |
|
$this->container = $container; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $dir |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
1 |
|
public function loadFromDirectory(string $dir) |
33
|
|
|
{ |
34
|
1 |
|
if (!is_dir($dir)) { |
35
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir)); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$iterator = new \RecursiveIteratorIterator( |
39
|
1 |
|
new \RecursiveDirectoryIterator($dir), |
40
|
1 |
|
\RecursiveIteratorIterator::LEAVES_ONLY |
41
|
|
|
); |
42
|
|
|
|
43
|
1 |
|
return $this->loadFromIterator($iterator); |
44
|
|
|
} |
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 |
92
|
|
|
{ |
93
|
1 |
|
if ($instance instanceof AbstractContainerAwareFixture) { |
94
|
1 |
|
$instance->setContainer($this->container); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return $instance; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param MongoFixtureInterface $list |
102
|
|
|
*/ |
103
|
1 |
|
public function addInstance(MongoFixtureInterface $list) |
104
|
|
|
{ |
105
|
1 |
|
$listClass = get_class($list); |
106
|
|
|
|
107
|
1 |
|
if (!isset($this->loadedClasses[$listClass])) { |
108
|
1 |
|
$this->loadedClasses[$listClass] = $list; |
109
|
|
|
} |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $fileName |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function loadFromFile(string $fileName) |
118
|
|
|
{ |
119
|
|
|
if (!is_readable($fileName)) { |
120
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$iterator = new \ArrayIterator(array(new \SplFileInfo($fileName))); |
124
|
|
|
|
125
|
|
|
return $this->loadFromIterator($iterator); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array|MongoFixtureInterface[] |
130
|
|
|
*/ |
131
|
2 |
|
public function getLoadedClasses() |
132
|
|
|
{ |
133
|
2 |
|
return $this->loadedClasses; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|