3 namespace Wallabag\CoreBundle\Command
;
5 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
6 use Symfony\Component\Console\Input\InputInterface
;
7 use Symfony\Component\Console\Input\InputOption
;
8 use Symfony\Component\Console\Input\ArrayInput
;
9 use Symfony\Component\Console\Output\OutputInterface
;
10 use Symfony\Component\Console\Output\NullOutput
;
11 use Wallabag\CoreBundle\Entity\User
;
12 use Wallabag\CoreBundle\Entity\Config
;
14 class InstallCommand
extends ContainerAwareCommand
19 protected $defaultInput;
22 * @var OutputInterface
24 protected $defaultOutput;
26 protected function configure()
29 ->setName('wallabag:install')
30 ->setDescription('Wallabag installer.')
34 InputOption
::VALUE_NONE
,
35 'Reset current database'
40 protected function execute(InputInterface
$input, OutputInterface
$output)
42 $this->defaultInput
= $input;
43 $this->defaultOutput
= $output;
45 $output->writeln('<info>Installing Wallabag...</info>');
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>');
59 protected function checkRequirements()
61 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
65 // @TODO: find a better way to check requirements
66 $label = '<comment>PCRE</comment>';
67 if (extension_loaded('pcre')) {
68 $status = '<info>OK!</info>';
72 $status = '<error>ERROR!</error>';
73 $help = 'You should enabled PCRE extension';
75 $rows[] = array($label, $status, $help);
77 $label = '<comment>DOM</comment>';
78 if (extension_loaded('DOM')) {
79 $status = '<info>OK!</info>';
83 $status = '<error>ERROR!</error>';
84 $help = 'You should enabled DOM extension';
86 $rows[] = array($label, $status, $help);
88 $this->getHelper('table')
89 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
91 ->render($this->defaultOutput
);
94 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
96 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
99 $this->defaultOutput
->writeln('');
104 protected function setupDatabase()
106 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
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');
113 ->runCommand('doctrine:database:drop', array('--force' => true))
114 ->runCommand('doctrine:database:create')
115 ->runCommand('doctrine:schema:create')
121 if (!$this->isDatabasePresent()) {
122 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
125 ->runCommand('doctrine:database:create')
126 ->runCommand('doctrine:schema:create')
127 ->runCommand('cache:clear')
133 $dialog = $this->getHelper('dialog');
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');
139 ->runCommand('doctrine:database:drop', array('--force' => true))
140 ->runCommand('doctrine:database:create')
141 ->runCommand('doctrine:schema:create')
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');
148 ->runCommand('doctrine:schema:drop', array('--force' => true))
149 ->runCommand('doctrine:schema:create')
153 $this->defaultOutput
->writeln('Creating schema');
156 ->runCommand('doctrine:schema:create')
160 $this->defaultOutput
->writeln('Clearing the cache');
161 $this->runCommand('cache:clear');
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);
174 $this->defaultOutput
->writeln('');
179 protected function setupAdmin()
181 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
183 $dialog = $this->getHelperSet()->get('dialog');
185 if (false === $dialog->askConfirmation($this->defaultOutput
, '<question>Would you like to create a new user ? (y/N)</question>', true)) {
189 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
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>', ''));
198 $config = new Config($user);
199 $config->setTheme($this->getContainer()->getParameter('theme'));
200 $config->setItemsPerPage($this->getContainer()->getParameter('items_on_page'));
201 $config->setRssLimit($this->getContainer()->getParameter('rss_limit'));
202 $config->setLanguage($this->getContainer()->getParameter('language'));
204 $em->persist($config);
208 $this->defaultOutput
->writeln('');
213 protected function setupAsset()
215 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
218 ->runCommand('assets:install')
219 ->runCommand('assetic:dump')
222 $this->defaultOutput
->writeln('');
230 * @param string $command
231 * @param array $parameters Parameters to this command (usually 'force' => true)
233 protected function runCommand($command, $parameters = array())
235 $parameters = array_merge(
236 array('command' => $command),
239 '--no-debug' => true,
240 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
244 if ($this->defaultInput
->getOption('no-interaction')) {
245 $parameters = array_merge($parameters, array('--no-interaction' => true));
248 $this->getApplication()->setAutoExit(false);
249 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
251 if (0 !== $exitCode) {
252 $this->getApplication()->setAutoExit(true);
254 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
255 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
256 $exception = new \
Exception($errorMessage, $exitCode);
261 // PDO does not always close the connection after Doctrine commands.
262 // See https://github.com/symfony/symfony/issues/11750.
263 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
269 * Check if the database already exists.
273 private function isDatabasePresent()
275 $databaseName = $this->getContainer()->getParameter('database_name');
278 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
279 } catch (\Exception
$exception) {
280 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
287 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
288 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
289 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
291 if (isset($params['path']) && file_exists($params['path'])) {
298 return in_array($databaseName, $schemaManager->listDatabases());
302 * Check if the schema is already created.
303 * If we found at least oen table, it means the schema exists.
307 private function isSchemaPresent()
309 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
311 return count($schemaManager->listTableNames()) > 0 ? true : false;