|
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\Plugin; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Finder\Finder; |
|
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* プラグインの config.yml, event.yml を扱うクラス |
|
21
|
|
|
* |
|
22
|
|
|
* TODO プラグインからこのクラスをカスタマイズすることはできないので, |
|
23
|
|
|
static メソッドにしているが, DI コンテナでの管理を検討する. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ConfigManager |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
|
|
|
|
|
28
|
|
|
* すべてのプラグインの設定情報を返す. |
|
29
|
|
|
* |
|
30
|
|
|
* すべてのプラグインの config.yml 及び event.yml を読み込み、連想配列で返す. |
|
31
|
|
|
* キャッシュファイルが存在する場合は、キャッシュを利用する. |
|
32
|
|
|
* キャッシュファイルが存在しない場合は、キャッシュを生成する. |
|
33
|
|
|
* $app['debug'] = true の場合は、キャッシュを利用しない. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
42 |
|
public static function getPluginConfigAll($debug = false) |
|
38
|
|
|
{ |
|
39
|
42 |
|
if ($debug) { |
|
40
|
1 |
|
return self::parsePluginConfigs(); |
|
41
|
|
|
} |
|
42
|
41 |
|
$pluginConfigCache = self::getPluginConfigCacheFile(); |
|
43
|
41 |
|
if (file_exists($pluginConfigCache)) { |
|
44
|
39 |
|
return require $pluginConfigCache; |
|
45
|
|
|
} |
|
46
|
2 |
|
if (self::writePluginConfigCache($pluginConfigCache) === false) { |
|
47
|
|
|
return self::parsePluginConfigs(); |
|
48
|
|
|
} else { |
|
49
|
2 |
|
return require $pluginConfigCache; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* プラグイン設定情報のキャッシュを書き込む. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $cacheFile |
|
57
|
|
|
* |
|
58
|
|
|
* @return int|boolean file_put_contents() の結果 |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public static function writePluginConfigCache($cacheFile = null) |
|
61
|
|
|
{ |
|
62
|
2 |
|
if (is_null($cacheFile)) { |
|
63
|
|
|
$cacheFile = self::getPluginConfigCacheFile(); |
|
64
|
|
|
} |
|
65
|
2 |
|
$pluginConfigs = self::parsePluginConfigs(); |
|
66
|
2 |
|
$temp_dir = self::getPluginTempRealDir(); |
|
67
|
2 |
|
if (!file_exists($temp_dir)) { |
|
68
|
1 |
|
@mkdir($temp_dir); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
return file_put_contents($cacheFile, sprintf('<?php return %s', var_export($pluginConfigs, true)).';'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* プラグイン設定情報のキャッシュファイルを削除する. |
|
76
|
|
|
* |
|
77
|
|
|
* @return boolean |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public static function removePluginConfigCache() |
|
80
|
|
|
{ |
|
81
|
1 |
|
$cacheFile = self::getPluginConfigCacheFile(); |
|
82
|
1 |
|
if (file_exists($cacheFile)) { |
|
83
|
1 |
|
return unlink($cacheFile); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* プラグイン設定情報のキャッシュファイルパスを返す. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
42 |
|
public static function getPluginConfigCacheFile() |
|
95
|
|
|
{ |
|
96
|
42 |
|
return self::getPluginTempRealDir().'/config_cache.php'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
42 |
|
public static function getPluginTempRealDir() |
|
100
|
|
|
{ |
|
101
|
42 |
|
return __DIR__.'/../../../app/cache/plugin'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
public static function getPluginRealDir() |
|
105
|
|
|
{ |
|
106
|
3 |
|
return __DIR__.'/../../../app/Plugin'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* プラグイン設定情報をパースし, 連想配列で返す. |
|
111
|
|
|
* |
|
112
|
|
|
* すべてのプラグインを探索し、 config.yml 及び event.yml をパースする. |
|
113
|
|
|
* パースした情報を連想配列で返す. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public static function parsePluginConfigs() |
|
118
|
|
|
{ |
|
119
|
3 |
|
$finder = Finder::create() |
|
120
|
3 |
|
->in(self::getPluginRealDir()) |
|
121
|
3 |
|
->directories() |
|
122
|
3 |
|
->depth(0); |
|
123
|
3 |
|
$finder->sortByName(); |
|
124
|
|
|
|
|
125
|
3 |
|
$pluginConfigs = []; |
|
126
|
3 |
|
foreach ($finder as $dir) { |
|
127
|
3 |
|
$code = $dir->getBaseName(); |
|
128
|
3 |
|
if (!$code) { |
|
129
|
|
|
//PHP5.3のgetBaseNameバグ対応 |
|
130
|
|
|
if (PHP_VERSION_ID < 50400) { |
|
131
|
|
|
$code = $dir->getFilename(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
3 |
|
$file = $dir->getRealPath().'/config.yml'; |
|
135
|
3 |
|
$config = null; |
|
|
|
|
|
|
136
|
3 |
|
if (file_exists($file)) { |
|
137
|
3 |
|
$config = Yaml::parse(file_get_contents($file)); |
|
138
|
|
|
} else { |
|
139
|
3 |
|
continue; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
3 |
|
$file = $dir->getRealPath().'/event.yml'; |
|
143
|
3 |
|
$event = null; |
|
144
|
3 |
|
if (file_exists($file)) { |
|
145
|
|
|
$event = Yaml::parse(file_get_contents($file)); |
|
146
|
|
|
} |
|
147
|
3 |
|
if (!is_null($config)) { |
|
148
|
3 |
|
$pluginConfigs[$code] = [ |
|
149
|
3 |
|
'config' => $config, |
|
150
|
3 |
|
'event' => $event, |
|
151
|
|
|
]; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
3 |
|
return $pluginConfigs; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|