Skip to content

Commit 9c695e4

Browse files
committed
added --dry-run to database:update, tests, several fixes
All Commands are services now
1 parent 3e49927 commit 9c695e4

15 files changed

+655
-66
lines changed

Command/AcceptLicenseCommand.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33
namespace Fuzzyma\Contao\DatabaseCommandsBundle\Command;
44

5-
use Contao\CoreBundle\Command\AbstractLockedCommand;
65
use Symfony\Component\Console\Input\InputInterface;
76
use Symfony\Component\Console\Input\InputOption;
87
use Symfony\Component\Console\Output\OutputInterface;
98
use Symfony\Component\Console\Question\Question;
109

11-
class AcceptLicenseCommand extends AbstractLockedCommand
10+
class AcceptLicenseCommand extends BaseCommand
1211
{
1312

14-
private $license = 'no';
1513

1614
protected function configure()
1715
{
1816

1917
$this
2018
->setName('contao:license')
21-
->setDescription('Accept the contao license');
19+
->setDescription('Accept the contao license')
20+
->addOption('yes', 'y', InputOption::VALUE_NONE, 'Accept the GNU GENERAL PUBLIC LICENSE');
2221
}
2322

2423
public function getQuestion($question, $default, $sep = ':')
@@ -29,29 +28,32 @@ public function getQuestion($question, $default, $sep = ':')
2928
protected function interact(InputInterface $input, OutputInterface $output)
3029
{
3130

32-
$questionHelper = $this->getHelper('question');
31+
if ($input->getOption('yes')) return;
3332

33+
$questionHelper = $this->getHelper('question');
3434
$question = new Question($this->getQuestion('Do you want to accept the GNU GENERAL PUBLIC LICENSE', 'yes'), 'yes');
35-
$this->license = $questionHelper->ask($input, $output, $question);
35+
$input->setOption('yes', $questionHelper->ask($input, $output, $question) == 'yes' ? true : false);
3636

3737
}
3838

39-
protected function executeLocked(InputInterface $input, OutputInterface $output)
39+
protected function execute(InputInterface $input, OutputInterface $output)
4040
{
4141

42-
$this->getContainer()->get('contao.framework')->initialize();
42+
$this->framework->initialize();
4343

44-
if($this->license == 'no'){
45-
$this->getContainer()->get('contao.install_tool')->persistConfig('licenseAccepted', false);
44+
if (!$input->getOption('yes')) {
45+
$this->installTool->persistConfig('licenseAccepted', false);
4646
$output->writeln('<error>Error: License was not accepted</error>');
47-
return;
47+
return 1;
4848
}
4949

5050

51-
$this->getContainer()->get('contao.install_tool')->persistConfig('licenseAccepted', true);
51+
$this->installTool->persistConfig('licenseAccepted', true);
5252

5353
$output->writeln('<info>Success: License accepted</info>');
5454

55+
return 0;
56+
5557
}
5658

5759
}

Command/BaseCommand.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Fuzzyma\Contao\DatabaseCommandsBundle\Command;
4+
5+
6+
use Contao\CoreBundle\Framework\ContaoFramework;
7+
use Contao\InstallationBundle\InstallTool;
8+
use Symfony\Component\Console\Command\Command;
9+
10+
class BaseCommand extends Command
11+
{
12+
13+
protected $framework;
14+
protected $installTool;
15+
16+
public function __construct(ContaoFramework $framework, InstallTool $installTool)
17+
{
18+
parent::__construct();
19+
20+
$this->framework = $framework;
21+
$this->installTool = $installTool;
22+
}
23+
24+
}

Command/DatabaseAddAdminCommand.php

+36-35
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22

33
namespace Fuzzyma\Contao\DatabaseCommandsBundle\Command;
44

5-
use Contao\CoreBundle\Command\AbstractLockedCommand;
5+
use Contao\CoreBundle\Framework\ContaoFramework;
6+
use Contao\InstallationBundle\InstallTool;
67
use Patchwork\Utf8;
8+
use Symfony\Component\Console\Exception\RuntimeException;
79
use Symfony\Component\Console\Input\InputInterface;
810
use Symfony\Component\Console\Input\InputOption;
911
use Symfony\Component\Console\Output\OutputInterface;
1012
use Symfony\Component\Console\Question\Question;
1113

12-
class DatabaseAddAdminCommand extends AbstractLockedCommand
14+
class DatabaseAddAdminCommand extends BaseCommand
1315
{
1416

15-
private $parameters = [];
16-
private $abort = false;
17+
private $locale;
18+
19+
public function __construct(ContaoFramework $framework, InstallTool $installTool, $locale)
20+
{
21+
parent::__construct($framework, $installTool);
22+
23+
$this->locale = $locale;
24+
}
25+
1726

1827
protected function configure()
1928
{
@@ -26,6 +35,7 @@ protected function configure()
2635
new InputOption('name', 'a', InputOption::VALUE_REQUIRED, 'Name'),
2736
new InputOption('email', 'm', InputOption::VALUE_REQUIRED, 'Email'),
2837
new InputOption('password', 'p', InputOption::VALUE_REQUIRED, 'Password'),
38+
new InputOption('force', 'f', InputOption::VALUE_NONE | InputOption::VALUE_OPTIONAL, 'Add admin even if there is already an admin in the table')
2939
));
3040
}
3141

