1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date: 17.03.16 |
5
|
|
|
* @time: 23:41 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\dev; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use LTDBeget\sphinx\enums\eVersion; |
12
|
|
|
use Symfony\Component\Yaml\Parser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class EnumGenerator |
16
|
|
|
* @package LTDBeget\dev |
17
|
|
|
*/ |
18
|
|
|
final class EnumGenerator |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
protected $fileContents = []; |
22
|
|
|
protected $documentation = []; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->prepareDocumentation(); |
27
|
|
|
$this->prepareContents(); |
28
|
|
|
$this->writeFiles(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* parse and prepare info from docks |
33
|
|
|
*/ |
34
|
|
|
private function prepareDocumentation() |
35
|
|
|
{ |
36
|
|
|
foreach (eVersion::getConstants() as $cons => $version) { |
37
|
|
|
$eVersion = eVersion::get($version); |
38
|
|
|
$documentation = $this->getDocumentation($eVersion); |
39
|
|
|
$this->processDocumentation($documentation); |
40
|
|
|
} |
41
|
|
|
$this->removeDuplicates(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
private function writeFiles() |
46
|
|
|
{ |
47
|
|
|
foreach ($this->fileContents as $fileName => $fileContent) { |
48
|
|
|
$name = $this->getOutputDir() . DIRECTORY_SEPARATOR . $fileName; |
49
|
|
|
file_put_contents($name, $fileContent); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* make enum file contents |
55
|
|
|
*/ |
56
|
|
|
private function prepareContents() |
57
|
|
|
{ |
58
|
|
|
foreach ($this->documentation as $section => $options) { |
59
|
|
|
$Section = ucfirst($section); |
60
|
|
|
$class_name = "e{$Section}Option"; |
61
|
|
|
|
62
|
|
|
$methods = []; |
63
|
|
|
$constants = []; |
64
|
|
|
foreach ($options as $option) { |
65
|
|
|
$method = " * @method static {{CLASS_NAME}} {{CONST}}()"; |
66
|
|
|
$method = str_replace("{{CLASS_NAME}}", $class_name, $method); |
67
|
|
|
$method = str_replace("{{CONST}}", strtoupper($option), $method); |
68
|
|
|
$methods[] = $method; |
69
|
|
|
|
70
|
|
|
$const = ' const {{CONST}} = "{{OPTION_NAME}}";'; |
71
|
|
|
$const = str_replace("{{OPTION_NAME}}", $option, $const); |
72
|
|
|
$const = str_replace("{{CONST}}", strtoupper($option), $const); |
73
|
|
|
$constants[] = $const; |
74
|
|
|
} |
75
|
|
|
$methods = implode("\n", $methods); |
76
|
|
|
$constants = implode("\n", $constants); |
77
|
|
|
$template = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . "enum"); |
78
|
|
|
$template = str_replace("{{CLASS_NAME}}", $class_name, $template); |
79
|
|
|
$template = str_replace("{{MAGIC_METHODS}}", $methods, $template); |
80
|
|
|
$template = str_replace("{{CONSTANTS}}", $constants, $template); |
81
|
|
|
|
82
|
|
|
$this->fileContents[$class_name . ".php"] = $template; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* save names from documentation to this documentation |
89
|
|
|
* @param array $documentation |
90
|
|
|
*/ |
91
|
|
|
private function processDocumentation(array $documentation) |
92
|
|
|
{ |
93
|
|
|
foreach ($documentation as $section => $options) { |
94
|
|
|
if (!array_key_exists($section, $this->documentation)) { |
95
|
|
|
$this->documentation[$section] = []; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($options as $optionName => $option) { |
99
|
|
|
$this->documentation[$section][] = $optionName; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* remove duplicates options names |
106
|
|
|
*/ |
107
|
|
|
private function removeDuplicates() |
108
|
|
|
{ |
109
|
|
|
foreach ($this->documentation as $section => $options) { |
110
|
|
|
$this->documentation[$section] = array_values(array_unique($options)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param eVersion $version |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function getDocumentation(eVersion $version) : array |
120
|
|
|
{ |
121
|
|
|
$documentation = (new Parser())->parse(file_get_contents($this->getFileName($version))); |
122
|
|
|
|
123
|
|
|
return is_array($documentation) ? $documentation : []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* name of file for save parsed data |
128
|
|
|
* @param eVersion $version |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
private function getFileName(eVersion $version) : string |
132
|
|
|
{ |
133
|
|
|
return $this->getPath() . "/documentation_{$version}.yaml"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* name of directory where needs to save parsed data |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
private function getPath() : string |
141
|
|
|
{ |
142
|
|
|
return realpath(__DIR__ . "/../../../sphinx/docs"); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* get path to output dir |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
private function getOutputDir() |
150
|
|
|
{ |
151
|
|
|
return realpath(__DIR__ . "/../sphinx/enums/options"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |