]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/InstallCommand.php
Add tests on TablePrefixSubscriber
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / InstallCommand.php
CommitLineData
2e45e7be
J
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Console\Input\InputInterface;
0bf99bb1
J
7use Symfony\Component\Console\Input\InputOption;
8use Symfony\Component\Console\Input\ArrayInput;
2e45e7be 9use Symfony\Component\Console\Output\OutputInterface;
0bf99bb1 10use Symfony\Component\Console\Output\NullOutput;
78507d28
JB
11use Symfony\Component\Console\Question\Question;
12use Symfony\Component\Console\Question\ConfirmationQuestion;
13use Symfony\Component\Console\Helper\Table;
1210dae1 14use Wallabag\UserBundle\Entity\User;
4d85d7e9 15use Wallabag\CoreBundle\Entity\Config;
2e45e7be
J
16
17class InstallCommand extends ContainerAwareCommand
18{
0bf99bb1
J
19 /**
20 * @var InputInterface
21 */
22 protected $defaultInput;
23
24 /**
25 * @var OutputInterface
26 */
27 protected $defaultOutput;
28
2e45e7be
J
29 protected function configure()
30 {
31 $this
32 ->setName('wallabag:install')
33 ->setDescription('Wallabag installer.')
0bf99bb1
J
34 ->addOption(
35 'reset',
36 null,
37 InputOption::VALUE_NONE,
38 'Reset current database'
39 )
2e45e7be
J
40 ;
41 }
42
43 protected function execute(InputInterface $input, OutputInterface $output)
44 {
0bf99bb1
J
45 $this->defaultInput = $input;
46 $this->defaultOutput = $output;
47
48 $output->writeln('<info>Installing Wallabag...</info>');
2e45e7be
J
49 $output->writeln('');
50
51 $this
0bf99bb1
J
52 ->checkRequirements()
53 ->setupDatabase()
54 ->setupAdmin()
55 ->setupAsset()
2e45e7be
J
56 ;
57
58 $output->writeln('<info>Wallabag has been successfully installed.</info>');
59 $output->writeln('<comment>Just execute `php app/console server:run` for using wallabag: http://localhost:8000</comment>');
60 }
61
0bf99bb1 62 protected function checkRequirements()
2e45e7be 63 {
0bf99bb1 64 $this->defaultOutput->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
2e45e7be
J
65
66 $fulfilled = true;
67
68 // @TODO: find a better way to check requirements
0bf99bb1 69 $label = '<comment>PCRE</comment>';
2e45e7be 70 if (extension_loaded('pcre')) {
0bf99bb1
J
71 $status = '<info>OK!</info>';
72 $help = '';
2e45e7be
J
73 } else {
74 $fulfilled = false;
0bf99bb1
J
75 $status = '<error>ERROR!</error>';
76 $help = 'You should enabled PCRE extension';
2e45e7be 77 }
0bf99bb1 78 $rows[] = array($label, $status, $help);
2e45e7be 79
0bf99bb1 80 $label = '<comment>DOM</comment>';
2e45e7be 81 if (extension_loaded('DOM')) {
0bf99bb1
J
82 $status = '<info>OK!</info>';
83 $help = '';
2e45e7be
J
84 } else {
85 $fulfilled = false;
0bf99bb1
J
86 $status = '<error>ERROR!</error>';
87 $help = 'You should enabled DOM extension';
2e45e7be 88 }
0bf99bb1
J
89 $rows[] = array($label, $status, $help);
90
78507d28
JB
91 $table = new Table($this->defaultOutput);
92 $table
0bf99bb1
J
93 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
94 ->setRows($rows)
78507d28 95 ->render();
2e45e7be
J
96
97 if (!$fulfilled) {
0bf99bb1
J
98 throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
99 } else {
100 $this->defaultOutput->writeln('<info>Success! Your system can run Wallabag properly.</info>');
2e45e7be
J
101 }
102
0bf99bb1 103 $this->defaultOutput->writeln('');
2e45e7be
J
104
105 return $this;
106 }
107
0bf99bb1 108 protected function setupDatabase()
2e45e7be 109 {
0bf99bb1 110 $this->defaultOutput->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
2e45e7be 111
0bf99bb1
J
112 // user want to reset everything? Don't care about what is already here
113 if (true === $this->defaultInput->getOption('reset')) {
114 $this->defaultOutput->writeln('Droping database, creating database and schema');
2e45e7be 115
0bf99bb1
J
116 $this
117 ->runCommand('doctrine:database:drop', array('--force' => true))
118 ->runCommand('doctrine:database:create')
119 ->runCommand('doctrine:schema:create')
120 ;
2e45e7be 121
0bf99bb1
J
122 return $this;
123 }
2e45e7be 124
0bf99bb1
J
125 if (!$this->isDatabasePresent()) {
126 $this->defaultOutput->writeln('Creating database and schema, clearing the cache');
2e45e7be 127
0bf99bb1
J
128 $this
129 ->runCommand('doctrine:database:create')
130 ->runCommand('doctrine:schema:create')
131 ->runCommand('cache:clear')
132 ;
2e45e7be 133
0bf99bb1
J
134 return $this;
135 }
2e45e7be 136
78507d28
JB
137 $questionHelper = $this->getHelper('question');
138 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
2e45e7be 139
78507d28 140 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1 141 $this->defaultOutput->writeln('Droping database, creating database and schema');
2e45e7be 142
0bf99bb1
J
143 $this
144 ->runCommand('doctrine:database:drop', array('--force' => true))
145 ->runCommand('doctrine:database:create')
146 ->runCommand('doctrine:schema:create')
147 ;
148 } elseif ($this->isSchemaPresent()) {
78507d28
JB
149 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
150 if ($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1 151 $this->defaultOutput->writeln('Droping schema and creating schema');
2e45e7be 152
0bf99bb1
J
153 $this
154 ->runCommand('doctrine:schema:drop', array('--force' => true))
155 ->runCommand('doctrine:schema:create')
156 ;
157 }
2e45e7be 158 } else {
0bf99bb1
J
159 $this->defaultOutput->writeln('Creating schema');
160
161 $this
162 ->runCommand('doctrine:schema:create')
163 ;
2e45e7be
J
164 }
165
0bf99bb1
J
166 $this->defaultOutput->writeln('Clearing the cache');
167 $this->runCommand('cache:clear');
168
0bf99bb1
J
169 $this->defaultOutput->writeln('');
170
171 return $this;
2e45e7be
J
172 }
173
0bf99bb1 174 protected function setupAdmin()
2e45e7be 175 {
0bf99bb1
J
176 $this->defaultOutput->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
177
78507d28
JB
178 $questionHelper = $this->getHelperSet()->get('question');
179 $question = new ConfirmationQuestion('Would you like to create a new user ? (y/N)', false);
0bf99bb1 180
78507d28 181 if (!$questionHelper->ask($this->defaultInput, $this->defaultOutput, $question)) {
0bf99bb1
J
182 return $this;
183 }
184
2e45e7be
J
185 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
186
ec3ce598
NL
187 $userManager = $this->getContainer()->get('fos_user.user_manager');
188 $user = $userManager->createUser();
78507d28
JB
189
190 $question = new Question('Username (default: wallabag) :', 'wallabag');
191 $user->setUsername($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
192
193 $question = new Question('Password (default: wallabag) :', 'wallabag');
194 $user->setPlainPassword($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
195
196 $question = new Question('Email:', '');
197 $user->setEmail($questionHelper->ask($this->defaultInput, $this->defaultOutput, $question));
198
a1691859 199 $user->setEnabled(true);
2e45e7be
J
200
201 $em->persist($user);
202
0bd2cb1e
J
203 $config = new Config($user);
204 $config->setTheme($this->getContainer()->getParameter('theme'));
205 $config->setItemsPerPage($this->getContainer()->getParameter('items_on_page'));
0c83fd59 206 $config->setRssLimit($this->getContainer()->getParameter('rss_limit'));
0bd2cb1e 207 $config->setLanguage($this->getContainer()->getParameter('language'));
2e45e7be 208
4d85d7e9 209 $em->persist($config);
0bf99bb1
J
210
211 $em->flush();
212
213 $this->defaultOutput->writeln('');
214
215 return $this;
2e45e7be
J
216 }
217
0bf99bb1 218 protected function setupAsset()
2e45e7be 219 {
0bf99bb1
J
220 $this->defaultOutput->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
221
2e45e7be 222 $this
0bf99bb1
J
223 ->runCommand('assets:install')
224 ->runCommand('assetic:dump')
2e45e7be
J
225 ;
226
0bf99bb1
J
227 $this->defaultOutput->writeln('');
228
229 return $this;
230 }
231
232 /**
4346a860 233 * Run a command.
0bf99bb1
J
234 *
235 * @param string $command
236 * @param array $parameters Parameters to this command (usually 'force' => true)
237 */
238 protected function runCommand($command, $parameters = array())
239 {
240 $parameters = array_merge(
241 array('command' => $command),
242 $parameters,
243 array(
244 '--no-debug' => true,
245 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
246 )
247 );
248
249 if ($this->defaultInput->getOption('no-interaction')) {
250 $parameters = array_merge($parameters, array('--no-interaction' => true));
251 }
252
253 $this->getApplication()->setAutoExit(false);
254 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
255
256 if (0 !== $exitCode) {
257 $this->getApplication()->setAutoExit(true);
258
259 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
260 $this->defaultOutput->writeln("<error>$errorMessage</error>");
261 $exception = new \Exception($errorMessage, $exitCode);
262
263 throw $exception;
264 }
265
266 // PDO does not always close the connection after Doctrine commands.
267 // See https://github.com/symfony/symfony/issues/11750.
268 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
269
2e45e7be
J
270 return $this;
271 }
0bf99bb1
J
272
273 /**
4346a860 274 * Check if the database already exists.
0bf99bb1 275 *
4346a860 276 * @return bool
0bf99bb1
J
277 */
278 private function isDatabasePresent()
279 {
af43bd37
JB
280 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
281 $databaseName = $connection->getDatabase();
0bf99bb1
J
282
283 try {
af43bd37 284 $schemaManager = $connection->getSchemaManager();
0bf99bb1 285 } catch (\Exception $exception) {
54a2241e 286 // mysql & sqlite
0bf99bb1
J
287 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
288 return false;
289 }
290
54a2241e
JB
291 // pgsql
292 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
293 return false;
294 }
295
0bf99bb1
J
296 throw $exception;
297 }
298
732c2ad8
J
299 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
300 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
301 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
302
303 if (isset($params['path']) && file_exists($params['path'])) {
304 return true;
305 }
306
307 return false;
308 }
309
0bf99bb1
J
310 return in_array($databaseName, $schemaManager->listDatabases());
311 }
312
313 /**
164bd801 314 * Check if the schema is already created.
4346a860 315 * If we found at least oen table, it means the schema exists.
0bf99bb1 316 *
4346a860 317 * @return bool
0bf99bb1
J
318 */
319 private function isSchemaPresent()
320 {
321 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
322
164bd801 323 return count($schemaManager->listTableNames()) > 0 ? true : false;
0bf99bb1 324 }
2e45e7be 325}