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\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LoadFixturesCommand. |
14
|
|
|
*/ |
15
|
|
|
class LoadFixturesCommand extends AbstractCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
5 |
|
protected function configure() |
21
|
|
|
{ |
22
|
5 |
|
parent::configure(); |
23
|
|
|
$this |
24
|
5 |
|
->setName('mongodb:fixtures:load') |
25
|
5 |
|
->setDescription('Load fixtures and applies them'); |
26
|
|
|
; |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
$this->io->writeln('Loading index fixtures'); |
35
|
|
|
|
36
|
|
|
$paths = array(); |
37
|
|
|
foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { |
|
|
|
|
38
|
|
|
$paths[] = $bundle->getPath().'/DataFixtures/Mongo'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$loader = new MongoFixturesLoader($this->getContainer()); |
42
|
|
|
|
43
|
|
|
foreach ($paths as $path) { |
44
|
|
|
if (is_dir($path)) { |
45
|
|
|
$loader->loadFromDirectory($path); |
46
|
|
|
} |
47
|
|
|
if (is_file($path)) { |
48
|
|
|
$loader->loadFromFile($path); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$fixtures = $loader->getLoadedClasses(); |
53
|
|
|
if (!$fixtures) { |
|
|
|
|
54
|
|
|
throw new \InvalidArgumentException( |
55
|
|
|
sprintf('Could not find any class to load in: %s', "\n\n- ".implode("\n- ", $paths)) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($fixtures as $fixture){ |
60
|
|
|
$this->loadFixture($fixture); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->io->writeln('Done.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param MongoFixtureInterface $indexList |
68
|
|
|
*/ |
69
|
|
|
private function loadFixture(MongoFixtureInterface $indexList) |
70
|
|
|
{ |
71
|
|
|
$indexList->loadData(); |
72
|
|
|
$indexList->loadIndexes(); |
73
|
|
|
$this->io->writeln('Loaded fixture: '. get_class($indexList)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: