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\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class LoadFixturesCommand. |
15
|
|
|
*/ |
16
|
|
|
class LoadFixturesCommand extends AbstractCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
parent::configure(); |
24
|
|
|
$this |
25
|
|
|
->setName('mongodb:fixtures:load') |
26
|
|
|
->setDescription('Load fixtures and applies them'); |
27
|
|
|
; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
34
|
|
|
{ |
35
|
|
|
$this->io->writeln('Loading mongo fixtures'); |
36
|
|
|
/** @var Application $application */ |
37
|
|
|
$application = $this->getApplication(); |
38
|
|
|
|
39
|
|
|
$paths = array(); |
40
|
|
|
foreach ($application->getKernel()->getBundles() as $bundle) { |
41
|
|
|
$paths[] = $bundle->getPath().'/DataFixtures/Mongo'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$loader = new MongoFixturesLoader($this->getContainer()); |
45
|
|
|
|
46
|
|
|
foreach ($paths as $path) { |
47
|
|
|
if (is_dir($path)) { |
48
|
|
|
$loader->loadFromDirectory($path); |
49
|
|
|
} |
50
|
|
|
if (is_file($path)) { |
51
|
|
|
$loader->loadFromFile($path); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$fixtures = $loader->getLoadedClasses(); |
56
|
|
|
if (empty($fixtures)) { |
57
|
|
|
throw new \InvalidArgumentException( |
58
|
|
|
sprintf('Could not find any class to load in: %s', "\n\n- ".implode("\n- ", $paths)) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($fixtures as $fixture){ |
63
|
|
|
$this->loadFixture($fixture); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->io->writeln('Done.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param MongoFixtureInterface $indexList |
71
|
|
|
*/ |
72
|
|
|
private function loadFixture(MongoFixtureInterface $indexList) |
73
|
|
|
{ |
74
|
|
|
$indexList->loadData(); |
75
|
|
|
$indexList->loadIndexes(); |
76
|
|
|
$this->io->writeln('Loaded fixture: '. get_class($indexList)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|