|
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
|
|
View Code Duplication |
final class EnvFilesLocator 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 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.yml' 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 = sprintf('%s/%s', $bundle->getPath(), $this->fixturesPath); |
|
61
|
|
|
if (false === file_exists($path)) { |
|
62
|
|
|
return []; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$pattern = sprintf('/.*\.%s(\..+)?\.(ya?ml|php)$/i', $environment); |
|
66
|
|
|
$files = SymfonyFinder::create()->files()->in($path)->name($pattern); |
|
67
|
|
|
$fixtureFiles = []; |
|
68
|
|
|
foreach ($files as $file) { |
|
69
|
|
|
$fixtureFiles[$file->getRealPath()] = true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return array_keys($fixtureFiles); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.