|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Fixtures\MongoFixturesLoader; |
|
6
|
|
|
use Facile\MongoDbBundle\Fixtures\MongoFixtureInterface; |
|
7
|
|
|
use Facile\MongoDbBundle\Fixtures\OrderedFixtureInterface; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class LoadFixturesCommand. |
|
17
|
|
|
*/ |
|
18
|
|
|
class LoadFixturesCommand extends AbstractCommand |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var MongoFixturesLoader */ |
|
21
|
|
|
private $loader; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
8 |
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
8 |
|
parent::configure(); |
|
29
|
|
|
$this |
|
30
|
8 |
|
->setName('mongodb:fixtures:load') |
|
31
|
8 |
|
->addArgument('addFixturesPath', InputArgument::OPTIONAL, 'Add a path to search in for fixtures files') |
|
32
|
8 |
|
->setDescription('Load fixtures and applies them'); |
|
33
|
|
|
; |
|
34
|
8 |
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
37
|
|
|
{ |
|
38
|
3 |
|
parent::initialize($input, $output); |
|
39
|
3 |
|
$this->loader = new MongoFixturesLoader($this->getContainer()); |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->io->writeln('Loading mongo fixtures'); |
|
48
|
|
|
/** @var Application $application */ |
|
49
|
3 |
|
$application = $this->getApplication(); |
|
50
|
|
|
|
|
51
|
3 |
|
$paths = $this->prepareSearchPaths($input, $application->getKernel()); |
|
52
|
|
|
|
|
53
|
3 |
|
$this->loadPaths($paths); |
|
54
|
|
|
|
|
55
|
3 |
|
$fixtures = $this->loader->getLoadedClasses(); |
|
56
|
3 |
|
if (empty($fixtures)) { |
|
57
|
1 |
|
throw new \InvalidArgumentException( |
|
58
|
1 |
|
sprintf('Could not find any class to load in: %s', "\n\n- ".implode("\n- ", $paths)) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
$this->sortFixtures($fixtures); |
|
63
|
|
|
|
|
64
|
2 |
|
foreach ($fixtures as $fixture) { |
|
65
|
2 |
|
$this->loadFixture($fixture); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
$this->io->writeln(sprintf('Done, loaded %d fixtures files', \count($fixtures))); |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param MongoFixtureInterface $indexList |
|
73
|
|
|
*/ |
|
74
|
2 |
|
private function loadFixture(MongoFixtureInterface $indexList) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$indexList->loadData(); |
|
77
|
2 |
|
$indexList->loadIndexes(); |
|
78
|
2 |
|
$this->io->writeln('Loaded fixture: '. \get_class($indexList)); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param InputInterface $input |
|
83
|
|
|
* @param KernelInterface $kernel |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
3 |
|
protected function prepareSearchPaths(InputInterface $input, KernelInterface $kernel): array |
|
88
|
|
|
{ |
|
89
|
3 |
|
$paths = []; |
|
90
|
|
|
|
|
91
|
3 |
|
if ($input->getArgument('addFixturesPath')) { |
|
92
|
2 |
|
$paths[] = $input->getArgument('addFixturesPath'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
foreach ($kernel->getBundles() as $bundle) { |
|
96
|
3 |
|
$paths[] = $bundle->getPath().'/DataFixtures/Mongo'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
return $paths; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $paths |
|
104
|
|
|
*/ |
|
105
|
3 |
|
protected function loadPaths($paths) |
|
106
|
|
|
{ |
|
107
|
3 |
|
foreach ($paths as $path) { |
|
108
|
3 |
|
if (is_dir($path)) { |
|
109
|
2 |
|
$this->loader->loadFromDirectory($path); |
|
110
|
|
|
} |
|
111
|
3 |
|
if (is_file($path)) { |
|
112
|
3 |
|
$this->loader->loadFromFile($path); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sorts fixtures by getOrder in case of implementing OrderedFixtureInterface |
|
119
|
|
|
* Fixtures with interface will be after the rest |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $fixtures |
|
122
|
|
|
* @return self |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function sortFixtures(&$fixtures): self |
|
125
|
|
|
{ |
|
126
|
2 |
|
usort($fixtures, function ($fixture1, $fixture2) { |
|
127
|
2 |
|
$isFixture1Instance = ($fixture1 instanceof OrderedFixtureInterface); |
|
128
|
2 |
|
$isFixture2Instance = ($fixture2 instanceof OrderedFixtureInterface); |
|
129
|
|
|
|
|
130
|
2 |
|
if ($isFixture1Instance && $isFixture2Instance) { |
|
131
|
2 |
|
return $fixture1->getOrder() - $fixture2->getOrder(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
if (!$isFixture1Instance && !$isFixture2Instance) { |
|
135
|
2 |
|
return 1; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
2 |
|
return ($isFixture2Instance) ? -1 : 1; |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
2 |
|
}); |
|
143
|
|
|
|
|
144
|
2 |
|
return $this; |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|