@@ -37,21 +47,17 @@ public function getQuestion($question, $default, $sep = ':')
3747
protected function interact(InputInterface $input, OutputInterface $output)
3848
{
3949

40-
$installTool = $this->getContainer()->get('contao.install_tool');
41-
42-
if (!$installTool->hasTable('tl_user')) {
43-
$output->writeln('<error>Error: tl_user does not exist</error>');
44-
return null;
50+
if (!$this->installTool->hasTable('tl_user')) {
51+
throw new RuntimeException('<error>Error: tl_user does not exist</error>', 1);
4552
}
4653

4754

4855
$questionHelper = $this->getHelper('question');
4956

50-
if ($installTool->hasAdminUser()) {
57+
if ($this->installTool->hasAdminUser() && !$input->getOption('force')) {
5158
$question = new Question($this->getQuestion('Admin entry already present in tl_user table. Add anyway?', 'no'), 'no');
52-
if ($questionHelper->ask($input, $output, $question) == 'no') {
53-
$this->abort = true;
54-
return;
59+
if ($questionHelper->ask($input, $output, $question) !== 'yes') {
60+
throw new RuntimeException('<error>Aborted: Admin entry already present in tl_user table.</error>', 2);
5561
}
5662
}
5763

@@ -99,13 +105,13 @@ protected function interact(InputInterface $input, OutputInterface $output)
99105
}
100106

101107
$password = $input->getOption('password');
102-
$minLength = $installTool->getConfig('minPasswordLength');
108+
$minLength = $this->installTool->getConfig('minPasswordLength');
103109

104-
$question = new Question('<info>Enter a password</info>');
110+
$question = new Question('<info>Enter a password:</info> ');
105111
$question->setValidator(function ($answer) use ($minLength, $username) {
106112
return $this->passwordValidator($answer, $minLength, $username);
107113
});
108-
$question->setHidden(true);
114+
// $question->setHidden(true); see symfony/symfony#19463
109115
$question->setMaxAttempts(3);
110116

111117
if (!$password) {
@@ -119,42 +125,37 @@ protected function interact(InputInterface $input, OutputInterface $output)
119125
}
120126
}
121127

122-
$this->parameters = [
123-
'username' => $username,
124-
'name' => $name,
125-
'email' => $email,
126-
'password' => $password
127-
];
128+
$input->setOption('username', $username);
129+
$input->setOption('name', $name);
130+
$input->setOption('email', $email);
131+
$input->setOption('password', $password);
128132

129133
}
130134

131-
protected function executeLocked(InputInterface $input, OutputInterface $output)
135+
protected function execute(InputInterface $input, OutputInterface $output)
132136
{
133137

134-
if ($this->abort) return;
138+
$this->framework->initialize();
135139

136-
$installTool = $this->getContainer()->get('contao.install_tool');
137-
$this->getContainer()->get('contao.framework')->initialize();
138-
139-
$username = isset($this->parameters['username']) ? $this->parameters['username'] : $input->getOption('username');
140-
$name = isset($this->parameters['name']) ? $this->parameters['name'] : $input->getOption('name');
141-
$email = isset($this->parameters['email']) ? $this->parameters['email'] : $input->getOption('email');
142-
$password = isset($this->parameters['password']) ? $this->parameters['password'] : $input->getOption('password');
140+
$username = $input->getOption('username');
141+
$name = $input->getOption('name');
142+
$email = $input->getOption('email');
143+
$password = $input->getOption('password');
143144

144-
$minLength = $installTool->getConfig('minPasswordLength');
145+
$minLength = $this->installTool->getConfig('minPasswordLength');
145146

146147
$this->usernameValidator($username);
147148
$this->emailValidator($email);
148149
$this->passwordValidator($password, $minLength, $username);
149150

150-
$installTool->persistConfig('adminEmail', $email);
151+
$this->installTool->persistConfig('adminEmail', $email);
151152

152-
$installTool->persistAdminUser(
153+
$this->installTool->persistAdminUser(
153154
$username,
154155
$name,
155156
$email,
156157
$password,
157-
$this->getContainer()->getParameter('locale')
158+
$this->locale
158159
);
159160

160161
$output->writeln('<info>Success: Admin user added</info>');

Command/DatabaseUpdateCommand.php

+22-10
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,47 @@
22

33
namespace Fuzzyma\Contao\DatabaseCommandsBundle\Command;
44

5-
use Contao\CoreBundle\Command\AbstractLockedCommand;
5+
use Contao\CoreBundle\Framework\ContaoFramework;
6+
use Contao\InstallationBundle\Database\Installer;
7+
use Contao\InstallationBundle\InstallTool;
68
use Symfony\Component\Console\Input\InputInterface;
79
use Symfony\Component\Console\Input\InputOption;
810
use Symfony\Component\Console\Output\OutputInterface;
911

1012

11-
class DatabaseUpdateCommand extends AbstractLockedCommand
13+
class DatabaseUpdateCommand extends BaseCommand
1214
{
1315

16+
private $installer;
17+
18+
public function __construct(ContaoFramework $framework, InstallTool $installTool, Installer $installer)
19+
{
20+
parent::__construct($framework, $installTool);
21+
22+
$this->installer = $installer;
23+
}
24+
1425
protected function configure()
1526
{
1627

1728
$this
1829
->setName('contao:database:update')
1930
->setDescription('Updates the database to reflect all changes made in the dca files')
2031
->setDefinition(array(
21-
new InputOption('drop', 'd', InputOption::VALUE_NONE, 'Includes table and column drops')
32+
new InputOption('drop', 'd', InputOption::VALUE_NONE, 'Includes table and column drops'),
33+
new InputOption('dry-run', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_NONE, 'Only print queries. Does not touch database')
2234
));
2335
}
2436

25-
protected function executeLocked(InputInterface $input, OutputInterface $output)
37+
protected function execute(InputInterface $input, OutputInterface $output)
2638
{
2739

2840
$drop = !!$input->getOption('drop');
2941

30-
$this->getContainer()->get('contao.framework')->initialize();
31-
$this->getContainer()->get('contao.install_tool')->handleRunOnce();
32-
33-
$installer = $this->getContainer()->get('contao.installer');
42+
$this->framework->initialize();
43+
$this->installTool->handleRunOnce();
3444

35-
$commands = $installer->getCommands();
45+
$commands = $this->installer->getCommands();
3646

3747
if (!$drop) {
3848
unset($commands['DROP']);
@@ -42,7 +52,9 @@ protected function executeLocked(InputInterface $input, OutputInterface $output)
4252
foreach ($commands as $category) {
4353
foreach ($category as $hash => $sql) {
4454
$output->writeln('<info>Executing Query: "' . $sql . '"</info>');
45-
$installer->execCommand($hash);
55+
if (!$input->getOption('dry-run')) {
56+
$this->installer->execCommand($hash);
57+
}
4658
}
4759
}
4860

Command/SetupCommand.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Fuzzyma\Contao\DatabaseCommandsBundle\Command;
44

5-
use Contao\CoreBundle\Command\AbstractLockedCommand;
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\ArrayInput;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\OutputInterface;
8-
use Symfony\Component\Console\Input\ArrayInput;
99

10-
class SetupCommand extends AbstractLockedCommand
10+
class SetupCommand extends Command
1111
{
1212

1313
protected function configure()
@@ -18,30 +18,30 @@ protected function configure()
1818
->setDescription('Accepts license, updates database and creates admin user');
1919
}
2020

21-
protected function executeLocked(InputInterface $input, OutputInterface $output)
21+
protected function execute(InputInterface $input, OutputInterface $output)
2222
{
2323

2424
$command = $this->getApplication()->find('contao:license');
2525

26-
if($command->run(new ArrayInput([]), $output)){
26+
if ($command->run(new ArrayInput([]), $output)) {
2727
return;
2828
}
2929

3030
$command = $this->getApplication()->find('doctrine:database:create');
3131

32-
if($command->run(new ArrayInput(["--if-not-exists" => true]), $output)){
32+
if ($command->run(new ArrayInput(["--if-not-exists" => true]), $output)) {
3333
return;
3434
}
3535

3636
$command = $this->getApplication()->find('contao:database:update');
3737

38-
if($command->run(new ArrayInput([]), $output)){
38+
if ($command->run(new ArrayInput([]), $output)) {
3939
return;
4040
}
4141

4242
$command = $this->getApplication()->find('contao:database:addAdmin');
4343

44-
if($command->run(new ArrayInput([]), $output)){
44+
if ($command->run(new ArrayInput([]), $output)) {
4545
return;
4646
}
4747

ContaoDatabaseCommandsBundle.php

+8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace Fuzzyma\Contao\DatabaseCommandsBundle;
44

5+
use Fuzzyma\Contao\DatabaseCommandsBundle\Command\AcceptLicenseCommand;
6+
use Symfony\Component\Console\Application;
57
use Symfony\Component\HttpKernel\Bundle\Bundle;
68

79
class ContaoDatabaseCommandsBundle extends Bundle
810
{
11+
12+
public function registerCommands(Application $application)
13+
{
14+
// stop the unneeded search here!!
15+
}
16+
917
}

0 commit comments

Comments
 (0)