|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\DataFixture; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Data\DataFixtureManager; |
|
8
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
|
9
|
|
|
use Doctrine\Common\DataFixtures\FixtureInterface; |
|
10
|
|
|
use Doctrine\Common\DataFixtures\Loader; |
|
11
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
|
12
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
|
13
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Alex Patterson <[email protected]> |
|
18
|
|
|
* @package Arp\LaminasDoctrine\Factory\Service |
|
19
|
|
|
*/ |
|
20
|
|
|
final class LoaderFactory extends AbstractFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param ContainerInterface $container |
|
24
|
|
|
* @param string $requestedName |
|
25
|
|
|
* @param array<string, mixed>|null $options |
|
26
|
|
|
* |
|
27
|
|
|
* @return Loader |
|
28
|
|
|
* |
|
29
|
|
|
* @throws ServiceNotCreatedException |
|
30
|
|
|
* @throws ServiceNotFoundException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): Loader |
|
33
|
|
|
{ |
|
34
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName); |
|
35
|
|
|
|
|
36
|
|
|
$loader = new Loader(); |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
if (!empty($options['directories'])) { |
|
40
|
|
|
foreach ($options['directories'] as $directory) { |
|
41
|
|
|
$loader->loadFromDirectory($directory); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!empty($options['fixtures'])) { |
|
46
|
|
|
foreach ($this->getFixtures($container, $options['fixtures'], $requestedName) as $fixture) { |
|
47
|
|
|
$loader->addFixture($fixture); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} catch (ContainerExceptionInterface $e) { |
|
51
|
|
|
throw $e; |
|
52
|
|
|
} catch (\Throwable $e) { |
|
53
|
|
|
throw new ServiceNotCreatedException( |
|
54
|
|
|
sprintf('Failed to load doctrine data fixtures: %s', $e->getMessage()), |
|
55
|
|
|
$e->getCode(), |
|
56
|
|
|
$e |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $loader; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ContainerInterface $container |
|
65
|
|
|
* @param array<string|FixtureInterface> $fixtures |
|
66
|
|
|
* @param string $requestedName |
|
67
|
|
|
* |
|
68
|
|
|
* @return array<FixtureInterface> |
|
69
|
|
|
* |
|
70
|
|
|
* @throws ServiceNotCreatedException |
|
71
|
|
|
* @throws ServiceNotFoundException |
|
72
|
|
|
*/ |
|
73
|
|
|
private function getFixtures(ContainerInterface $container, array $fixtures, string $requestedName): array |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var DataFixtureManager $dataFixtureManager */ |
|
76
|
|
|
$dataFixtureManager = $this->getService($container, DataFixtureManager::class, $requestedName); |
|
77
|
|
|
$fixtureObjects = []; |
|
78
|
|
|
|
|
79
|
|
|
foreach ($fixtures as $fixture) { |
|
80
|
|
|
if (is_string($fixture)) { |
|
81
|
|
|
$fixture = $this->getService($dataFixtureManager, $fixture, $requestedName); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!$fixture instanceof FixtureInterface) { |
|
85
|
|
|
throw new ServiceNotCreatedException( |
|
86
|
|
|
sprintf( |
|
87
|
|
|
'The data fixture must be an object of type \'%s\'; \'%s\' provided in \'%s\'', |
|
88
|
|
|
FixtureInterface::class, |
|
89
|
|
|
(is_object($fixture) ? get_class($fixture) : gettype($fixture)), |
|
90
|
|
|
$requestedName |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$fixtureObjects[] = $fixture; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $fixtureObjects; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|