|
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
|
60 |
|
public function setContainer(ContainerInterface $container = null) |
|
37
|
|
|
{ |
|
38
|
60 |
|
$this->container = $container; |
|
39
|
60 |
|
} |
|
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
|
45 |
|
public function getFixturesFromDirectory($path) |
|
48
|
|
|
{ |
|
49
|
45 |
|
$fixtures = []; |
|
50
|
|
|
|
|
51
|
45 |
|
$loaders = $this->getDataLoadersFromDirectory($path); |
|
52
|
42 |
|
foreach ($loaders as $loader) { |
|
53
|
30 |
|
$fixtures = array_merge($fixtures, $loader->getFixtures()); |
|
54
|
42 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
// If no data loader is found, takes all fixtures files |
|
57
|
42 |
|
if (0 === count($loaders)) { |
|
58
|
33 |
|
return parent::getFixturesFromDirectory($path); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
30 |
|
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
|
15 |
|
public function getDataLoaders(array $bundles, $environment) |
|
75
|
|
|
{ |
|
76
|
15 |
|
$loadersPaths = $this->getLoadersPaths($bundles, $environment); |
|
77
|
|
|
|
|
78
|
|
|
// Add all fixtures to the new Doctrine loader |
|
79
|
15 |
|
$loaders = []; |
|
80
|
15 |
|
foreach ($loadersPaths as $path) { |
|
81
|
15 |
|
$loaders = array_merge($loaders, $this->getDataLoadersFromDirectory($path)); |
|
82
|
15 |
|
} |
|
83
|
|
|
|
|
84
|
15 |
|
return $loaders; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get data loaders inside the given directory. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $path Directory path |
|
91
|
|
|
* |
|
92
|
|
|
* @return LoaderInterface[] |
|
93
|
|
|
*/ |
|
94
|
54 |
|
private function getDataLoadersFromDirectory($path) |
|
95
|
|
|
{ |
|
96
|
54 |
|
$loaders = []; |
|
97
|
|
|
|
|
98
|
|
|
// Get all PHP classes in given folder |
|
99
|
54 |
|
$phpClasses = []; |
|
100
|
54 |
|
$finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php'); |
|
101
|
51 |
|
foreach ($finder as $file) { |
|
102
|
|
|
/* @var SplFileInfo $file */ |
|
103
|
36 |
|
$phpClasses[$file->getRealPath()] = true; |
|
104
|
36 |
|
require_once $file->getRealPath(); |
|
105
|
51 |
|
} |
|
106
|
|
|
|
|
107
|
51 |
|
$loaderInterface = 'Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface'; |
|
108
|
|
|
|
|
109
|
|
|
// Check if PHP classes are data loaders or not |
|
110
|
51 |
|
foreach (get_declared_classes() as $className) { |
|
111
|
51 |
|
$reflectionClass = new \ReflectionClass($className); |
|
112
|
51 |
|
$sourceFile = $reflectionClass->getFileName(); |
|
113
|
|
|
|
|
114
|
51 |
|
if (true === isset($phpClasses[$sourceFile])) { |
|
115
|
36 |
|
if ($reflectionClass->implementsInterface($loaderInterface) && !$reflectionClass->isAbstract()) { |
|
116
|
36 |
|
$loader = new $className(); |
|
117
|
36 |
|
$loaders[$className] = $loader; |
|
118
|
|
|
|
|
119
|
36 |
|
if ($loader instanceof ContainerAwareInterface) { |
|
120
|
36 |
|
$loader->setContainer($this->container); |
|
121
|
36 |
|
} |
|
122
|
48 |
|
} |
|
123
|
37 |
|
} |
|
124
|
51 |
|
} |
|
125
|
|
|
|
|
126
|
51 |
|
return $loaders; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|