|
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\NotClonableTrait; |
|
16
|
|
|
use Symfony\Component\Finder\Finder as SymfonyFinder; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Théo FIDRY <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class EnvDirectoryLocator implements FixtureLocatorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use NotClonableTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $fixturesPath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $fixturePath Path to which to look for fixtures relative to the bundle path. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(string $fixturePath) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->fixturesPath = $fixturePath; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Locate fixture files found inside a folder matching the environment name. |
|
41
|
|
|
* |
|
42
|
|
|
* For example, if the given fixture path is 'Resources/fixtures', it will try to locate |
|
43
|
|
|
* the files in the 'Resources/fixtures/dev' for each bundle passed ('dev' being the |
|
44
|
|
|
* environment). |
|
45
|
|
|
* |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function locateFiles(array $bundles, string $environment): array |
|
49
|
|
|
{ |
|
50
|
|
|
$fixtureFiles = []; |
|
51
|
|
|
foreach ($bundles as $bundle) { |
|
52
|
|
|
$fixtureFiles = $fixtureFiles + $this->locateBundleFiles($bundle, $environment); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $fixtureFiles; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function locateBundleFiles(BundleInterface $bundle, string $environment): array |
|
59
|
|
|
{ |
|
60
|
|
|
$path = '' !== $environment |
|
61
|
|
|
? sprintf('%s/%s/%s', $bundle->getPath(), $this->fixturesPath, $environment) |
|
62
|
|
|
: sprintf('%s/%s', $bundle->getPath(), $this->fixturesPath) |
|
63
|
|
|
; |
|
64
|
|
|
$path = realpath($path); |
|
65
|
|
|
if (false === $path || false === file_exists($path)) { |
|
66
|
|
|
return []; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$files = SymfonyFinder::create()->files()->in($path)->depth(0)->name('/.*\.(ya?ml|php)$/i'); |
|
70
|
|
|
$fixtureFiles = []; |
|
71
|
|
|
foreach ($files as $file) { |
|
72
|
|
|
$fixtureFiles[$file->getRealPath()] = true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return array_keys($fixtureFiles); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|