|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Yii 2 config loader |
|
4
|
|
|
* |
|
5
|
|
|
* @see https://github.com/sergeymakinen/yii2-config |
|
6
|
|
|
* @copyright Copyright (c) 2016-2017 Sergey Makinen (https://makinen.ru) |
|
7
|
|
|
* @license https://github.com/sergeymakinen/yii2-config/blob/master/LICENSE The MIT License |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace sergeymakinen\yii\config; |
|
11
|
|
|
|
|
12
|
|
|
use yii\base\InvalidValueException; |
|
13
|
|
|
use yii\helpers\StringHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* PHP bootstrap config loader. |
|
17
|
|
|
*/ |
|
18
|
|
|
class PhpBootstrapLoader extends Loader |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritDoc |
|
22
|
|
|
*/ |
|
23
|
19 |
|
public function compile() |
|
24
|
|
|
{ |
|
25
|
19 |
|
foreach ($this->resolveFiles() as $file) { |
|
26
|
19 |
|
$contents = $this->getPurePhp(file_get_contents($file)); |
|
27
|
19 |
|
if ($contents === false) { |
|
28
|
9 |
|
throw new InvalidValueException("The '{$file}' file is not a pure PHP file."); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
19 |
|
$this->storage->bootstrap[] = $contents; |
|
32
|
19 |
|
} |
|
33
|
19 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritDoc |
|
37
|
|
|
*/ |
|
38
|
28 |
|
public function load() |
|
39
|
|
|
{ |
|
40
|
28 |
|
foreach ($this->resolveFiles() as $file) { |
|
41
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
42
|
28 |
|
require_once $file; |
|
43
|
28 |
|
} |
|
44
|
28 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns a pure PHP code from the input string or `false` if the string is not a pure PHP file. |
|
48
|
|
|
* @param string $string input string. |
|
49
|
|
|
* @return string|false PHP code as a string or `false`. |
|
50
|
|
|
* @since 2.0 |
|
51
|
|
|
*/ |
|
52
|
19 |
|
protected function getPurePhp($string) |
|
53
|
|
|
{ |
|
54
|
19 |
|
$tokens = token_get_all($string); |
|
55
|
19 |
|
$tokenCount = count($tokens); |
|
56
|
|
|
if ( |
|
57
|
|
|
$tokenCount === 0 |
|
58
|
19 |
|
|| $this->isDesiredToken($tokens[0], [T_INLINE_HTML, T_OPEN_TAG_WITH_ECHO]) |
|
59
|
19 |
|
|| $this->isDesiredToken($tokens[$tokenCount - 1], T_INLINE_HTML) |
|
60
|
19 |
|
) { |
|
61
|
6 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
19 |
|
for ($index = 1; $index < $tokenCount; $index++) { |
|
65
|
19 |
|
if ($this->isDesiredToken($tokens[$index], [T_INLINE_HTML, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) { |
|
66
|
3 |
|
return false; |
|
67
|
|
|
} |
|
68
|
19 |
|
} |
|
69
|
|
|
|
|
70
|
19 |
|
if ($this->isDesiredToken($tokens[$tokenCount - 1], T_CLOSE_TAG)) { |
|
71
|
3 |
|
$string = StringHelper::byteSubstr($string, 0, -1 * StringHelper::byteLength($tokens[$tokenCount - 1][1])); |
|
72
|
3 |
|
} |
|
73
|
19 |
|
return trim(StringHelper::byteSubstr($string, StringHelper::byteLength($tokens[0][1]))); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns whether the token is of the desired type. |
|
78
|
|
|
* @param array|string $token |
|
79
|
|
|
* @param int|int[] $type |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
19 |
|
private function isDesiredToken($token, $type) |
|
83
|
|
|
{ |
|
84
|
19 |
|
if (is_array($token) && in_array($token[0], (array) $type, true)) { |
|
85
|
12 |
|
return true; |
|
86
|
|
|
} else { |
|
87
|
19 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|