Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
28 | class InstallerCommand extends Command |
||
29 | { |
||
30 | protected static $defaultName = 'eccube:install'; |
||
31 | |||
32 | /** |
||
33 | * @var ContainerInterface |
||
34 | */ |
||
35 | protected $container; |
||
36 | |||
37 | /** |
||
38 | * @var SymfonyStyle |
||
39 | */ |
||
40 | protected $io; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $databaseUrl; |
||
46 | |||
47 | public function __construct(ContainerInterface $container) |
||
48 | { |
||
49 | parent::__construct(); |
||
50 | |||
51 | $this->container = $container; |
||
52 | } |
||
53 | |||
54 | protected function configure() |
||
55 | { |
||
56 | $this |
||
57 | ->setDescription('Install EC-CUBE'); |
||
58 | } |
||
59 | |||
60 | protected function interact(InputInterface $input, OutputInterface $output) |
||
61 | { |
||
62 | $this->io->title('EC-CUBE Installer Interactive Wizard'); |
||
63 | $this->io->text([ |
||
64 | 'If you prefer to not use this interactive wizard, define the environment valiables as follows:', |
||
65 | '', |
||
66 | ' $ export APP_ENV=dev', |
||
67 | ' $ export APP_DEBUG=1', |
||
68 | ' $ export DATABASE_URL=database_url', |
||
69 | ' $ export DATABASE_SERVER_VERSION=server_version', |
||
70 | ' $ export MAILER_URL=mailer_url', |
||
71 | ' $ export ECCUBE_AUTH_MAGIC=auth_magic', |
||
72 | ' ... and more', |
||
73 | ' $ php bin/console eccube:install --no-interaction', |
||
74 | '', |
||
75 | ]); |
||
76 | |||
77 | // DATABASE_URL |
||
78 | $databaseUrl = $this->container->getParameter('eccube_database_url'); |
||
79 | if (empty($databaseUrl)) { |
||
80 | $databaseUrl = 'sqlite:///var/eccube.db'; |
||
81 | } |
||
82 | $databaseUrl = $this->io->ask('Database Url', $databaseUrl); |
||
83 | |||
84 | // DATABASE_SERVER_VERSION |
||
85 | $serverVersion = $this->getDatabaseServerVersion($databaseUrl); |
||
86 | |||
87 | // MAILER_URL |
||
88 | $mailerUrl = $this->container->getParameter('eccube_mailer_url'); |
||
89 | if (empty($mailerUrl)) { |
||
90 | $mailerUrl = 'null://localhost'; |
||
91 | } |
||
92 | $mailerUrl = $this->io->ask('Mailer Url', $mailerUrl); |
||
93 | |||
94 | // ECCUBE_AUTH_MAGIC |
||
95 | $authMagic = $this->container->getParameter('eccube_auth_magic'); |
||
96 | if (empty($authMagic) || $authMagic === '<change.me>') { |
||
97 | $authMagic = StringUtil::random(); |
||
98 | } |
||
99 | $authMagic = $this->io->ask('Auth Magic', $authMagic); |
||
100 | |||
101 | $this->io->caution('Execute the installation process. All data is initialized.'); |
||
102 | $question = new ConfirmationQuestion('Is it OK?'); |
||
103 | if (!$this->io->askQuestion($question)) { |
||
104 | // `no`の場合はキャンセルメッセージを出力して終了する |
||
105 | $this->setCode(function () { |
||
106 | $this->io->success('EC-CUBE installation stopped.'); |
||
107 | }); |
||
108 | |||
109 | return; |
||
110 | } |
||
111 | |||
112 | $envParameters = [ |
||
113 | 'APP_ENV' => 'dev', |
||
114 | 'APP_DEBUG' => '1', |
||
115 | 'DATABASE_URL' => $databaseUrl, |
||
116 | 'DATABASE_SERVER_VERSION' => $serverVersion, |
||
117 | 'MAILER_URL' => $mailerUrl, |
||
118 | 'ECCUBE_AUTH_MAGIC' => $authMagic, |
||
119 | 'ECCUBE_ADMIN_ROUTE' => 'admin', |
||
120 | 'ECCUBE_TEMPLATE_CODE' => 'default', |
||
121 | 'ECCUBE_LOCALE' => 'ja', |
||
122 | ]; |
||
123 | |||
124 | $envDir = $this->container->getParameter('kernel.project_dir'); |
||
125 | $envFile = $envDir.'/.env'; |
||
126 | $envDistFile = $envDir.'/.env.dist'; |
||
127 | |||
128 | $env = file_exists($envFile) |
||
129 | ? file_get_contents($envFile) |
||
130 | : file_get_contents($envDistFile); |
||
131 | |||
132 | $env = StringUtil::replaceOrAddEnv($env, $envParameters); |
||
133 | |||
134 | file_put_contents($envFile, $env); |
||
135 | } |
||
136 | |||
137 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
141 | |||
142 | protected function execute(InputInterface $input, OutputInterface $output) |
||
184 | |||
185 | protected function getDatabaseName($databaseUrl) |
||
199 | |||
200 | protected function getDatabaseServerVersion($databaseUrl) |
||
201 | { |
||
202 | try { |
||
231 | } |
||
232 |