1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Hautelook\AliceBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Baldur Rensch <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Hautelook\AliceBundle\Console\Command\Doctrine; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
15
|
|
|
use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkBundleConsoleApplication; |
18
|
|
|
use Symfony\Component\Console\Application as ConsoleApplication; |
19
|
|
|
use Symfony\Component\Console\Command\Command; |
20
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Command used to load the fixtures. |
28
|
|
|
*/ |
29
|
|
|
final class DoctrineOrmLoadDataFixturesCommand extends Command |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ManagerRegistry |
33
|
|
|
*/ |
34
|
|
|
private $doctrine; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var AliceBundleLoaderInterface |
38
|
|
|
*/ |
39
|
|
|
private $loader; |
40
|
|
|
|
41
|
|
|
public function __construct(string $name, ManagerRegistry $managerRegistry, AliceBundleLoaderInterface $loader) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($name); |
44
|
|
|
|
45
|
|
|
$this->doctrine = $managerRegistry; |
46
|
|
|
$this->loader = $loader; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
protected function configure() |
53
|
|
|
{ |
54
|
|
|
$this |
55
|
|
|
->setAliases(['hautelook:fixtures:load']) |
56
|
|
|
->setDescription('Load data fixtures to your database.') |
57
|
|
|
->addOption( |
58
|
|
|
'bundle', |
59
|
|
|
'b', |
60
|
|
|
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
61
|
|
|
'Bundles where fixtures should be loaded.' |
62
|
|
|
) |
63
|
|
|
->addOption( |
64
|
|
|
'manager', |
65
|
|
|
'em', |
66
|
|
|
InputOption::VALUE_REQUIRED, |
67
|
|
|
'The entity manager to use for this command. If not specified, use the default Doctrine fixtures entity' |
68
|
|
|
.'manager.' |
69
|
|
|
) |
70
|
|
|
->addOption( |
71
|
|
|
'append', |
72
|
|
|
null, |
73
|
|
|
InputOption::VALUE_NONE, |
74
|
|
|
'Append the data fixtures instead of deleting all data from the database first.' |
75
|
|
|
) |
76
|
|
|
->addOption( |
77
|
|
|
'shard', |
78
|
|
|
null, |
79
|
|
|
InputOption::VALUE_REQUIRED, |
80
|
|
|
'The shard database id to use for this command.' |
81
|
|
|
) |
82
|
|
|
->addOption('purge-with-truncate', |
83
|
|
|
null, |
84
|
|
|
InputOption::VALUE_NONE, |
85
|
|
|
'Purge data by using a database-level TRUNCATE statement when using Doctrine fixtures.' |
86
|
|
|
) |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function setApplication(ConsoleApplication $application = null) |
94
|
|
|
{ |
95
|
|
|
if (null !== $application && false === $application instanceof FrameworkBundleConsoleApplication) { |
96
|
|
|
throw new \InvalidArgumentException( |
97
|
|
|
sprintf( |
98
|
|
|
'Expected application to be an instance of "%s".', |
99
|
|
|
FrameworkBundleConsoleApplication::class |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
parent::setApplication($application); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
* |
111
|
|
|
* @throws RuntimeException Unsupported Application type |
112
|
|
|
*/ |
113
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
114
|
|
|
{ |
115
|
|
|
// Warn the user that the database will be purged |
116
|
|
|
// Ask him to confirm his choice |
117
|
|
|
if ($input->isInteractive() && !$input->getOption('append')) { |
118
|
|
|
if (false === $this->askConfirmation( |
119
|
|
|
$input, |
120
|
|
|
$output, |
121
|
|
|
'<question>Careful, database will be purged. Do you want to continue y/N ?</question>', |
122
|
|
|
false |
123
|
|
|
) |
124
|
|
|
) { |
125
|
|
|
return 0; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$manager = $this->doctrine->getManager($input->getOption('manager')); |
130
|
|
|
$environment = $input->getOption('env'); |
131
|
|
|
$bundles = $input->getOption('bundle'); |
132
|
|
|
$shard = $input->getOption('shard'); |
133
|
|
|
$append = $input->getOption('append'); |
134
|
|
|
$truncate = $input->getOption('purge-with-truncate'); |
135
|
|
|
/** @var FrameworkBundleConsoleApplication $application */ |
136
|
|
|
$application = $this->getApplication(); |
137
|
|
|
|
138
|
|
|
$this->loader->load($application, $manager, $bundles, $environment, $append, $truncate, $shard); |
|
|
|
|
139
|
|
|
|
140
|
|
|
return 0; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Prompts to the user a message to ask him a confirmation. |
145
|
|
|
* |
146
|
|
|
* @param InputInterface $input |
147
|
|
|
* @param OutputInterface $output |
148
|
|
|
* @param string $question |
149
|
|
|
* @param bool $default |
150
|
|
|
* |
151
|
|
|
* @return bool User choice |
152
|
|
|
*/ |
153
|
|
|
private function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default) |
154
|
|
|
{ |
155
|
|
|
/** @var QuestionHelper $questionHelper */ |
156
|
|
|
$questionHelper = $this->getHelperSet()->get('question'); |
157
|
|
|
$question = new ConfirmationQuestion($question, $default); |
158
|
|
|
|
159
|
|
|
return (bool) $questionHelper->ask($input, $output, $question); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.