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 Symfony\Component\Console\Question\Question
;
12 use Symfony\Component\Console\Question\ConfirmationQuestion
;
13 use Symfony\Component\Console\Helper\Table
;
14 use Wallabag\UserBundle\Entity\User
;
15 use Wallabag\CoreBundle\Entity\Config
;
17 class InstallCommand
extends ContainerAwareCommand
22 protected $defaultInput;
25 * @var OutputInterface
27 protected $defaultOutput;
29 protected function configure()
32 ->setName('wallabag:install')
33 ->setDescription('Wallabag installer.')
37 InputOption
::VALUE_NONE
,
38 'Reset current database'
43 protected function execute(InputInterface
$input, OutputInterface
$output)
45 $this->defaultInput
= $input;
46 $this->defaultOutput
= $output;
48 $output->writeln('<info>Installing Wallabag...</info>');
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>');
62 protected function checkRequirements()
64 $this->defaultOutput
->writeln('<info><comment>Step 1 of 4.</comment> Checking system requirements.</info>');
68 // @TODO: find a better way to check requirements
69 $label = '<comment>PCRE</comment>';
70 if (extension_loaded('pcre')) {
71 $status = '<info>OK!</info>';
75 $status = '<error>ERROR!</error>';
76 $help = 'You should enabled PCRE extension';
78 $rows[] = array($label, $status, $help);
80 $label = '<comment>DOM</comment>';
81 if (extension_loaded('DOM')) {
82 $status = '<info>OK!</info>';
86 $status = '<error>ERROR!</error>';
87 $help = 'You should enabled DOM extension';
89 $rows[] = array($label, $status, $help);
91 $table = new Table($this->defaultOutput
);
93 ->setHeaders(array('Checked', 'Status', 'Recommendation'))
98 throw new \
RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
100 $this->defaultOutput
->writeln('<info>Success! Your system can run Wallabag properly.</info>');
103 $this->defaultOutput
->writeln('');
108 protected function setupDatabase()
110 $this->defaultOutput
->writeln('<info><comment>Step 2 of 4.</comment> Setting up database.</info>');
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');
117 ->runCommand('doctrine:database:drop', array('--force' => true))
118 ->runCommand('doctrine:database:create')
119 ->runCommand('doctrine:schema:create')
125 if (!$this->isDatabasePresent()) {
126 $this->defaultOutput
->writeln('Creating database and schema, clearing the cache');
129 ->runCommand('doctrine:database:create')
130 ->runCommand('doctrine:schema:create')
131 ->runCommand('cache:clear')
137 $questionHelper = $this->getHelper('question');
138 $question = new ConfirmationQuestion('It appears that your database already exists. Would you like to reset it? (y/N)', false);
140 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
141 $this->defaultOutput
->writeln('Droping database, creating database and schema');
144 ->runCommand('doctrine:database:drop', array('--force' => true))
145 ->runCommand('doctrine:database:create')
146 ->runCommand('doctrine:schema:create')
148 } elseif ($this->isSchemaPresent()) {
149 $question = new ConfirmationQuestion('Seems like your database contains schema. Do you want to reset it? (y/N)', false);
150 if ($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
151 $this->defaultOutput
->writeln('Droping schema and creating schema');
154 ->runCommand('doctrine:schema:drop', array('--force' => true))
155 ->runCommand('doctrine:schema:create')
159 $this->defaultOutput
->writeln('Creating schema');
162 ->runCommand('doctrine:schema:create')
166 $this->defaultOutput
->writeln('Clearing the cache');
167 $this->runCommand('cache:clear');
169 $this->defaultOutput
->writeln('');
174 protected function setupAdmin()
176 $this->defaultOutput
->writeln('<info><comment>Step 3 of 4.</comment> Administration setup.</info>');
178 $questionHelper = $this->getHelperSet()->get('question');
179 $question = new ConfirmationQuestion('Would you like to create a new user ? (y/N)', false);
181 if (!$questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question)) {
185 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
187 $userManager = $this->getContainer()->get('fos_user.user_manager');
188 $user = $userManager->createUser();
190 $question = new Question('Username (default: wallabag) :', 'wallabag');
191 $user->setUsername($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
193 $question = new Question('Password (default: wallabag) :', 'wallabag');
194 $user->setPlainPassword($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
196 $question = new Question('Email:', '');
197 $user->setEmail($questionHelper->ask($this->defaultInput
, $this->defaultOutput
, $question));
199 $user->setEnabled(true);
203 $config = new Config($user);
204 $config->setTheme($this->getContainer()->getParameter('theme'));
205 $config->setItemsPerPage($this->getContainer()->getParameter('items_on_page'));
206 $config->setRssLimit($this->getContainer()->getParameter('rss_limit'));
207 $config->setLanguage($this->getContainer()->getParameter('language'));
209 $em->persist($config);
213 $this->defaultOutput
->writeln('');
218 protected function setupAsset()
220 $this->defaultOutput
->writeln('<info><comment>Step 4 of 4.</comment> Installing assets.</info>');
223 ->runCommand('assets:install')
224 ->runCommand('assetic:dump')
227 $this->defaultOutput
->writeln('');
235 * @param string $command
236 * @param array $parameters Parameters to this command (usually 'force' => true)
238 protected function runCommand($command, $parameters = array())
240 $parameters = array_merge(
241 array('command' => $command),
244 '--no-debug' => true,
245 '--env' => $this->defaultInput
->getOption('env') ?: 'dev',
249 if ($this->defaultInput
->getOption('no-interaction')) {
250 $parameters = array_merge($parameters, array('--no-interaction' => true));
253 $this->getApplication()->setAutoExit(false);
254 $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
256 if (0 !== $exitCode) {
257 $this->getApplication()->setAutoExit(true);
259 $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
260 $this->defaultOutput
->writeln("<error>$errorMessage</error>");
261 $exception = new \
Exception($errorMessage, $exitCode);
266 // PDO does not always close the connection after Doctrine commands.
267 // See https://github.com/symfony/symfony/issues/11750.
268 $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
274 * Check if the database already exists.
278 private function isDatabasePresent()
280 $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
281 $databaseName = $connection->getDatabase();
284 $schemaManager = $connection->getSchemaManager();
285 } catch (\Exception
$exception) {
287 if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
292 if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
299 // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
300 if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
301 $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
303 if (isset($params['path']) && file_exists($params['path'])) {
310 return in_array($databaseName, $schemaManager->listDatabases());
314 * Check if the schema is already created.
315 * If we found at least oen table, it means the schema exists.
319 private function isSchemaPresent()
321 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
323 return count($schemaManager->listTableNames()) > 0 ? true : false;