|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of EC-CUBE |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* http://www.lockon.co.jp/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Eccube\DependencyInjection; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Configuration as DoctrineBundleConfiguration; |
|
17
|
|
|
use Doctrine\DBAL\Connection; |
|
18
|
|
|
use Doctrine\DBAL\DriverManager; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
21
|
|
|
use Symfony\Component\Finder\Finder; |
|
22
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
23
|
|
|
|
|
24
|
|
|
class EccubeExtension extends Extension implements PrependExtensionInterface |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* Loads a specific configuration. |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \InvalidArgumentException When provided tag is not defined in this extension |
|
30
|
|
|
*/ |
|
31
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
32
|
|
|
{ |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* Allow an extension to prepend the extension configurations. |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function prepend(ContainerBuilder $container) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
1 |
|
$pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin'; |
|
41
|
1 |
|
$pluginDirs = $this->getPluginDirectories($pluginDir); |
|
42
|
|
|
|
|
43
|
1 |
|
$container->setParameter('eccube.plugins.enabled', []); |
|
44
|
|
|
// ファイル設置のみの場合は, 無効なプラグインとみなす. |
|
45
|
|
|
// DB接続後, 有効無効の判定を行う. |
|
46
|
1 |
|
$container->setParameter('eccube.plugins.disabled', $pluginDirs); |
|
47
|
|
|
|
|
48
|
|
|
// FIXME WebTestCase で DATABASE_URL が取得できず落ちる |
|
49
|
1 |
|
if (!array_key_exists('APP_ENV', $_ENV) || $_ENV['APP_ENV'] == 'test') { |
|
50
|
1 |
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// doctrine.yml, または他のprependで差し込まれたdoctrineの設定値を取得する. |
|
54
|
|
|
$configs = $container->getExtensionConfig('doctrine'); |
|
55
|
|
|
|
|
56
|
|
|
// $configsは, env変数(%env(xxx)%)やパラメータ変数(%xxx.xxx%)がまだ解決されていないため, resolveEnvPlaceholders()で解決する |
|
57
|
|
|
// @see https://github.com/symfony/symfony/issues/22456 |
|
58
|
|
|
$configs = $container->resolveEnvPlaceholders($configs, true); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
// doctrine bundleのconfigurationで設定値を正規化する. |
|
61
|
|
|
$configration = new DoctrineBundleConfiguration($container->getParameter('kernel.debug')); |
|
62
|
|
|
$config = $this->processConfiguration($configration, $configs); |
|
63
|
|
|
|
|
64
|
|
|
// prependのタイミングではコンテナのインスタンスは利用できない. |
|
65
|
|
|
// 直接dbalのconnectionを生成し, dbアクセスを行う. |
|
66
|
|
|
$params = $config['dbal']['connections'][$config['dbal']['default_connection']]; |
|
67
|
|
|
$conn = DriverManager::getConnection($params); |
|
68
|
|
|
|
|
69
|
|
|
if (!$this->isConnected($conn)) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$stmt = $conn->query('select * from dtb_plugin'); |
|
74
|
|
|
$plugins = $stmt->fetchAll(); |
|
75
|
|
|
|
|
76
|
|
|
$enabled = []; |
|
77
|
|
|
foreach ($plugins as $plugin) { |
|
78
|
|
|
if ($plugin['enabled']) { |
|
79
|
|
|
$enabled[] = $plugin['code']; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$disabled = []; |
|
84
|
|
|
foreach ($pluginDirs as $dir) { |
|
85
|
|
|
if (!in_array($dir, $enabled)) { |
|
86
|
|
|
$disabled[] = $dir; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// 他で使いまわすため, パラメータで保持しておく. |
|
91
|
|
|
$container->setParameter('eccube.plugins.enabled', $enabled); |
|
92
|
|
|
$container->setParameter('eccube.plugins.disabled', $disabled); |
|
93
|
|
|
|
|
94
|
|
|
$pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin'; |
|
95
|
|
|
$this->configureTwigPaths($container, $enabled, $pluginDir); |
|
96
|
|
|
$this->configureTranslations($container, $enabled, $pluginDir); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
protected function configureTwigPaths(ContainerBuilder $container, $enabled, $pluginDir) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$paths = []; |
|
102
|
|
|
|
|
103
|
|
|
foreach ($enabled as $code) { |
|
104
|
|
|
$dir = $pluginDir.'/'.$code.'/Resource/template'; |
|
105
|
|
|
if (file_exists($dir)) { |
|
106
|
|
|
$paths[$dir] = $code; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (!empty($paths)) { |
|
111
|
|
|
$container->prependExtensionConfig('twig', [ |
|
112
|
|
|
'paths' => $paths, |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
protected function configureTranslations(ContainerBuilder $container, $enabled, $pluginDir) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$paths = []; |
|
120
|
|
|
|
|
121
|
|
|
foreach ($enabled as $code) { |
|
122
|
|
|
$dir = $pluginDir.'/'.$code.'/Resource/locale'; |
|
123
|
|
|
if (file_exists($dir)) { |
|
124
|
|
|
$paths[] = $dir; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (!empty($paths)) { |
|
129
|
|
|
$container->prependExtensionConfig('framework', [ |
|
130
|
|
|
'translator' => [ |
|
131
|
|
|
'paths' => $paths, |
|
132
|
|
|
], |
|
133
|
|
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
protected function isConnected(Connection $conn) |
|
138
|
|
|
{ |
|
139
|
|
|
try { |
|
140
|
|
|
if (!$conn->ping()) { |
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
} catch (\Exception $e) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$sm = $conn->getSchemaManager(); |
|
148
|
|
|
$tables = array_filter( |
|
149
|
|
|
$sm->listTables(), |
|
150
|
|
|
function ($table) { |
|
151
|
|
|
return $table->getName() === 'dtb_plugin'; |
|
152
|
|
|
} |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
return empty($tables) ? false : true; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
protected function getPluginDirectories($pluginDir) |
|
159
|
|
|
{ |
|
160
|
1 |
|
$finder = (new Finder()) |
|
161
|
1 |
|
->in($pluginDir) |
|
162
|
1 |
|
->depth(0) |
|
163
|
1 |
|
->directories(); |
|
164
|
|
|
|
|
165
|
1 |
|
$dirs = []; |
|
166
|
1 |
|
foreach ($finder as $dir) { |
|
167
|
1 |
|
$dirs[] = $dir->getBaseName(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
return $dirs; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|