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