Completed
Push — master ( d5a7ee...de5af2 )
by Sergey
01:43
created

PhpBootstrapLoader::isDesiredToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 * Yii 2 config loader.
4
 *
5
 * @see       https://github.com/sergeymakinen/yii2-config
6
 * @copyright Copyright (c) 2016 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/yii2-config/blob/master/LICENSE The MIT License
8
 */
9
10
namespace sergeymakinen\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 9
    public function compile()
24
    {
25 9
        foreach ($this->resolveFiles() as $file) {
26 9
            $contents = $this->getPurePhp(file_get_contents($file));
27 9
            if ($contents === false) {
28 3
                throw new InvalidValueException("The '{$file}' file is not a pure PHP file.");
29
            }
30
31 9
            $this->storage->bootstrap[] = $contents;
32 9
        }
33 9
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 24
    public function load()
39
    {
40 24
        foreach ($this->resolveFiles() as $file) {
41
            /** @noinspection PhpIncludeInspection */
42 24
            require_once $file;
43 24
        }
44 24
    }
45
46
    /**
47
     * Returns a pure PHP code from the input string or false if the string is not a pure PHP file.
48
     *
49
     * @param string $contents
50
     *
51
     * @return string|false
52
     * @since 1.1
53
     */
54 9
    protected function getPurePhp($contents)
55
    {
56 9
        $tokens = token_get_all($contents);
57 9
        $tokenCount = count($tokens);
58
        if (
59
            $tokenCount === 0
60 9
            || $this->isDesiredToken($tokens[0], [T_INLINE_HTML, T_OPEN_TAG_WITH_ECHO])
61 9
            || $this->isDesiredToken($tokens[$tokenCount - 1], T_INLINE_HTML)
62 9
        ) {
63 2
            return false;
64
        }
65
66 9
        for ($index = 1; $index < $tokenCount; $index++) {
67 9
            if ($this->isDesiredToken($tokens[$index], [T_INLINE_HTML, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) {
68 1
                return false;
69
            }
70 9
        }
71
72 9
        return trim(StringHelper::byteSubstr($contents, StringHelper::byteLength($tokens[0][1])));
73
    }
74
75
    /**
76
     * Returns whether the token is of a desired type.
77
     *
78
     * @param array|string $token
79
     * @param int|int[] $type
80
     *
81
     * @return bool
82
     */
83 9
    private function isDesiredToken($token, $type)
84
    {
85 9
        if (is_array($token) && in_array($token[0], (array) $type, true)) {
86 3
            return true;
87
        } else {
88 9
            return false;
89
        }
90
    }
91
}
92