Failed Conditions
Branch experimental/sf (68db07)
by Kentaro
42:17 queued 33:39
created

ConfigManager::writePluginConfigCache()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 3.0175
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$debug" missing
Loading history...
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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