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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Théo FIDRY <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class EnvDirectoryLocator implements FixtureLocatorInterface |
22
|
|
|
{ |
23
|
|
|
use IsAServiceTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $fixturesPath; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
private $projectDir; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $fixturePath Path to which to look for fixtures relative to the bundle path. |
37
|
|
|
* @param string|null $projectDir Path of the project directory. |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $fixturePath, string $projectDir = null) |
40
|
|
|
{ |
41
|
|
|
$this->fixturesPath = $fixturePath; |
42
|
|
|
$this->projectDir = $projectDir; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Locate fixture files found inside a folder matching the environment name. |
47
|
|
|
* |
48
|
|
|
* For example, if the given fixture path is 'Resources/fixtures', it will try to locate |
49
|
|
|
* the files in the 'Resources/fixtures/dev' for each bundle passed ('dev' being the |
50
|
|
|
* environment). |
51
|
|
|
* |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function locateFiles(array $bundles, string $environment): array |
55
|
|
|
{ |
56
|
|
|
$fixtureFiles = null === $this->projectDir ? [] : $this->doLocateFiles($this->projectDir, $environment); |
57
|
|
|
|
58
|
|
|
foreach ($bundles as $bundle) { |
59
|
|
|
//// ---$fixtureFiles = $fixtureFiles + $this->doLocateFiles($bundle->getPath(), $environment);--- |
60
|
|
|
// do not use "plus" operator: |
61
|
|
|
// "... for keys that exist in both arrays, the elements from the left-hand array will be used, |
62
|
|
|
// and the matching elements from the right-hand array will be IGNORED." |
63
|
|
|
// |
64
|
|
|
// $bundle1Files = ['bundle1/001-A.php', 'bundle1/001-B.php', 'bundle1/001-C.php']; |
|
|
|
|
65
|
|
|
// $bundle2Files = ['bundle2/001-A.php', 'bundle2/001-B.php', 'bundle2/001-C.php', 'bundle2/001-D.php']; |
|
|
|
|
66
|
|
|
// ---------- |
67
|
|
|
// var_dump($bundle1Files + $bundle2Files); |
|
|
|
|
68
|
|
|
// > 4 elements: 3 from bundle1 + 1 from bundle2 |
69
|
|
|
// |
70
|
|
|
// var_dump(array_merge($bundle1Files, $bundle2Files)); |
|
|
|
|
71
|
|
|
// > 7 elements: 3 from bundle1 + 4 from bundle2 |
72
|
|
|
$fixtureFiles = array_merge($fixtureFiles, $this->doLocateFiles($bundle->getPath(), $environment)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $fixtureFiles; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function doLocateFiles(string $path, string $environment): array |
79
|
|
|
{ |
80
|
|
|
$path = '' !== $environment |
81
|
|
|
? sprintf('%s/%s/%s', $path, $this->fixturesPath, $environment) |
82
|
|
|
: sprintf('%s/%s', $path, $this->fixturesPath) |
83
|
|
|
; |
84
|
|
|
$path = realpath($path); |
85
|
|
|
if (false === $path || false === file_exists($path)) { |
86
|
|
|
return []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$files = SymfonyFinder::create()->files()->in($path)->depth(0)->name('/.*\.(ya?ml|php)$/i'); |
90
|
|
|
|
91
|
|
|
// this sort helps to set an order with filename ( "001-root-level-fixtures.yml", "002-another-level-fixtures.yml", ... ) |
92
|
|
|
$files = $files->sort( function ($a, $b) { return strcasecmp($a, $b); } ); |
93
|
|
|
|
94
|
|
|
$fixtureFiles = []; |
95
|
|
|
foreach ($files as $file) { |
96
|
|
|
$fixtureFiles[$file->getRealPath()] = true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return array_keys($fixtureFiles); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.