|
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
|
|
|
|
|
12
|
|
|
namespace WebHelper\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
15
|
|
|
use WebHelper\Parser\Server\Server; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* WebHelper Factory. |
|
19
|
|
|
* |
|
20
|
|
|
* @author James <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Factory |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var array [description] */ |
|
25
|
|
|
private $servers = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Factory constructor. |
|
29
|
|
|
*/ |
|
30
|
7 |
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
7 |
|
$yaml = new Yaml(); |
|
33
|
7 |
|
$config = $yaml->parse(file_get_contents(__DIR__.'/../res/servers.yml')); |
|
34
|
7 |
|
$this->servers = $config['servers']; |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Builds a parser instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $name a server specification name |
|
41
|
|
|
* |
|
42
|
|
|
* @return ParserInterface a parser instance |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function createParser($name) |
|
45
|
|
|
{ |
|
46
|
7 |
|
$parser = new Parser(); |
|
47
|
|
|
|
|
48
|
7 |
|
$parser->setServer($this->createServer($name)); |
|
49
|
|
|
|
|
50
|
7 |
|
$compiler = new Compiler( |
|
51
|
7 |
|
$parser->getServer()->getStartMultiLine(), |
|
52
|
7 |
|
$parser->getServer()->getEndMultiLine(), |
|
53
|
7 |
|
$parser->getServer()->getSimpleDirective() |
|
54
|
7 |
|
); |
|
55
|
|
|
|
|
56
|
7 |
|
$parser->setCompiler($compiler); |
|
57
|
|
|
|
|
58
|
7 |
|
return $parser; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Builds a server instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name a server specification name |
|
65
|
|
|
* |
|
66
|
|
|
* @return Server\ServerInterface a server instance |
|
67
|
|
|
*/ |
|
68
|
7 |
|
public function createServer($name) |
|
69
|
|
|
{ |
|
70
|
7 |
|
$server = new Server(); |
|
71
|
7 |
|
if (in_array($name, $this->getKnownServers())) { |
|
72
|
|
|
$server |
|
73
|
7 |
|
->setStartMultiLine($this->servers[$name]['directives']['start_multiline']) |
|
74
|
7 |
|
->setEndMultiLine($this->servers[$name]['directives']['end_multiline']) |
|
75
|
7 |
|
->setSimpleDirective($this->servers[$name]['directives']['simple']) |
|
76
|
7 |
|
->setBinaries($this->servers[$name]['controlers']) |
|
77
|
7 |
|
->setDetectionParameter($this->servers[$name]['switch']['detect']) |
|
78
|
7 |
|
->setBeforeMethods($this->servers[$name]['parser']['before']) |
|
79
|
7 |
|
->setAfterMethods($this->servers[$name]['parser']['after']) |
|
80
|
|
|
; |
|
81
|
7 |
|
} |
|
82
|
|
|
|
|
83
|
7 |
|
return $server; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieves known servers from res/servers.yml specifications. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array known server names |
|
90
|
|
|
*/ |
|
91
|
7 |
|
public function getKnownServers() |
|
92
|
|
|
{ |
|
93
|
7 |
|
return array_keys($this->servers); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|