3 namespace Wallabag\CoreBundle\Command
;
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
;
16 class InstallCommand
extends ContainerAwareCommand
21 protected $defaultInput;
24 * @var OutputInterface
26 protected $defaultOutput;
28 protected function configure()
31 ->setName('wallabag:install')
32 ->setDescription('Wallabag installer.')
36 InputOption
::VALUE_NONE
,
37 'Reset current database'
42 protected function execute(InputInterface
$input, OutputInterface
$output)
44 $this->defaultInput
= $input;
45 $this->defaultOutput
= $output;
47 $output->writeln('<info>Installing Wallabag...</info>');
57 $output->writeln('<info>Wallabag has been successfully installed.</info>');
58 $output->writeln('<comment>Just execute `php bin/console server:run` for using wallabag: http://localhost:8000</comment>');
61 protected function checkRequirements()
63 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
67 $label = '<comment>PCRE</comment>';
68 if (extension_loaded('pcre')) {
69 $status = '<info>OK!</info>';
73 $status = '<error>ERROR!</error>';
74 $help = 'You should enabled PCRE extension';
76 $rows[] = array($label, $status, $help);
78 $label = '<comment>DOM</comment>';
79 if (extension_loaded('DOM')) {
80 $status = '<info>OK!</info>';
84 $status = '<error>ERROR!</error>';
85 $help = 'You should enabled DOM extension';
87 $rows[] = array($label, $status, $help);
89 $table = new Table($this->defaultOutput
);
91 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
96 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
99 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
101 $this->defaultOutput
->writeln('');
106 protected function setupDatabase()
108 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
110 // user want to reset everything? Don't care about what is already here
111 if (true === $this->defaultInput
->getOption('reset')) {
112 $this->defaultOutput
->writeln('Droping database, creating database and schema, clearing the cache');
115 ->runCommand('doctrine:database:drop', array('--force' => true))
116 ->runCommand('doctrine:database:create')
117 ->runCommand('doctrine:schema:create')
118 ->runCommand('cache:clear')
121 $this->defaultOutput
->writeln('');
126 if (!$this->isDatabasePresent()) {
127 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
130 ->runCommand('doctrine:database:create')
131 ->runCommand('doctrine:schema:create')
132 ->runCommand('cache:clear')
135 $this->defaultOutput
->writeln('');
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);
143 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
144 $this->defaultOutput
->writeln('Droping database, creating database and schema');
147 ->runCommand('doctrine:database:drop', array('--force' => true))
148 ->runCommand('doctrine:database:create')
149 ->runCommand('doctrine:schema:create')
151 } elseif ($this->isSchemaPresent()) {
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)) {
154 $this->defaultOutput
->writeln('Droping schema and creating schema');
157 ->runCommand('doctrine:schema:drop', array('--force' => true))
158 ->runCommand('doctrine:schema:create')
162 $this->defaultOutput
->writeln('Creating schema');
165 ->runCommand('doctrine:schema:create')
169 $this->defaultOutput
->writeln('Clearing the cache');
170 $this->runCommand('cache:clear');
172 $this->defaultOutput
->writeln('');
177 protected function setupAdmin()
179 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
181 $questionHelper = $this->getHelperSet()->get('question');
182 $question = new ConfirmationQuestion('Would you like to create a new user ? (y/N)', false);
184 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
188 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
190 $userManager = $this->getContainer()->get('fos_user.user_manager');
191 $user = $userManager->createUser();
193 $question = new Question('Username (default: wallabag) :', 'wallabag');
194 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
196 $question = new Question('Password (default: wallabag) :', 'wallabag');
197 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
199 $question = new Question('Email:', '');
200 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
202 $user->setEnabled(true);
206 $config = new Config($user);
207 $config->setTheme($this->getContainer()->getParameter('theme'));
208 $config->setItemsPerPage($this->getContainer()->getParameter('items_on_page'));
209 $config->setRssLimit($this->getContainer()->getParameter('rss_limit'));
210 $config->setLanguage($this->getContainer()->getParameter('language'));
212 $em->persist($config);
216 $this->defaultOutput
->writeln('');
221 protected function setupAsset()
223 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
226 ->runCommand('assets:install')
227 ->runCommand('assetic:dump')
230 $this->defaultOutput
->writeln('');
238 * @param string $command
239 * @param array $parameters Parameters to this command (usually 'force' => true)
241 protected function runCommand($command, $parameters = array())
243 $parameters = array_merge(
244 array('command' => $command),
247 '--no-debug' => true,
248 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
252 if ($this->defaultInput
->getOption('no-interaction')) {
253 $parameters = array_merge($parameters, array('--no-interaction' => true));
256 $this->getApplication()->setAutoExit(false);
257 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
259 if (0 !== $exitCode) {
260 $this->getApplication()->setAutoExit(true);
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);
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();
277 * Check if the database already exists.
281 private function isDatabasePresent()
283 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
284 $databaseName = $connection->getDatabase();
287 $schemaManager = $connection->getSchemaManager();
288 } catch (\Exception
$exception) {
290 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
295 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
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();
306 if (isset($params['path']) && file_exists($params['path'])) {
313 return in_array($databaseName, $schemaManager->listDatabases());
317 * Check if the schema is already created.
318 * If we found at least oen table, it means the schema exists.
322 private function isSchemaPresent()
324 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
326 return count($schemaManager->listTableNames()) > 0 ? true : false;