1 | <?php |
||
22 | class FixturesFinder implements FixturesFinderInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $bundleFixturesPath; |
||
28 | |||
29 | /** |
||
30 | * @param string $bundleFixturesPath Path in which fixtures files or loaders are expected to be found. |
||
31 | */ |
||
32 | 210 | public function __construct($bundleFixturesPath) |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 138 | public function getFixtures(KernelInterface $kernel, array $bundles, $environment) |
|
41 | { |
||
42 | 138 | $loadersPaths = $this->getLoadersPaths($bundles, $environment); |
|
43 | |||
44 | // Add all fixtures to the new Doctrine loader |
||
45 | 138 | $fixtures = []; |
|
46 | 138 | foreach ($loadersPaths as $path) { |
|
47 | 138 | $fixtures = array_merge($fixtures, $this->getFixturesFromDirectory($path)); |
|
48 | 135 | } |
|
49 | |||
50 | 135 | if (0 === count($fixtures)) { |
|
51 | throw new \InvalidArgumentException( |
||
52 | sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $loadersPaths)) |
||
53 | ); |
||
54 | } |
||
55 | |||
56 | // Get real fixtures path |
||
57 | // Note: Fixtures returned are guaranteed to be unique here |
||
58 | 135 | return $this->resolveFixtures($kernel, $fixtures); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 156 | public function resolveFixtures(KernelInterface $kernel, array $fixtures) |
|
65 | { |
||
66 | 156 | $resolvedFixtures = []; |
|
67 | |||
68 | // Get real fixtures path |
||
69 | 156 | foreach ($fixtures as $index => $fixture) { |
|
70 | 156 | if ($fixture instanceof \SplFileInfo) { |
|
71 | 18 | $filePath = $fixture->getRealPath(); |
|
72 | |||
73 | 18 | if (false === $filePath) { |
|
74 | throw new \RuntimeException( |
||
75 | sprintf( |
||
76 | 'The file %s pointed by a %s instance was not found.', |
||
77 | (string) $fixture, |
||
78 | get_class($fixture) |
||
79 | ) |
||
80 | ); |
||
81 | } |
||
82 | 18 | $fixture = $filePath; |
|
83 | 18 | } |
|
84 | |||
85 | 156 | if (false === is_string($fixture)) { |
|
86 | throw new \InvalidArgumentException( |
||
87 | 'Expected fixtures passed to be either strings or a SplFileInfo instances.' |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | 156 | if ('@' === $fixture[0]) { |
|
92 | // If $kernel fails to resolve the resource, will throw a \InvalidArgumentException exception |
||
93 | 51 | $realPath = $kernel->locateResource($fixture, null, true); |
|
94 | 51 | } else { |
|
95 | 144 | $realPath = realpath($fixture); |
|
96 | } |
||
97 | |||
98 | 156 | if (false === $realPath || false === file_exists($realPath)) { |
|
99 | throw new \InvalidArgumentException(sprintf('The file "%s" was not found', $fixture)); |
||
100 | } |
||
101 | |||
102 | 156 | if (false === is_file($realPath)) { |
|
103 | throw new \InvalidArgumentException( |
||
104 | sprintf('Expected "%s to be a fixture file, got a directory instead.', $fixture) |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | 156 | $resolvedFixtures[$realPath] = true; |
|
109 | 156 | } |
|
110 | |||
111 | 156 | return array_keys($resolvedFixtures); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 96 | public function getFixturesFromDirectory($path) |
|
129 | |||
130 | /** |
||
131 | * Gets paths to directories containing loaders and fixtures files. |
||
132 | * |
||
133 | * @param BundleInterface[] $bundles |
||
134 | * @param string $environment |
||
135 | * |
||
136 | * @return string[] Real paths to loaders. |
||
137 | */ |
||
138 | 147 | protected function getLoadersPaths(array $bundles, $environment) |
|
165 | } |
||
166 |