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\Locator; |
13
|
|
|
|
14
|
|
|
use Hautelook\AliceBundle\FixtureLocatorInterface; |
15
|
|
|
use Nelmio\Alice\IsAServiceTrait; |
16
|
|
|
use Symfony\Component\Finder\Finder as SymfonyFinder; |
17
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
18
|
|
|
|
19
|
|
|
final class EnvDirectoryLocator implements FixtureLocatorInterface |
20
|
|
|
{ |
21
|
|
|
use IsAServiceTrait; |
22
|
|
|
|
23
|
|
|
private $fixturesPath; |
24
|
|
|
private $rootDirs; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $fixturePath Path to which to look for fixtures relative to the bundle/base directory paths. |
28
|
|
|
* @param string[] $rootDirs Root directories. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $fixturePath, array $rootDirs) |
31
|
|
|
{ |
32
|
|
|
$this->fixturesPath = $fixturePath; |
33
|
|
|
$this->rootDirs = $rootDirs; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Locates fixture files found inside a folder matching the environment name. |
38
|
|
|
* |
39
|
|
|
* For example, if the given fixture path is 'Resources/fixtures', it will try to locate |
40
|
|
|
* the files in the 'Resources/fixtures/dev' for each bundle passed ('dev' being the |
41
|
|
|
* environment). |
42
|
|
|
* |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function locateFiles(array $bundles, string $environment): array |
46
|
|
|
{ |
47
|
|
|
$fixtureFiles = array_merge( |
48
|
|
|
...array_map( |
49
|
|
|
function (string $rootDir) use ($environment): array { |
50
|
|
|
return $this->doLocateFiles($rootDir, $environment); |
51
|
|
|
}, |
52
|
|
|
$this->rootDirs |
53
|
|
|
), |
54
|
|
|
...array_map( |
55
|
|
|
function (BundleInterface $bundle) use ($environment): array { |
56
|
|
|
return $this->doLocateFiles($bundle->getPath(), $environment); |
57
|
|
|
}, |
58
|
|
|
$bundles |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $fixtureFiles; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets the list of files that can be found in the given path. |
67
|
|
|
* |
68
|
|
|
* @param string $path |
69
|
|
|
* @param string $environment |
70
|
|
|
* |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
|
private function doLocateFiles(string $path, string $environment): array |
74
|
|
|
{ |
75
|
|
|
$path = '' !== $environment |
76
|
|
|
? sprintf('%s/%s/%s', $path, $this->fixturesPath, $environment) |
77
|
|
|
: sprintf('%s/%s', $path, $this->fixturesPath) |
78
|
|
|
; |
79
|
|
|
|
80
|
|
|
$path = realpath($path); |
81
|
|
|
|
82
|
|
|
if (false === $path || false === file_exists($path)) { |
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$files = SymfonyFinder::create()->files()->in($path)->depth(0)->name('/.*\.(ya?ml|php)$/i'); |
87
|
|
|
|
88
|
|
|
// this sort helps to set an order with filename ( "001-root-level-fixtures.yml", "002-another-level-fixtures.yml", ... ) |
89
|
|
|
$files = $files->sort(function ($a, $b) { |
90
|
|
|
return strcasecmp($a, $b); |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
$fixtureFiles = []; |
94
|
|
|
foreach ($files as $file) { |
95
|
|
|
$fixtureFiles[$file->getRealPath()] = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return array_keys($fixtureFiles); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|