GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Compiler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
/**
4
 * This file is part of WebHelper Parser.
5
 *
6
 * (c) James <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace WebHelper\Parser;
12
13
use WebHelper\Parser\Directive\SimpleDirective;
14
use WebHelper\Parser\Exception\InvalidConfigException;
15
16
/**
17
 * Web server configuration generic compiler.
18
 *
19
 * @author James <[email protected]>
20
 */
21
class Compiler
22
{
23
    /**
24
     * The parser instance.
25
     *
26
     * @var Parser
27
     */
28
    private $parser;
29
30
    /**
31
     * The string to match as a starting multi-line directive.
32
     *
33
     * @var string
34
     */
35
    private $startMultiLine;
36
37
    /**
38
     * The string to match as an ending multi-line directive.
39
     *
40
     * @var string
41
     */
42
    private $endMultiLine;
43
44
    /**
45
     * The string to match a simple Directive.
46
     *
47
     * @var string
48
     */
49
    private $simpleDirective;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param string $startMultiLine     match as a starting multi-line directive
55
     * @param string $endMultiLine       match as an ending multi-line directive
56
     * @param string $simpleDirective    match a simple directive
57
     */
58 7
    public function __construct($startMultiLine, $endMultiLine, $simpleDirective)
59
    {
60 7
        $this->startMultiLine = $startMultiLine;
61 7
        $this->endMultiLine = $endMultiLine;
62 7
        $this->simpleDirective = $simpleDirective;
63 7
    }
64
65
    /**
66
     * Sets the parser instance.
67
     *
68
     * @param Parser $parser the parser instance
69
     */
70 7
    public function setParser(Parser $parser)
71
    {
72 7
        $this->parser = $parser;
73
74 7
        return $this;
75
    }
76
77
    /**
78
     * Does a nested array of lines depending on container Directives.
79
     *
80
     * @param array  $activeConfig a clean config array of lines
81
     * @param string $context      the context name
82
     * @param string $value        an optional context value
83
     *
84
     * @return Directive\BlockDirective a full context of directives
85
     */
86 10
    public function doCompile($activeConfig, $context = 'main', $value = '')
87
    {
88 10
        $tempConfig = [];
89
90 10
        while (!empty($activeConfig)) {
91 10
            $lineConfig = array_shift($activeConfig);
92 10
            $tempConfig[] = $this->subCompile($activeConfig, $lineConfig);
93 9
        }
94
95 8
        return $this->buildBlockDirective($context, $value, $tempConfig);
96
    }
97
98
    /**
99
     * Looks for a container directive.
100
     *
101
     * @param array  $activeConfig a clean config array of directives
102
     * @param string $lineConfig   a line
103
     *
104
     * @throws Exception\InvalidConfigException if a simple directive has invalid syntax
105
     *
106
     * @return Directive\DirectiveInterface a directive or a container of directives
107
     */
108 10
    private function subCompile(&$activeConfig, $lineConfig)
109
    {
110 10
        if (preg_match($this->startMultiLine, $lineConfig, $container)) {
111 9
            return $this->findEndingKey(trim($container['key']), trim($container['value']), $activeConfig);
112
        }
113
114 9
        if (!preg_match($this->simpleDirective, $lineConfig, $container)) {
115 1
            throw InvalidConfigException::forSimpleDirectiveSyntaxError($lineConfig);
116
        }
117
118 9
        return $this->buildSimpleDirective(trim($container['key']), trim($container['value']));
119
    }
120
121
    /**
122
     * Finds the end of a container directive.
123
     *
124
     * @param string $context      a container's name
125
     * @param string $contextValue a container's value
126
     * @param array  $activeConfig a clean config array of lines
127
     *
128
     * @throws Exception\InvalidConfigException if a container does not end correctly
129
     *
130
     * @return Directive\BlockDirective a container of directives
131
     */
132 9
    private function findEndingKey($context, $contextValue, &$activeConfig)
133
    {
134 9
        $lines = [];
135 9
        $endMultiLine = sprintf($this->endMultiLine, $context);
136
137 9
        while (!empty($activeConfig)) {
138 8
            $lineConfig = array_shift($activeConfig);
139
140 8
            if (preg_match($endMultiLine, $lineConfig)) {
141 7
                return $this->buildBlockDirective($context, $contextValue, $lines);
142
            }
143
144 8
            $lines[] = $this->subCompile($activeConfig, $lineConfig);
145 8
        }
146
147 1
        throw InvalidConfigException::forEndingKeyNotFound($context);
148
    }
149
150
    /**
151
     * Builds a BlockDirective.
152
     *
153
     * @param string $context      a container's name
154
     * @param string $contextValue a container's value
155
     * @param array  $lines        an array of directives
156
     *
157
     * @return Directive\BlockDirective the BlockDirective
158
     */
159 8
    private function buildBlockDirective($context, $contextValue, $lines)
160
    {
161 8
        $class = 'WebHelper\Parser\Directive\BlockDirective';
162 8
        $known = $this->parser->getServer()->getKnownDirectives();
163 8
        if (in_array($context, array_keys($known))) {
164 3
            if (isset($known[$context]['class'])) {
165 1
                $class = 'WebHelper\Parser\Directive\\'.$known[$context]['class'];
166 1
            }
167 3
        }
168 8
        $block = new $class($context, $contextValue);
169 8
        foreach ($lines as $directive) {
170 8
            $block->add($directive);
171 8
        }
172
173 8
        return $block;
174
    }
175
176
    /**
177
     * Build a SimpleDirective or an InclusionDirective.
178
     *
179
     * Remember that inclusion are parsed as simple directives but are block directives
180
     *
181
     * @see Directive\InclusionDirective Inclusion Doc
182
     *
183
     * @param string $key   a directive's name
184
     * @param string $value a directive's value
185
     *
186
     * @return Directive\SimpleDirective|Directive\InclusionDirective the Directive
187
     */
188 9
    private function buildSimpleDirective($key, $value)
189
    {
190 9
        $known = $this->parser->getServer()->getKnownDirectives();
191 9
        if (in_array($key, array_keys($known))) {
192 2
            if (isset($known[$key]['class'])) {
193 2
                $class = 'WebHelper\Parser\Directive\\'.$known[$key]['class'];
194 2
                return new $class($key, $value, $this->parser);
195
            }
196 1
        }
197
198 9
        return new SimpleDirective($key, $value);
199
    }
200
}
201