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