-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVisualizerTask.php
520 lines (454 loc) · 14.8 KB
/
VisualizerTask.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
declare(strict_types=1);
namespace Phing\Task\Ext\Visualizer;
use Jawira\PlantUmlClient\Client;
use Phing\Exception\BuildException;
use Phing\Io\FileReader;
use Phing\Io\FileWriter;
use Phing\Io\File;
use Phing\Project;
use Phing\Task\Ext\Http\HttpTask;
use Phing\Util\StringHelper;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;
use XSLTProcessor;
use function array_reduce;
use function filter_var;
use function reset;
use function simplexml_load_string;
use function strval;
use const FILTER_VALIDATE_URL;
/**
* Class VisualizerTask
*
* VisualizerTask creates diagrams using buildfiles, these diagrams represents calls and depends among targets.
*
* @author Jawira Portugal
*/
class VisualizerTask extends HttpTask
{
protected const ARROWS_HORIZONTAL = 'horizontal';
protected const ARROWS_VERTICAL = 'vertical';
protected const FORMAT_EPS = 'eps';
protected const FORMAT_PNG = 'png';
protected const FORMAT_PUML = 'puml';
protected const FORMAT_SVG = 'svg';
protected const SERVER = 'http://www.plantuml.com/plantuml';
protected const STATUS_OK = 200;
protected const XSL_CALLS = __DIR__ . '/calls.xsl';
protected const XSL_FOOTER = __DIR__ . '/footer.xsl';
protected const XSL_HEADER = __DIR__ . '/header.xsl';
protected const XSL_TARGETS = __DIR__ . '/targets.xsl';
/**
* @var string Diagram format
*/
protected $format;
/**
* @var null|string Location in disk where diagram is saved
*/
protected $destination;
/**
* @var string PlantUml server
*/
protected $server;
/**
* @var string Arrows' direction
*/
protected $direction;
/**
* @var bool Show title in diagram
*/
protected $showTitle;
/**
* @var bool Show description in diagram
*/
protected $showDescription;
/**
* @var string Text to display
*/
protected $footer;
/**
* Setting some default values and checking requirements
*/
public function init(): void
{
parent::init();
if (!class_exists(Client::class)) {
$exceptionMessage = get_class($this) . ' requires "jawira/plantuml-client" library';
}
if (!class_exists(XSLTProcessor::class)) {
$exceptionMessage = get_class($this) . ' requires XSL extension';
}
if (!class_exists(SimpleXMLElement::class)) {
$exceptionMessage = get_class($this) . ' requires SimpleXML extension';
}
if (isset($exceptionMessage)) {
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException($exceptionMessage);
}
$this->setFormat(self::FORMAT_PNG);
$this->setServer(self::SERVER);
$this->setDirection(self::ARROWS_VERTICAL);
$this->setShowTitle(true);
$this->setShowDescription(false);
$this->setFooter('');
}
/**
* The main entry point method.
*
* @throws \Phing\Io\IOException
* @throws \Exception
*/
public function main(): void
{
$pumlDiagram = $this->generatePumlDiagram();
$destination = $this->resolveImageDestination();
$format = $this->getFormat();
$image = $this->generateImage($pumlDiagram, $format);
$this->saveToFile($image, $destination);
}
/**
* Retrieves loaded buildfiles and generates a PlantUML diagram
*
* @return string
* @throws \Phing\Io\IOException
*/
protected function generatePumlDiagram(): string
{
/**
* @var \Phing\Parser\XmlContext $xmlContext
*/
$xmlContext = $this->getProject()
->getReference("phing.parsing.context");
$importStack = $xmlContext->getImportStack();
return $this->generatePuml($importStack);
}
/**
* Read through provided buildfiles and generates a PlantUML diagram
*
* @param \Phing\Io\File[] $buildFiles
*
* @return string
* @throws \Phing\Io\IOException
*/
protected function generatePuml(array $buildFiles): string
{
if (!($firstBuildFile = reset($buildFiles))) {
$exceptionMessage = 'No buildfile to process';
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException($exceptionMessage);
}
$puml = $this->transformToPuml($firstBuildFile, self::XSL_HEADER);
$puml = array_reduce($buildFiles, function (string $carry, File $buildFile) {
return $carry . $this->transformToPuml($buildFile, self::XSL_CALLS);
}, $puml);
$puml = array_reduce($buildFiles, function (string $carry, File $buildFile) {
return $carry . $this->transformToPuml($buildFile, self::XSL_TARGETS);
}, $puml);
$puml .= $this->transformToPuml($firstBuildFile, self::XSL_FOOTER);
return $puml;
}
/**
* Transforms buildfile using provided xsl file
*
* @param \Phing\Io\File $buildfile Path to buildfile
* @param string $xslFile XSLT file
*
* @return string
* @throws \Phing\Io\IOException
*/
protected function transformToPuml(File $buildfile, string $xslFile): string
{
$xml = $this->loadXmlFile($buildfile->getPath());
$xsl = $this->loadXmlFile($xslFile);
$processor = new XSLTProcessor();
$processor->setParameter('', 'direction', $this->getDirection());
$processor->setParameter('', 'description', strval($this->getProject()->getDescription()));
$processor->setParameter('', 'showTitle', strval($this->isShowTitle()));
$processor->setParameter('', 'showDescription', strval($this->isShowDescription()));
$processor->setParameter('', 'footer', $this->getFooter());
$processor->importStylesheet($xsl);
return $processor->transformToXml($xml) . PHP_EOL;
}
/**
* Load XML content from a file
*
* @param string $xmlFile XML or XSLT file
*
* @return \SimpleXMLElement
* @throws \Phing\Io\IOException
*/
protected function loadXmlFile(string $xmlFile): SimpleXMLElement
{
$xmlContent = (new FileReader($xmlFile))->read();
$xml = simplexml_load_string($xmlContent);
if (!($xml instanceof SimpleXMLElement)) {
$exceptionMessage = "Error loading XML file: $xmlFile";
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException($exceptionMessage);
}
return $xml;
}
/**
* Get the image's final location
*
* @return \Phing\Io\File
* @throws \Phing\Io\IOException
*/
protected function resolveImageDestination(): File
{
$phingFile = $this->getProject()->getProperty('phing.file');
$format = $this->getFormat();
$candidate = $this->getDestination();
$path = $this->resolveDestination($phingFile, $format, $candidate);
return new File($path);
}
/**
* @return string
*/
public function getFormat(): string
{
return $this->format;
}
/**
* Sets and validates diagram's format
*
* @param string $format
*
* @return \Phing\Task\Ext\VisualizerTask
*/
public function setFormat(string $format): VisualizerTask
{
switch ($format) {
case self::FORMAT_PUML:
case self::FORMAT_PNG:
case self::FORMAT_EPS:
case self::FORMAT_SVG:
$this->format = $format;
break;
default:
$exceptionMessage = "'$format' is not a valid format";
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException($exceptionMessage);
}
return $this;
}
/**
* @return null|string
*/
public function getDestination(): ?string
{
return $this->destination;
}
/**
* @param null|string $destination
*
* @return \Phing\Task\Ext\VisualizerTask
*/
public function setDestination(?string $destination): self
{
$this->destination = $destination;
return $this;
}
/**
* Figure diagram's file path
*
* @param string $buildfilePath Path to main buildfile, this is used as fallback dir.
* @param string $format Extension to use.
* @param null|string $destination Desired destination provided by user.
*
* @return string
*/
protected function resolveDestination(string $buildfilePath, string $format, ?string $destination): string
{
$buildfileInfo = pathinfo($buildfilePath);
// Fallback
if (empty($destination)) {
$destination = $buildfileInfo['dirname'];
}
// Adding filename if necessary
if (is_dir($destination)) {
$destination .= DIRECTORY_SEPARATOR . $buildfileInfo['filename'] . '.' . $format;
}
// Parent directory must exist
if (!is_dir(dirname($destination))) {
$exceptionMessage = "Directory '$destination' is invalid";
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException(sprintf($exceptionMessage, $destination));
}
// Adding right extension if necessary
if (!StringHelper::endsWith(".$format", $destination)) {
$destination .= ".$format";
}
return $destination;
}
/**
* Generates an actual image using PlantUML code
*
* @param string $pumlDiagram
* @param string $format
*
* @return string
* @throws \Exception
*/
protected function generateImage(string $pumlDiagram, string $format): string
{
if ($format === self::FORMAT_PUML) {
$this->log('Bypassing, no need to call server', Project::MSG_DEBUG);
return $pumlDiagram;
}
$this->prepareImageUrl($pumlDiagram, $this->getFormat());
$response = $this->request();
$this->processResponse($response); // used for status validation
return $response->getBody()->getContents();
}
/**
* Prepares URL from where image will be downloaded
*
* @param string $pumlDiagram
* @param string $format
* @throws \Jawira\PlantUmlClient\ClientException
*/
protected function prepareImageUrl(string $pumlDiagram, string $format): void
{
$server = $this->getServer();
$this->log("Server: $server", Project::MSG_VERBOSE);
$imageUrl = (new Client())->setServer($server)
->generateUrl($pumlDiagram, $format);
$this->log($imageUrl, Project::MSG_DEBUG);
$this->setUrl($imageUrl);
}
/**
* @return string
*/
public function getServer(): string
{
return $this->server;
}
/**
* @param string $server
*
* @return \Phing\Task\Ext\VisualizerTask
*/
public function setServer(string $server): self
{
if (!filter_var($server, FILTER_VALIDATE_URL)) {
$exceptionMessage = 'Invalid PlantUml server';
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException($exceptionMessage);
}
$this->server = $server;
return $this;
}
/**
* @return string
*/
public function getDirection(): string
{
return $this->direction;
}
/**
* @param string $direction
*
* @return \Phing\Task\Ext\VisualizerTask
*/
public function setDirection(string $direction): self
{
$this->direction = $direction === self::ARROWS_HORIZONTAL ? self::ARROWS_HORIZONTAL : self::ARROWS_VERTICAL;
return $this;
}
/**
* @return bool
*/
public function isShowTitle(): bool
{
return $this->showTitle;
}
/**
* @param bool $showTitle
*/
public function setShowTitle(bool $showTitle): void
{
$this->showTitle = $showTitle;
}
/**
* @return bool
*/
public function isShowDescription(): bool
{
return $this->showDescription;
}
/**
* @param bool $showDescription
*/
public function setShowDescription(bool $showDescription): void
{
$this->showDescription = $showDescription;
}
/**
* Receive server's response
*
* This method validates `$response`'s status
*
* @param \Psr\Http\Message\ResponseInterface $response Response from server
*
* @return void
*/
protected function processResponse(ResponseInterface $response): void
{
$status = $response->getStatusCode();
$reasonPhrase = $response->getReasonPhrase();
$this->log("Response status: $status", Project::MSG_DEBUG);
$this->log("Response reason: $reasonPhrase", Project::MSG_DEBUG);
if ($status !== self::STATUS_OK) {
$exceptionMessage = "Request unsuccessful. Response from server: $status $reasonPhrase";
$this->log($exceptionMessage, Project::MSG_ERR);
throw new BuildException($exceptionMessage);
}
}
/**
* Save provided $content string into $destination file
*
* @param string $content Content to save
* @param \Phing\Io\File $destination Location where $content is saved
*
* @return void
*/
protected function saveToFile(string $content, File $destination): void
{
$path = $destination->getPath();
$this->log("Writing: $path");
(new FileWriter($destination))->write($content);
}
/**
* @return string
*/
public function getFooter(): string
{
return $this->footer;
}
/**
* @param string $footer
*/
public function setFooter(string $footer): void
{
$this->footer = $footer;
}
}