1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1);
|
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrineFixtures\Factory\Service;
|
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\AbstractFactory;
|
8
|
|
|
use Arp\LaminasFactory\Exception\ServiceNotCreatedException;
|
9
|
|
|
use Doctrine\Common\DataFixtures\FixtureInterface;
|
10
|
|
|
use Doctrine\Common\DataFixtures\Loader;
|
11
|
|
|
use Interop\Container\ContainerInterface;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* @author Alex Patterson <[email protected]>
|
15
|
|
|
* @package Arp\LaminasDoctrineFixtures\Factory\Service
|
16
|
|
|
*/
|
17
|
|
|
final class LoaderFactory extends AbstractFactory
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* @param ContainerInterface $container
|
21
|
|
|
* @param string $requestedName
|
22
|
|
|
* @param array|null $options
|
23
|
|
|
*
|
24
|
|
|
* @return Loader
|
25
|
|
|
*/
|
26
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
27
|
|
|
{
|
28
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName);
|
29
|
|
|
|
30
|
|
|
$loader = new Loader();
|
31
|
|
|
|
32
|
|
|
if (! empty($options['directories'])) {
|
33
|
|
|
foreach ($options['directories'] as $directory) {
|
34
|
|
|
$loader->loadFromDirectory($directory);
|
35
|
|
|
}
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
if (! empty($options['fixtures'])) {
|
39
|
|
|
foreach ($this->getFixtures($container, $options['fixtures'], $requestedName) as $fixture) {
|
40
|
|
|
$loader->addFixture($fixture);
|
41
|
|
|
}
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
return $loader;
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* Create the provided fixtures.
|
49
|
|
|
*
|
50
|
|
|
* @param ContainerInterface $container
|
51
|
|
|
* @param array $fixtures
|
52
|
|
|
* @param string $requestedName
|
53
|
|
|
*
|
54
|
|
|
* @return FixtureInterface[]
|
55
|
|
|
*
|
56
|
|
|
* @throws ServiceNotCreatedException If a fixture cannot be created
|
57
|
|
|
*/
|
58
|
|
|
private function getFixtures(ContainerInterface $container, array $fixtures, string $requestedName): array
|
59
|
|
|
{
|
60
|
|
|
$fixtureObjects = [];
|
61
|
|
|
|
62
|
|
|
foreach ($fixtures as $fixture) {
|
63
|
|
|
if (is_string($fixture)) {
|
64
|
|
|
if (!$container->has($fixture)) {
|
65
|
|
|
throw new ServiceNotCreatedException(
|
66
|
|
|
sprintf('The data fixture \'%s\' could not be found registered', $fixture)
|
67
|
|
|
);
|
68
|
|
|
}
|
69
|
|
|
$fixture = $container->get($fixture);
|
70
|
|
|
}
|
71
|
|
|
if (! $fixture instanceof FixtureInterface) {
|
72
|
|
|
throw new ServiceNotCreatedException(
|
73
|
|
|
sprintf(
|
74
|
|
|
'The data fixture must be an object of type \'%s\'; \'%s\' provided in \'%s\'',
|
75
|
|
|
FixtureInterface::class,
|
76
|
|
|
(is_object($fixture) ? get_class($fixture) : gettype($fixture)),
|
77
|
|
|
$requestedName
|
78
|
|
|
)
|
79
|
|
|
);
|
80
|
|
|
}
|
81
|
|
|
$fixtureObjects[] = $fixture;
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
return $fixtureObjects;
|
85
|
|
|
}
|
86
|
|
|
}
|
87
|
|
|
|