1 | <?php declare(strict_types = 1); |
||
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) |
|
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 |
|
101 | |||
102 | /** |
||
103 | * @param array $paths |
||
104 | */ |
||
105 | 3 | protected function loadPaths($paths) |
|
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 |
||
147 | |||
148 | |||
149 | } |
||
150 |