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