]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/InstallCommand.php
Adding new user
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / InstallCommand.php
CommitLineData
2e45e7be
J
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Console\Input\InputInterface;
7use Symfony\Component\Console\Output\OutputInterface;
2f69eb4a 8use Wallabag\CoreBundle\Entity\User;
4d85d7e9 9use Wallabag\CoreBundle\Entity\Config;
2e45e7be
J
10
11class InstallCommand extends ContainerAwareCommand
12{
13 protected function configure()
14 {
15 $this
16 ->setName('wallabag:install')
17 ->setDescription('Wallabag installer.')
18 ;
19 }
20
21 protected function execute(InputInterface $input, OutputInterface $output)
22 {
23 $output->writeln('<info>Installing Wallabag.</info>');
24 $output->writeln('');
25
26 $this
27 ->checkStep($output)
28 ->setupStep($input, $output)
29 ;
30
31 $output->writeln('<info>Wallabag has been successfully installed.</info>');
32 $output->writeln('<comment>Just execute `php app/console server:run` for using wallabag: http://localhost:8000</comment>');
33 }
34
35 protected function checkStep(OutputInterface $output)
36 {
37 $output->writeln('<info>Checking system requirements.</info>');
38
39 $fulfilled = true;
40
41 // @TODO: find a better way to check requirements
42 $output->writeln('<comment>Check PCRE</comment>');
43 if (extension_loaded('pcre')) {
44 $output->writeln(' <info>OK</info>');
45 } else {
46 $fulfilled = false;
47 $output->writeln(' <error>ERROR</error>');
48 $output->writeln('<comment>You should enabled PCRE extension</comment>');
49 }
50
51 $output->writeln('<comment>Check DOM</comment>');
52 if (extension_loaded('DOM')) {
53 $output->writeln(' <info>OK</info>');
54 } else {
55 $fulfilled = false;
56 $output->writeln(' <error>ERROR</error>');
57 $output->writeln('<comment>You should enabled DOM extension</comment>');
58 }
59
60 if (!$fulfilled) {
61 throw new RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
62 }
63
64 $output->writeln('');
65
66 return $this;
67 }
68
69 protected function setupStep(InputInterface $input, OutputInterface $output)
70 {
71 $output->writeln('<info>Setting up database.</info>');
72
73 $this->setupDatabase($input, $output);
74
75 // if ($this->getHelperSet()->get('dialog')->askConfirmation($output, '<question>Load fixtures (Y/N)?</question>', false)) {
76 // $this->setupFixtures($input, $output);
77 // }
78
79 $output->writeln('');
80 $output->writeln('<info>Administration setup.</info>');
81
82 $this->setupAdmin($output);
83
84 $output->writeln('');
85
86 return $this;
87 }
88
89 protected function setupDatabase(InputInterface $input, OutputInterface $output)
90 {
91 if ($this->getHelperSet()->get('dialog')->askConfirmation($output, '<question>Drop current database (Y/N)?</question>', true)) {
92 $connection = $this->getContainer()->get('doctrine')->getConnection();
93 $params = $connection->getParams();
94
95 $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
96 unset($params['dbname']);
97
98 if (!isset($params['path'])) {
99 $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
100 }
101
102 $connection->getSchemaManager()->dropDatabase($name);
103 } else {
104 throw new \Exception("Install setup stopped, database need to be dropped. Please backup your current one and re-launch the install command.");
105 }
106
107 $this
108 ->runCommand('doctrine:database:create', $input, $output)
109 ->runCommand('doctrine:schema:create', $input, $output)
110 ->runCommand('cache:clear', $input, $output)
111 ->runCommand('assets:install', $input, $output)
112 ->runCommand('assetic:dump', $input, $output)
113 ;
114 }
115
116 protected function setupFixtures(InputInterface $input, OutputInterface $output)
117 {
118 $doctrineConfig = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection()->getConfiguration();
119 $logger = $doctrineConfig->getSQLLogger();
120 // speed up fixture load
121 $doctrineConfig->setSQLLogger(null);
122 $this->runCommand('doctrine:fixtures:load', $input, $output);
123 $doctrineConfig->setSQLLogger($logger);
124 }
125
126 protected function setupAdmin(OutputInterface $output)
127 {
128 $dialog = $this->getHelperSet()->get('dialog');
129 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
130
2f69eb4a 131 $user = new User();
2e45e7be
J
132 $user->setUsername($dialog->ask($output, '<question>Username</question> <comment>(default: wallabag)</comment> :', 'wallabag'));
133 $user->setPassword($dialog->ask($output, '<question>Password</question> <comment>(default: wallabag)</comment> :', 'wallabag'));
134 $user->setEmail($dialog->ask($output, '<question>Email:</question>', ''));
135
136 $em->persist($user);
137
4d85d7e9
J
138 $config = new Config();
139 $config->setUser($user);
140 $config->setTheme('baggy');
141 $config->setItemsPerPage(10);
142 $config->setLanguage('en_US');
2e45e7be 143
4d85d7e9 144 $em->persist($config);
2e45e7be
J
145 }
146
147 protected function runCommand($command, InputInterface $input, OutputInterface $output)
148 {
149 $this
150 ->getApplication()
151 ->find($command)
152 ->run($input, $output)
153 ;
154
155 return $this;
156 }
157}