1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright (c) 2016 ublaboo <[email protected]> |
7
|
|
|
* @author Pavel Janda <[email protected]> |
8
|
|
|
* @package Ublaboo |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ublaboo\ApiDocu; |
12
|
|
|
|
13
|
|
|
use Nette\Application\IRouter; |
14
|
|
|
use Nette\Application\Request; |
15
|
|
|
use Nette\Application\Routers\RouteList; |
16
|
|
|
use Nette\Http; |
17
|
|
|
use Ublaboo\ApiRouter\ApiRoute; |
18
|
|
|
|
19
|
|
|
class Starter |
20
|
|
|
{ |
21
|
|
|
public const API_DOCU_STARTER_QUERY_KEY_TARGET = '__apiDocu'; |
22
|
|
|
public const API_DOCU_STARTER_QUERY_KEY_GENERATE = '__apiDocuGenerate'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Generator |
26
|
|
|
*/ |
27
|
|
|
private $generator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var IRouter |
31
|
|
|
*/ |
32
|
|
|
private $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Http\Response |
36
|
|
|
*/ |
37
|
|
|
private $response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Http\Request |
41
|
|
|
*/ |
42
|
|
|
private $httpRequest; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Generator $generator |
47
|
|
|
* @param IRouter $router |
48
|
|
|
* @param Http\Response $response |
49
|
|
|
* @param Http\Request $httpRequest |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
Generator $generator, |
53
|
|
|
IRouter $router, |
54
|
|
|
Http\Response $response, |
55
|
|
|
Http\Request $httpRequest |
56
|
|
|
) { |
57
|
|
|
$this->generator = $generator; |
58
|
|
|
$this->router = $router; |
59
|
|
|
|
60
|
|
|
$this->response = $response; |
61
|
|
|
$this->httpRequest = $httpRequest; |
62
|
|
|
|
63
|
|
|
if ($router instanceof RouteList) { |
64
|
|
|
$this->attachEvents(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Event thatis firex when particular ApiRoute is matched |
71
|
|
|
*/ |
72
|
|
|
public function routeMatched(ApiRoute $route, Request $request): void |
73
|
|
|
{ |
74
|
|
|
if (($format = $request->getParameter(self::API_DOCU_STARTER_QUERY_KEY_GENERATE)) !== null) { |
75
|
|
|
$this->generator->generateAll($this->router); |
76
|
|
|
|
77
|
|
|
exit(0); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (($format = $request->getParameter(self::API_DOCU_STARTER_QUERY_KEY_TARGET)) !== null) { |
81
|
|
|
$this->generator->generateTarget($route, $request); |
82
|
|
|
|
83
|
|
|
exit(0); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Find ApiRoutes and add listener to each ApiRoute::onMatch event |
90
|
|
|
*/ |
91
|
|
|
protected function attachEvents(): void |
92
|
|
|
{ |
93
|
|
|
foreach ($this->router as $route) { |
|
|
|
|
94
|
|
|
if ($route instanceof ApiRoute) { |
95
|
|
|
$route->onMatch[] = [$this, 'routeMatched']; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|