1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/zamm/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Zamm; |
14
|
|
|
|
15
|
|
|
use Maslosoft\EmbeDi\EmbeDi; |
16
|
|
|
use Maslosoft\Zamm\Interfaces\ConverterInterface; |
17
|
|
|
use Maslosoft\Zamm\Converters\PhpConverter; |
18
|
|
|
use Maslosoft\Zamm\Decorators\StarRemover; |
19
|
|
|
use Maslosoft\Zamm\Extractors\AddendumExtractor; |
20
|
|
|
use Maslosoft\Zamm\Interfaces\Extractors\ExtractorInterface; |
21
|
|
|
use Maslosoft\Zamm\Extractors\NullExtractor; |
22
|
|
|
use Maslosoft\Zamm\Interfaces\FileDecoratorInterface; |
23
|
|
|
use Maslosoft\Zamm\FileDecorators\IgnoreFileDecorator; |
24
|
|
|
use Maslosoft\Zamm\FileDecorators\MadeByFileDecorator; |
25
|
|
|
use Maslosoft\Zamm\Interfaces\SourceAccessorInterface; |
26
|
|
|
use Maslosoft\Zamm\Outputs\FileOutput; |
27
|
|
|
use Maslosoft\Zamm\Interfaces\OutputInterface; |
28
|
|
|
use Maslosoft\Zamm\Renderers\ClassRenderer; |
29
|
|
|
use Maslosoft\Zamm\Interfaces\Renderers\ClassRendererInterface; |
30
|
|
|
use Maslosoft\Zamm\Interfaces\Renderers\MethodRendererInterface; |
31
|
|
|
use Maslosoft\Zamm\Interfaces\Renderers\PropertyRendererInterface; |
32
|
|
|
use Maslosoft\Zamm\Interfaces\Renderers\RendererInterface; |
33
|
|
|
use Maslosoft\Zamm\Renderers\MethodRenderer; |
34
|
|
|
use Maslosoft\Zamm\Renderers\PropertyRenderer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Zamm: PHP Doc extractor for code fragments |
38
|
|
|
* This class allows extracting code fragments into documentation. |
39
|
|
|
* It is designed to be IDE friendly. It should autocomplete and automatically reflect code refactors if properly used. |
40
|
|
|
* This documentation is also processed with Zamm. |
41
|
|
|
* |
42
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
43
|
|
|
*/ |
44
|
|
|
class Zamm implements SourceAccessorInterface |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
use Traits\SourceMagic; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @deprecated |
51
|
|
|
* TODO See if it's used somewhere |
52
|
|
|
*/ |
53
|
|
|
const Version = '1.0.0'; |
|
|
|
|
54
|
|
|
const MadeBy = 'Generated by Maslosoft Zamm'; |
|
|
|
|
55
|
|
|
const DefaultInstanceId = 'zamm'; |
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Input for documentation |
59
|
|
|
* Array of folders and files to process. |
60
|
|
|
* @var string[] |
61
|
|
|
*/ |
62
|
|
|
public $input = [ |
63
|
|
|
'docs' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
public $output = [ |
67
|
|
|
|
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Configuration of decorators. |
72
|
|
|
* This should be array with keys of renderer interface names and values of decorator classes. |
73
|
|
|
* @var string[][] |
74
|
|
|
*/ |
75
|
|
|
public $decorators = [ |
76
|
|
|
// All around decorators |
77
|
|
|
RendererInterface::class => [ |
78
|
|
|
StarRemover::class |
79
|
|
|
], |
80
|
|
|
// Class decorators |
81
|
|
|
ClassRendererInterface::class => [ |
82
|
|
|
], |
83
|
|
|
// Property decorators |
84
|
|
|
PropertyRendererInterface::class => [ |
85
|
|
|
], |
86
|
|
|
// Method decorators |
87
|
|
|
MethodRendererInterface::class => [ |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Configuration of file decorators. |
93
|
|
|
* This should be array with keys of converter names and values of file decorator classes. |
94
|
|
|
* @var string[][] |
95
|
|
|
*/ |
96
|
|
|
public $fileDecorators = [ |
97
|
|
|
// All file decorators |
98
|
|
|
FileDecoratorInterface::class => [ |
99
|
|
|
MadeByFileDecorator::class |
100
|
|
|
], |
101
|
|
|
// PHP converter decorators |
102
|
|
|
PhpConverter::class => [ |
103
|
|
|
IgnoreFileDecorator::class |
104
|
|
|
] |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Converters |
109
|
|
|
* Array of class names of converters. These will be applied in order specified here, to all files. |
110
|
|
|
* All converters should implement `IConverter` interface. |
111
|
|
|
* @see ConverterInterface |
112
|
|
|
* @var string[] |
113
|
|
|
*/ |
114
|
|
|
public $converters = [ |
115
|
|
|
PhpConverter::class |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Outputs classes |
120
|
|
|
* Array of class names of outputs. These will be applied in order specified here, to all files. |
121
|
|
|
* All outputs should implement `IOutput` interface. |
122
|
|
|
* @see OutputInterface |
123
|
|
|
* @var string[] |
124
|
|
|
*/ |
125
|
|
|
public $outputs = [ |
126
|
|
|
FileOutput::class |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Extractor class name. |
131
|
|
|
* This class will be used to extract source fragments. Defaults to `AddendumExtractor`. |
132
|
|
|
* It implements `IExtractor` interface. |
133
|
|
|
* @see AddendumExtractor |
134
|
|
|
* @see ExtractorInterface |
135
|
|
|
* @var string |
136
|
|
|
*/ |
137
|
|
|
public $extractor = AddendumExtractor::class; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Extractor instance |
141
|
|
|
* @var ExtractorInterface |
142
|
|
|
*/ |
143
|
|
|
private $_extractor = null; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Working class name |
147
|
|
|
* @var string|null |
148
|
|
|
*/ |
149
|
|
|
private $_className = ''; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* EmbeDi instance |
153
|
|
|
* @var EmbeDi |
154
|
|
|
*/ |
155
|
|
|
private $_di = null; |
156
|
|
|
|
157
|
|
|
public function __construct($className = null) |
158
|
|
|
{ |
159
|
|
|
$this->_className = $className; |
160
|
|
|
$this->_di = new EmbeDi(self::DefaultInstanceId); |
161
|
|
|
$this->_di->configure($this); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function init() |
165
|
|
|
{ |
166
|
|
|
$this->_di->store($this); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function methods() |
170
|
|
|
{ |
171
|
|
|
/** |
172
|
|
|
* TODO Return all method names |
173
|
|
|
*/ |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function properties() |
177
|
|
|
{ |
178
|
|
|
/** |
179
|
|
|
* TODO Return all property names |
180
|
|
|
*/ |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function method($name) |
184
|
|
|
{ |
185
|
|
|
return new MethodRenderer($this->getExtractor(), $name); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function property($name) |
189
|
|
|
{ |
190
|
|
|
return new PropertyRenderer($this->getExtractor(), $name); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function setExtractor(ExtractorInterface $extractor) |
194
|
|
|
{ |
195
|
|
|
$this->_extractor = $extractor; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getExtractor() |
199
|
|
|
{ |
200
|
|
|
if (null === $this->_extractor) |
201
|
|
|
{ |
202
|
|
|
|
203
|
|
|
if (!$this->_className) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$extractorClass = NullExtractor::class; |
206
|
|
|
} |
207
|
|
|
else |
208
|
|
|
{ |
209
|
|
|
$extractorClass = $this->extractor; |
210
|
|
|
} |
211
|
|
|
$extractor = new $extractorClass(); |
212
|
|
|
$extractor->setClassName($this->_className); |
213
|
|
|
$this->_extractor = $extractor; |
214
|
|
|
} |
215
|
|
|
return $this->_extractor; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public static function __callStatic($name, $arguments) |
219
|
|
|
{ |
220
|
|
|
$className = get_called_class(); |
221
|
|
|
return new MethodRenderer((new Zamm($className))->getExtractor(), $name); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* |
226
|
|
|
* @return ClassRenderer |
227
|
|
|
*/ |
228
|
|
|
public function __toString() |
229
|
|
|
{ |
230
|
|
|
return (string) new ClassRenderer($this->getExtractor()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|