|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Hautelook\AliceBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Baldur Rensch <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Hautelook\AliceBundle\Finder; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\Finder as SymfonyFinder; |
|
15
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Théo FIDRY <[email protected]> |
|
21
|
|
|
*/ |
|
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
|
102 |
|
public function __construct($bundleFixturesPath) |
|
33
|
|
|
{ |
|
34
|
102 |
|
$this->bundleFixturesPath = $bundleFixturesPath; |
|
35
|
102 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
66 |
|
public function getFixtures(KernelInterface $kernel, array $bundles, $environment) |
|
41
|
|
|
{ |
|
42
|
66 |
|
$loadersPaths = $this->getLoadersPaths($bundles, $environment); |
|
43
|
|
|
|
|
44
|
|
|
// Add all fixtures to the new Doctrine loader |
|
45
|
66 |
|
$fixtures = []; |
|
46
|
66 |
|
foreach ($loadersPaths as $path) { |
|
47
|
66 |
|
$fixtures = array_merge($fixtures, $this->getFixturesFromDirectory($path)); |
|
48
|
63 |
|
} |
|
49
|
|
|
|
|
50
|
63 |
|
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
|
63 |
|
return $this->resolveFixtures($kernel, $fixtures); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
84 |
|
public function resolveFixtures(KernelInterface $kernel, array $fixtures) |
|
65
|
|
|
{ |
|
66
|
84 |
|
$resolvedFixtures = []; |
|
67
|
|
|
|
|
68
|
|
|
// Get real fixtures path |
|
69
|
84 |
|
foreach ($fixtures as $index => $fixture) { |
|
70
|
84 |
|
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
|
84 |
|
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
|
84 |
|
if ('@' === $fixture[0]) { |
|
92
|
|
|
// If $kernel fails to resolve the resource, will throw a \InvalidArgumentException exception |
|
93
|
21 |
|
$realPath = $kernel->locateResource($fixture, null, true); |
|
94
|
21 |
|
} else { |
|
95
|
78 |
|
$realPath = realpath($fixture); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
84 |
|
if (false === $realPath || false === file_exists($realPath)) { |
|
99
|
|
|
throw new \InvalidArgumentException(sprintf('The file "%s" was not found', $fixture)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
84 |
|
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
|
84 |
|
$resolvedFixtures[$realPath] = true; |
|
109
|
84 |
|
} |
|
110
|
|
|
|
|
111
|
84 |
|
return array_keys($resolvedFixtures); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
54 |
|
public function getFixturesFromDirectory($path) |
|
118
|
|
|
{ |
|
119
|
54 |
|
$fixtures = []; |
|
120
|
|
|
|
|
121
|
54 |
|
$finder = SymfonyFinder::create()->in($path)->depth(0)->files()->name('*.yml'); |
|
122
|
54 |
|
foreach ($finder as $file) { |
|
123
|
|
|
/* @var SplFileInfo $file */ |
|
124
|
54 |
|
$fixtures[$file->getRealPath()] = true; |
|
125
|
54 |
|
} |
|
126
|
|
|
|
|
127
|
54 |
|
return array_keys($fixtures); |
|
128
|
|
|
} |
|
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
|
75 |
|
protected function getLoadersPaths(array $bundles, $environment) |
|
139
|
|
|
{ |
|
140
|
|
|
$environments = [ |
|
141
|
75 |
|
lcfirst($environment) => true, |
|
142
|
75 |
|
ucfirst($environment) => true, |
|
143
|
75 |
|
]; |
|
144
|
|
|
|
|
145
|
75 |
|
$paths = []; |
|
146
|
75 |
|
foreach ($bundles as $bundle) { |
|
147
|
75 |
|
$path = sprintf('%s/%s', $bundle->getPath(), $this->bundleFixturesPath); |
|
148
|
75 |
|
if (true === file_exists($path)) { |
|
149
|
75 |
|
$paths[$path] = true; |
|
150
|
|
|
try { |
|
151
|
75 |
|
$files = SymfonyFinder::create()->directories()->in($path); |
|
152
|
72 |
|
foreach ($files as $file) { |
|
153
|
|
|
/** @var SplFileInfo $file */ |
|
154
|
51 |
|
if (true === isset($environments[$file->getRelativePathname()])) { |
|
155
|
45 |
|
$paths[$file->getRealPath()] = true; |
|
156
|
45 |
|
} |
|
157
|
72 |
|
} |
|
158
|
75 |
|
} catch (\InvalidArgumentException $exception) { |
|
159
|
|
|
} |
|
160
|
75 |
|
} |
|
161
|
75 |
|
} |
|
162
|
|
|
|
|
163
|
75 |
|
return array_keys($paths); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|