|
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\Doctrine\Finder; |
|
13
|
|
|
|
|
14
|
|
|
use Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
17
|
|
|
use Symfony\Component\Finder\Finder as SymfonyFinder; |
|
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Extends its parent class to take into account doctrine data loaders. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Théo FIDRY <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class FixturesFinder extends \Hautelook\AliceBundle\Finder\FixturesFinder implements ContainerAwareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ContainerInterface|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $container; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
168 |
|
public function setContainer(ContainerInterface $container = null) |
|
37
|
|
|
{ |
|
38
|
168 |
|
$this->container = $container; |
|
39
|
168 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
* |
|
44
|
|
|
* Extended to look for data loaders. If a data loader is found, will take the fixtures from it instead of taking |
|
45
|
|
|
* all the fixtures files. |
|
46
|
|
|
*/ |
|
47
|
117 |
|
public function getFixturesFromDirectory($path) |
|
48
|
|
|
{ |
|
49
|
117 |
|
$fixtures = []; |
|
50
|
|
|
|
|
51
|
117 |
|
$loaders = $this->getDataLoadersFromDirectory($path); |
|
52
|
114 |
|
foreach ($loaders as $loader) { |
|
53
|
96 |
|
$fixtures = array_merge($fixtures, $loader->getFixtures()); |
|
54
|
114 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
// If no data loader is found, takes all fixtures files |
|
57
|
114 |
|
if (0 === count($loaders)) { |
|
58
|
75 |
|
return parent::getFixturesFromDirectory($path); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
96 |
|
return $fixtures; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Gets all data loaders instances. |
|
66
|
|
|
* |
|
67
|
|
|
* For first get all the path for where to look for data loaders. |
|
68
|
|
|
* |
|
69
|
|
|
* @param BundleInterface[] $bundles |
|
70
|
|
|
* @param string $environment |
|
71
|
|
|
* |
|
72
|
|
|
* @return LoaderInterface[] Fixtures files real paths. |
|
73
|
|
|
*/ |
|
74
|
87 |
|
public function getDataLoaders(array $bundles, $environment) |
|
75
|
|
|
{ |
|
76
|
87 |
|
$loadersPaths = $this->getLoadersPaths($bundles, $environment); |
|
77
|
|
|
|
|
78
|
|
|
// Add all fixtures to the new Doctrine loader |
|
79
|
87 |
|
$loaders = []; |
|
80
|
87 |
|
foreach ($loadersPaths as $path) { |
|
81
|
87 |
|
if (is_dir($path)) { |
|
82
|
87 |
|
$loaders = array_merge($loaders, $this->getDataLoadersFromDirectory($path)); |
|
83
|
|
|
} |
|
84
|
87 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $loaders; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get data loaders inside the given directory. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $path Directory path |
|
93
|
|
|
* |
|
94
|
126 |
|
* @return LoaderInterface[] |
|
95
|
|
|
*/ |
|
96
|
126 |
|
private function getDataLoadersFromDirectory($path) |
|
97
|
|
|
{ |
|
98
|
|
|
$declaredClassesBeforeLoadingPhpFiles = get_declared_classes(); |
|
99
|
126 |
|
$includedFilesFromPath = $this->includePhpFiles($path, array_flip(get_included_files())); |
|
|
|
|
|
|
100
|
126 |
|
|
|
101
|
123 |
|
\Symfony\Component\VarDumper\VarDumper::dump(array_diff($declaredClassesBeforeLoadingPhpFiles, get_declared_classes()));die(); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
102 |
|
return $this->loadDataLoaders( |
|
|
|
|
|
|
104
|
102 |
|
array_diff($declaredClassesBeforeLoadingPhpFiles, get_declared_classes()), |
|
105
|
123 |
|
$includedFilesFromPath |
|
106
|
|
|
); |
|
107
|
123 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
123 |
|
* Includes all the PHP files present in a folder and returns the list of the included files. |
|
111
|
123 |
|
* |
|
112
|
123 |
|
* @param string $path |
|
113
|
|
|
* @param array $includedFiles Real files path as keys |
|
114
|
123 |
|
* |
|
115
|
102 |
|
* @return array Loaded real files path as keys |
|
116
|
102 |
|
*/ |
|
117
|
102 |
|
private function includePhpFiles($path, array $includedFiles) |
|
118
|
|
|
{ |
|
119
|
102 |
|
$includedFilesFromPath = []; |
|
120
|
102 |
|
$finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php'); |
|
121
|
102 |
|
foreach ($finder as $file) { |
|
122
|
120 |
|
/* @var SplFileInfo $file */ |
|
123
|
104 |
|
$includedFilesFromPath[$fileRealPath = $file->getRealPath()] = true; |
|
124
|
123 |
|
|
|
125
|
|
|
if (false === array_key_exists($fileRealPath, $includedFiles)) { |
|
126
|
123 |
|
require_once $fileRealPath; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $includedFilesFromPath; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Looks for loaders among the classes given. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $classNames |
|
137
|
|
|
* @param array $includedFilesFromPath Real files path as keys |
|
138
|
|
|
* |
|
139
|
|
|
* @return LoaderInterface[] |
|
140
|
|
|
*/ |
|
141
|
|
|
private function loadDataLoaders(array $classNames, array $includedFilesFromPath) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
$loaders = []; |
|
144
|
|
|
$loaderInterface = LoaderInterface::class; |
|
145
|
|
|
|
|
146
|
|
|
foreach ($classNames as $className) { |
|
147
|
|
|
$reflectionClass = new \ReflectionClass($className); |
|
148
|
|
|
$sourceFile = $reflectionClass->getFileName(); |
|
149
|
|
|
|
|
150
|
|
|
if (false === array_key_exists($sourceFile, $includedFilesFromPath)) { |
|
151
|
|
|
// The class does not come from the loaded directories |
|
152
|
|
|
continue; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ($reflectionClass->implementsInterface($loaderInterface) && false === $reflectionClass->isAbstract()) { |
|
156
|
|
|
$loader = new $className(); |
|
157
|
|
|
$loaders[$className] = $loader; |
|
158
|
|
|
|
|
159
|
|
|
if ($loader instanceof ContainerAwareInterface) { |
|
160
|
|
|
$loader->setContainer($this->container); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $loaders; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.