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