-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader.php
127 lines (102 loc) · 3.72 KB
/
Loader.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace Kununu\DataFixtures\Loader;
use ArrayIterator;
use InvalidArgumentException;
use Iterator;
use Kununu\DataFixtures\FixtureInterface;
use Kununu\DataFixtures\InitializableFixtureInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use SplFileInfo;
abstract class Loader implements LoaderInterface
{
private const string FILE_EXTENSION = '.php';
private array $fixtures = [];
private array $initalizableFixtures = [];
final public function loadFromDirectory(string $dir): void
{
if (!is_dir($dir)) {
throw new InvalidArgumentException(sprintf('"%s" does not exist', $dir));
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY
);
$this->loadFromIterator($iterator);
}
final public function loadFromFile(string $fileName): void
{
if (!is_readable($fileName)) {
throw new InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName));
}
$this->loadFromIterator(new ArrayIterator([new SplFileInfo($fileName)]));
}
final public function loadFromClassName(string $className): void
{
$reflClass = new ReflectionClass($className);
$this->loadFromIterator(new ArrayIterator([new SplFileInfo($reflClass->getFileName())]));
}
final public function getFixture(string $className): FixtureInterface
{
if (!isset($this->fixtures[$className])) {
throw new InvalidArgumentException(sprintf('"%s" is not a registered fixture', $className));
}
return $this->fixtures[$className];
}
final public function addFixture(FixtureInterface $fixture): void
{
if (!isset($this->fixtures[$fixtureClass = $fixture::class])) {
$this->fixtures[$fixtureClass] = $fixture;
}
}
final public function getFixtures(): array
{
return $this->fixtures;
}
final public function registerInitializableFixture(string $className, mixed ...$args): void
{
if (!isset($this->initalizableFixtures[$className])) {
$this->initalizableFixtures[$className] = $args;
}
}
final public function clearFixtures(): void
{
$this->fixtures = [];
}
abstract protected function supports(string $className): bool;
private function createFixture(string $className): FixtureInterface
{
$class = new $className();
if (isset($this->initalizableFixtures[$className])
&& in_array(InitializableFixtureInterface::class, class_implements($class))
) {
$class->initializeFixture(...$this->initalizableFixtures[$className]);
}
return $class;
}
private function loadFromIterator(Iterator $iterator): void
{
$includedFiles = [];
foreach ($iterator as $file) {
if ($file->getBasename(self::FILE_EXTENSION) == $file->getBasename()) {
continue;
}
$sourceFile = realpath($file->getPathName());
require_once $sourceFile;
$includedFiles[] = $sourceFile;
}
$declared = get_declared_classes();
// Make the declared classes order deterministic
sort($declared);
foreach ($declared as $className) {
$reflClass = new ReflectionClass($className);
$sourceFile = $reflClass->getFileName();
if (in_array($sourceFile, $includedFiles) && $this->supports($className)) {
$fixture = $this->createFixture($className);
$this->addFixture($fixture);
}
}
}
}