-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriter.php
More file actions
124 lines (96 loc) · 4.19 KB
/
Copy pathWriter.php
File metadata and controls
124 lines (96 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Scraper;
use Riimu\Kit\PHPEncoder\PHPEncoder;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\DocBlock\Tag\ReturnTag;
use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Reflection\ClassReflection;
class Writer
{
private $path;
private $namespace;
private $arrayEncoder;
private $operations = [];
public function __construct($path, $namespace, array $operations)
{
$this->path = $path;
$this->namespace = $namespace;
$this->operations = $operations;
$this->arrayEncoder = new PHPEncoder();
}
public function write()
{
$this->writeApiFile();
$this->writeParamFile();
}
private function writeApiFile()
{
$file = $this->path . DIRECTORY_SEPARATOR . 'Api.php';
if (file_exists($file) && class_exists($this->namespace . '\\Api')) {
$class = new ClassReflection($this->namespace . '\\Api');
$apiClass = ClassGenerator::fromReflection($class);
} else {
$apiClass = new ClassGenerator('Api', $this->namespace, null, 'OpenStack\\Common\\Api\\AbstractApi');
}
if (!$apiClass->hasMethod('__construct')) {
$apiClass->addProperty('params', null, PropertyGenerator::FLAG_PRIVATE);
$apiClass->addMethod('__construct', [], MethodGenerator::FLAG_PUBLIC, '$this->params = new Params;');
}
foreach ($this->operations as $operation) {
$path = str_replace(['{', '}'], '', $operation['path']);
$name = strtolower($operation['method']) . ucfirst(substr($path, strrpos($path, DIRECTORY_SEPARATOR) + 1));
if ($apiClass->hasMethod($name)) {
continue;
}
foreach ($operation['params'] as $kName => $arr) {
$operation['params'][$kName] = '$this->params->' . $kName . ucfirst($arr['location']) . '()';
}
$body = sprintf("return %s;", $this->arrayEncoder->encode($operation, ['array.align' => true]));
$body = str_replace("'$", '$', $body);
$body = str_replace("()'", '()', $body);
$docblock = new DocBlockGenerator(
sprintf("Returns information about %s %s HTTP operation", $operation['method'], $operation['path']),
null,
[new ReturnTag(['array'])]
);
$apiClass->addMethod($name, [], MethodGenerator::FLAG_PUBLIC, $body, $docblock);
}
$output = sprintf("<?php\n\n%s", $apiClass->generate());
file_put_contents($file, $output);
}
private function writeParamFile()
{
$file = $this->path . DIRECTORY_SEPARATOR . 'Params.php';
if (file_exists($file) && class_exists($this->namespace . '\\Params')) {
$class = new ClassReflection($this->namespace . '\\Params');
$paramsClass = ClassGenerator::fromReflection($class);
} else {
$paramsClass = new ClassGenerator('Params', $this->namespace, null, 'OpenStack\\Common\\Api\\AbstractParams');
}
foreach ($this->operations as $operation) {
$params = $operation['params'];
if (empty($params)) {
continue;
}
foreach ($params as $paramName => $paramVal) {
$name = $paramName . ucfirst($paramVal['location']);
if ($paramsClass->hasMethod($name)) {
continue;
}
$body = sprintf("return %s;", $this->arrayEncoder->encode($paramVal, ['array.align' => true]));
$body = str_replace("'$", '$', $body);
$body = str_replace("()'", '()', $body);
$docblock = new DocBlockGenerator(
sprintf("Returns information about %s parameter", $paramName),
null,
[new ReturnTag(['array'])]
);
$paramsClass->addMethod($name, [], MethodGenerator::FLAG_PUBLIC, $body, $docblock);
}
}
$output = sprintf("<?php\n\n%s", $paramsClass->generate());
file_put_contents($file, $output);
}
}