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
|
|
|
// doctrine.yml, または他のprependで差し込まれたdoctrineの設定値を取得する. |
49
|
1 |
|
$configs = $container->getExtensionConfig('doctrine'); |
50
|
|
|
|
51
|
|
|
// $configsは, env変数(%env(xxx)%)やパラメータ変数(%xxx.xxx%)がまだ解決されていないため, resolveEnvPlaceholders()で解決する |
52
|
|
|
// @see https://github.com/symfony/symfony/issues/22456 |
53
|
1 |
|
$configs = $container->resolveEnvPlaceholders($configs, true); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// doctrine bundleのconfigurationで設定値を正規化する. |
56
|
1 |
|
$configration = new DoctrineBundleConfiguration($container->getParameter('kernel.debug')); |
57
|
1 |
|
$config = $this->processConfiguration($configration, $configs); |
58
|
|
|
|
59
|
|
|
// prependのタイミングではコンテナのインスタンスは利用できない. |
60
|
|
|
// 直接dbalのconnectionを生成し, dbアクセスを行う. |
61
|
1 |
|
$params = $config['dbal']['connections'][$config['dbal']['default_connection']]; |
62
|
1 |
|
$conn = DriverManager::getConnection($params); |
63
|
|
|
|
64
|
1 |
|
if (!$this->isConnected($conn)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$stmt = $conn->query('select * from dtb_plugin'); |
69
|
1 |
|
$plugins = $stmt->fetchAll(); |
70
|
|
|
|
71
|
1 |
|
$enabled = []; |
72
|
1 |
|
foreach ($plugins as $plugin) { |
73
|
|
|
if ($plugin['enabled']) { |
74
|
|
|
$enabled[] = $plugin['code']; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$disabled = []; |
79
|
1 |
|
foreach ($pluginDirs as $dir) { |
80
|
1 |
|
if (!in_array($dir, $enabled)) { |
81
|
1 |
|
$disabled[] = $dir; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// 他で使いまわすため, パラメータで保持しておく. |
86
|
1 |
|
$container->setParameter('eccube.plugins.enabled', $enabled); |
87
|
1 |
|
$container->setParameter('eccube.plugins.disabled', $disabled); |
88
|
|
|
|
89
|
1 |
|
$pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin'; |
90
|
1 |
|
$this->configureTwigPaths($container, $enabled, $pluginDir); |
91
|
1 |
|
$this->configureTranslations($container, $enabled, $pluginDir); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
View Code Duplication |
protected function configureTwigPaths(ContainerBuilder $container, $enabled, $pluginDir) |
|
|
|
|
95
|
|
|
{ |
96
|
1 |
|
$paths = []; |
97
|
|
|
|
98
|
1 |
|
foreach ($enabled as $code) { |
99
|
|
|
$dir = $pluginDir.'/'.$code.'/Resource/template'; |
100
|
|
|
if (file_exists($dir)) { |
101
|
|
|
$paths[$dir] = $code; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
if (!empty($paths)) { |
106
|
|
|
$container->prependExtensionConfig('twig', [ |
107
|
|
|
'paths' => $paths, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
View Code Duplication |
protected function configureTranslations(ContainerBuilder $container, $enabled, $pluginDir) |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
$paths = []; |
115
|
|
|
|
116
|
1 |
|
foreach ($enabled as $code) { |
117
|
|
|
$dir = $pluginDir.'/'.$code.'/Resource/locale'; |
118
|
|
|
if (file_exists($dir)) { |
119
|
|
|
$paths[] = $dir; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
if (!empty($paths)) { |
124
|
|
|
$container->prependExtensionConfig('framework', [ |
125
|
|
|
'translator' => [ |
126
|
|
|
'paths' => $paths, |
127
|
|
|
], |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
protected function isConnected(Connection $conn) |
133
|
|
|
{ |
134
|
|
|
try { |
135
|
1 |
|
if (!$conn->ping()) { |
136
|
1 |
|
return false; |
137
|
|
|
} |
138
|
|
|
} catch (\Exception $e) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
$sm = $conn->getSchemaManager(); |
143
|
1 |
|
$tables = array_filter( |
144
|
1 |
|
$sm->listTables(), |
145
|
1 |
|
function ($table) { |
146
|
1 |
|
return $table->getName() === 'dtb_plugin'; |
147
|
1 |
|
} |
148
|
|
|
); |
149
|
|
|
|
150
|
1 |
|
return empty($tables) ? false : true; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
protected function getPluginDirectories($pluginDir) |
154
|
|
|
{ |
155
|
1 |
|
$finder = (new Finder()) |
156
|
1 |
|
->in($pluginDir) |
157
|
1 |
|
->sortByName() |
158
|
1 |
|
->depth(0) |
159
|
1 |
|
->directories(); |
160
|
|
|
|
161
|
1 |
|
$dirs = []; |
162
|
1 |
|
foreach ($finder as $dir) { |
163
|
1 |
|
$dirs[] = $dir->getBaseName(); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
return $dirs; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|