1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Fixtures; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MongoFixturesLoader. |
11
|
|
|
*/ |
12
|
|
|
class MongoFixturesLoader |
13
|
|
|
{ |
14
|
|
|
/** @var array|MongoFixtureInterface[] */ |
15
|
|
|
private $loadedClasses; |
16
|
|
|
|
17
|
|
|
/** @var ContainerInterface */ |
18
|
|
|
private $container; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* MongoFixturesLoader constructor. |
22
|
|
|
* |
23
|
|
|
* @param ContainerInterface $container |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ContainerInterface $container) |
26
|
|
|
{ |
27
|
|
|
$this->container = $container; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $dir |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function loadFromDirectory(string $dir) |
36
|
|
|
{ |
37
|
|
|
if (!is_dir($dir)) { |
38
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$iterator = new \RecursiveIteratorIterator( |
42
|
|
|
new \RecursiveDirectoryIterator($dir), |
43
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY |
44
|
|
|
); |
45
|
|
|
return $this->loadFromIterator($iterator); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $fileName |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function loadFromFile(string $fileName) |
54
|
|
|
{ |
55
|
|
|
if (!is_readable($fileName)) { |
56
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$iterator = new \ArrayIterator(array(new \SplFileInfo($fileName))); |
60
|
|
|
return $this->loadFromIterator($iterator); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \Iterator $iterator |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
private function loadFromIterator(\Iterator $iterator) |
69
|
|
|
{ |
70
|
|
|
$includedFiles = array(); |
71
|
|
|
foreach ($iterator as $file) { |
72
|
|
|
if (($fileName = $file->getBasename('.php')) == $file->getBasename()) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
$sourceFile = realpath($file->getPathName()); |
76
|
|
|
require_once $sourceFile; |
77
|
|
|
$includedFiles[] = $sourceFile; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$classes = array(); |
81
|
|
|
$declared = get_declared_classes(); |
82
|
|
|
foreach ($declared as $className) { |
83
|
|
|
$reflClass = new \ReflectionClass($className); |
84
|
|
|
$sourceFile = $reflClass->getFileName(); |
85
|
|
|
|
86
|
|
|
if (in_array($sourceFile, $includedFiles)) { |
87
|
|
|
$classes[] = $this->buildFixture($className); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $classes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param MongoFixtureInterface $list |
96
|
|
|
*/ |
97
|
|
|
public function addInstance(MongoFixtureInterface $list) |
98
|
|
|
{ |
99
|
|
|
$listClass = get_class($list); |
100
|
|
|
|
101
|
|
|
if (!isset($this->loadedClasses[$listClass])) { |
102
|
|
|
$this->loadedClasses[$listClass] = $list; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array|MongoFixtureInterface[] |
108
|
|
|
*/ |
109
|
|
|
public function getLoadedClasses() |
110
|
|
|
{ |
111
|
|
|
return $this->loadedClasses; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $className |
116
|
|
|
* |
117
|
|
|
* @return MongoFixtureInterface |
118
|
|
|
*/ |
119
|
|
|
private function buildFixture($className): MongoFixtureInterface |
120
|
|
|
{ |
121
|
|
|
$instance = new $className; |
122
|
|
|
|
123
|
|
|
if ($instance instanceof AbstractContainerAwareFixture) { |
124
|
|
|
$instance->setContainer($this->container); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$classes[] = $instance; |
|
|
|
|
128
|
|
|
$this->addInstance($instance); |
129
|
|
|
|
130
|
|
|
return $instance; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.