|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Fixtures\MongoFixturesLoader; |
|
8
|
|
|
use Facile\MongoDbBundle\Fixtures\MongoFixtureInterface; |
|
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\Input\InputOption; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class LoadFixturesCommand. |
|
18
|
|
|
*/ |
|
19
|
|
|
class LoadFixturesCommand extends AbstractCommand |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var MongoFixturesLoader */ |
|
22
|
|
|
private $loader; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
7 |
|
protected function configure() |
|
28
|
|
|
{ |
|
29
|
7 |
|
parent::configure(); |
|
30
|
|
|
$this |
|
31
|
7 |
|
->setName('mongodb:fixtures:load') |
|
32
|
7 |
|
->addArgument('addFixturesPath', InputArgument::OPTIONAL, 'Add a path to search in for fixtures files') |
|
33
|
7 |
|
->setDescription('Load fixtures and applies them'); |
|
34
|
|
|
; |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
38
|
|
|
{ |
|
39
|
2 |
|
parent::initialize($input, $output); |
|
40
|
2 |
|
$this->loader = new MongoFixturesLoader($this->getContainer()); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->io->writeln('Loading mongo fixtures'); |
|
49
|
|
|
/** @var Application $application */ |
|
50
|
2 |
|
$application = $this->getApplication(); |
|
51
|
|
|
|
|
52
|
2 |
|
$paths = $this->prepareSearchPaths($input, $application->getKernel()); |
|
53
|
|
|
|
|
54
|
2 |
|
$this->loadPaths($paths); |
|
55
|
|
|
|
|
56
|
2 |
|
$fixtures = $this->loader->getLoadedClasses(); |
|
57
|
2 |
|
if (empty($fixtures)) { |
|
58
|
1 |
|
throw new \InvalidArgumentException( |
|
59
|
1 |
|
sprintf('Could not find any class to load in: %s', "\n\n- ".implode("\n- ", $paths)) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
foreach ($fixtures as $fixture){ |
|
64
|
1 |
|
$this->loadFixture($fixture); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
$this->io->writeln(sprintf('Done, loaded %d fixtures files', count($fixtures))); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param MongoFixtureInterface $indexList |
|
72
|
|
|
*/ |
|
73
|
1 |
|
private function loadFixture(MongoFixtureInterface $indexList) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$indexList->loadData(); |
|
76
|
1 |
|
$indexList->loadIndexes(); |
|
77
|
1 |
|
$this->io->writeln('Loaded fixture: '. get_class($indexList)); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param InputInterface $input |
|
82
|
|
|
* @param KernelInterface $kernel |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
2 |
|
protected function prepareSearchPaths(InputInterface $input, KernelInterface $kernel): array |
|
87
|
|
|
{ |
|
88
|
2 |
|
$paths = []; |
|
89
|
|
|
|
|
90
|
2 |
|
if ($input->getArgument('addFixturesPath')) { |
|
91
|
1 |
|
$paths[] = $input->getArgument('addFixturesPath'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
foreach ($kernel->getBundles() as $bundle) { |
|
95
|
2 |
|
$paths[] = $bundle->getPath().'/DataFixtures/Mongo'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return $paths; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $paths |
|
103
|
|
|
*/ |
|
104
|
2 |
|
protected function loadPaths($paths) |
|
105
|
|
|
{ |
|
106
|
2 |
|
foreach ($paths as $path) { |
|
107
|
2 |
|
if (is_dir($path)) { |
|
108
|
1 |
|
$this->loader->loadFromDirectory($path); |
|
109
|
|
|
} |
|
110
|
2 |
|
if (is_file($path)) { |
|
111
|
2 |
|
$this->loader->loadFromFile($path); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
2 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|