aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Command/InstallCommand.php
blob: bf2f747d81a70b762b3567a28dcf14733b2ccc8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

namespace Wallabag\CoreBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Entity\UsersConfig;

class InstallCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('wallabag:install')
            ->setDescription('Wallabag installer.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('<info>Installing Wallabag.</info>');
        $output->writeln('');

        $this
            ->checkStep($output)
            ->setupStep($input, $output)
        ;

        $output->writeln('<info>Wallabag has been successfully installed.</info>');
        $output->writeln('<comment>Just execute `php app/console server:run` for using wallabag: http://localhost:8000</comment>');
    }

    protected function checkStep(OutputInterface $output)
    {
        $output->writeln('<info>Checking system requirements.</info>');

        $fulfilled = true;

        // @TODO: find a better way to check requirements
        $output->writeln('<comment>Check PCRE</comment>');
        if (extension_loaded('pcre')) {
            $output->writeln(' <info>OK</info>');
        } else {
            $fulfilled = false;
            $output->writeln(' <error>ERROR</error>');
            $output->writeln('<comment>You should enabled PCRE extension</comment>');
        }

        $output->writeln('<comment>Check DOM</comment>');
        if (extension_loaded('DOM')) {
            $output->writeln(' <info>OK</info>');
        } else {
            $fulfilled = false;
            $output->writeln(' <error>ERROR</error>');
            $output->writeln('<comment>You should enabled DOM extension</comment>');
        }

        if (!$fulfilled) {
            throw new RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
        }

        $output->writeln('');

        return $this;
    }

    protected function setupStep(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('<info>Setting up database.</info>');

        $this->setupDatabase($input, $output);

        // if ($this->getHelperSet()->get('dialog')->askConfirmation($output, '<question>Load fixtures (Y/N)?</question>', false)) {
        //     $this->setupFixtures($input, $output);
        // }

        $output->writeln('');
        $output->writeln('<info>Administration setup.</info>');

        $this->setupAdmin($output);

        $output->writeln('');

        return $this;
    }

    protected function setupDatabase(InputInterface $input, OutputInterface $output)
    {
        if ($this->getHelperSet()->get('dialog')->askConfirmation($output, '<question>Drop current database (Y/N)?</question>', true)) {
            $connection = $this->getContainer()->get('doctrine')->getConnection();
            $params = $connection->getParams();

            $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
            unset($params['dbname']);

            if (!isset($params['path'])) {
                $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
            }

            $connection->getSchemaManager()->dropDatabase($name);
        } else {
            throw new \Exception("Install setup stopped, database need to be dropped. Please backup your current one and re-launch the install command.");
        }

        $this
            ->runCommand('doctrine:database:create', $input, $output)
            ->runCommand('doctrine:schema:create', $input, $output)
            ->runCommand('cache:clear', $input, $output)
            ->runCommand('assets:install', $input, $output)
            ->runCommand('assetic:dump', $input, $output)
        ;
    }

    protected function setupFixtures(InputInterface $input, OutputInterface $output)
    {
        $doctrineConfig = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection()->getConfiguration();
        $logger = $doctrineConfig->getSQLLogger();
        // speed up fixture load
        $doctrineConfig->setSQLLogger(null);
        $this->runCommand('doctrine:fixtures:load', $input, $output);
        $doctrineConfig->setSQLLogger($logger);
    }

    protected function setupAdmin(OutputInterface $output)
    {
        $dialog = $this->getHelperSet()->get('dialog');
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');

        $user = new User();
        $user->setUsername($dialog->ask($output, '<question>Username</question> <comment>(default: wallabag)</comment> :', 'wallabag'));
        $user->setPassword($dialog->ask($output, '<question>Password</question> <comment>(default: wallabag)</comment> :', 'wallabag'));
        $user->setEmail($dialog->ask($output, '<question>Email:</question>', ''));

        $em->persist($user);

        $pagerConfig = new UsersConfig();
        $pagerConfig->setUser($user);
        $pagerConfig->setName('pager');
        $pagerConfig->setValue(10);

        $em->persist($pagerConfig);

        $languageConfig = new LanguageConfig();
        $languageConfig->setUser($user);
        $languageConfig->setName('language');
        $languageConfig->setValue('en_EN');

        $em->persist($languageConfig);

        $em->flush();
    }

    protected function runCommand($command, InputInterface $input, OutputInterface $output)
    {
        $this
            ->getApplication()
            ->find($command)
            ->run($input, $output)
        ;

        return $this;
    }
}