-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSlimCodeScraper.php
97 lines (81 loc) · 3.08 KB
/
SlimCodeScraper.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
<?php
namespace wapmorgan\OpenApiGenerator\Integration;
use App\Application\Actions\Action;
use Slim\App;
use wapmorgan\OpenApiGenerator\ReflectionsCollection;
use wapmorgan\OpenApiGenerator\Scraper\Endpoint;
use wapmorgan\OpenApiGenerator\Scraper\PathResultWrapper;
use wapmorgan\OpenApiGenerator\Scraper\Result;
use wapmorgan\OpenApiGenerator\Scraper\Server;
use wapmorgan\OpenApiGenerator\Scraper\Specification;
use wapmorgan\OpenApiGenerator\ScraperSkeleton;
abstract class SlimCodeScraper extends ScraperSkeleton
{
protected string $actionClass = Action::class;
/**
* @param string $folder
* @return App
*/
public function getApp(string $folder): App
{
/** @var App $app */
require_once $folder . '/app/app.php';
return $app;
}
/**
* @return PathResultWrapper|null
*/
abstract public function getPathResultWrapper(): ?PathResultWrapper;
public function getTitle()
{
return 'API Specification';
}
/**
* @return array
*/
public function scrape(string $folder): array
{
$app = $this->getApp($folder);
$routes = $app->getRouteCollector()->getRoutes();
$result = [];
$result[0] = new Specification();
$result[0]->version = static::$specificationVersion;
$result[0]->title = static::$specificationTitle;
$result[0]->description = sprintf(static::$specificationDescription, static::$specificationVersion);
foreach ($this->getServers() as $serverUrl) {
$result[0]->servers[] = new Server(['url' => $serverUrl]);
}
$path_wrapper = $this->getPathResultWrapper();
foreach ($routes as $route) {
$endpoint = new Endpoint();
$pattern = $route->getPattern();
$endpoint->id = $pattern;
$endpoint->httpMethod = strtolower(current($route->getMethods()));
$callable = $route->getCallable();
if (is_string($callable) && class_exists($callable) && is_a($callable, $this->actionClass, true)) {
$action_reflection = ReflectionsCollection::getMethod($callable, 'action');
$action_doc = $action_reflection->getDocComment();
$endpoint->callback = [$callable, 'action'];
} else {
$action_doc = null;
}
if (substr_count($pattern, '/') > 1) {
$endpoint->tags[] = substr($pattern, 1, strpos($pattern, '/', 1) - 1);
}
// check for @auth tag
if (
isset($action_doc)
&& !empty($action_auth = $this->getDocParameter($action_doc, 'auth', ''))
) {
if (isset($action_auth) && !empty($action_auth)) {
$this->ensureSecuritySchemeAdded($result[0], $action_auth);
$endpoint->securitySchemes[] = $action_auth;
}
}
$endpoint->resultWrapper = $path_wrapper;
$endpoint->result = 'null';
$result[0]->endpoints[] = $endpoint;
}
return $result;
}
}