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