|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSFS\services; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Propel\Runtime\ActiveRecord\ActiveRecordInterface; |
|
7
|
|
|
use PSFS\base\dto\Dto; |
|
8
|
|
|
use PSFS\base\Logger; |
|
9
|
|
|
use PSFS\base\Router; |
|
10
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
|
11
|
|
|
use PSFS\base\types\helpers\InjectorHelper; |
|
12
|
|
|
use PSFS\base\types\SimpleService; |
|
13
|
|
|
use PSFS\base\types\traits\Api\DocumentorHelperTrait; |
|
14
|
|
|
use PSFS\base\types\traits\Api\SwaggerFormaterTrait; |
|
15
|
|
|
use ReflectionClass; |
|
16
|
|
|
use ReflectionException; |
|
17
|
|
|
use ReflectionMethod; |
|
18
|
|
|
use SplFileInfo; |
|
19
|
|
|
use Symfony\Component\Finder\Finder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DocumentorService |
|
23
|
|
|
* @package PSFS\services |
|
24
|
|
|
*/ |
|
25
|
|
|
class DocumentorService extends SimpleService |
|
26
|
|
|
{ |
|
27
|
|
|
use DocumentorHelperTrait; |
|
28
|
|
|
use SwaggerFormaterTrait; |
|
29
|
|
|
|
|
30
|
|
|
const DTO_INTERFACE = Dto::class; |
|
31
|
|
|
const MODEL_INTERFACE = ActiveRecordInterface::class; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @Injectable |
|
35
|
|
|
* @var \PSFS\base\Router route |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $route; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Method that extract all endpoints for each module |
|
41
|
|
|
* @param array $module |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws ReflectionException |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function extractApiEndpoints(array $module) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$modulePath = $module['path'] . DIRECTORY_SEPARATOR . 'Api'; |
|
48
|
1 |
|
$moduleName = $module['name']; |
|
49
|
1 |
|
$endpoints = []; |
|
50
|
1 |
|
if (file_exists($modulePath)) { |
|
51
|
1 |
|
$finder = new Finder(); |
|
52
|
1 |
|
$finder->files()->in($modulePath)->depth('< 2')->name('*.php'); |
|
53
|
1 |
|
if (count($finder)) { |
|
54
|
|
|
/** @var SplFileInfo $file */ |
|
55
|
1 |
|
foreach ($finder as $file) { |
|
56
|
1 |
|
$filename = str_replace([$modulePath, '/'], ['', '\\'], $file->getPathname()); |
|
57
|
1 |
|
$namespace = "\\{$moduleName}\\Api" . str_replace('.php', '', $filename); |
|
58
|
1 |
|
$info = $this->extractApiInfo($namespace, $moduleName); |
|
59
|
1 |
|
if (!empty($info)) { |
|
60
|
1 |
|
$endpoints[$namespace] = $info; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
1 |
|
return $endpoints; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $namespace |
|
70
|
|
|
* @param $module |
|
71
|
|
|
* @return array |
|
72
|
|
|
* @throws ReflectionException |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function extractApiInfo($namespace, $module) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$info = []; |
|
77
|
1 |
|
if (Router::exists($namespace) && !I18nHelper::checkI18Class($namespace)) { |
|
78
|
1 |
|
$reflection = new ReflectionClass($namespace); |
|
79
|
1 |
|
$visible = InjectorHelper::checkIsVisible($reflection->getDocComment()); |
|
80
|
1 |
|
if ($visible && $reflection->isInstantiable()) { |
|
81
|
1 |
|
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
82
|
|
|
try { |
|
83
|
1 |
|
$mInfo = $this->extractMethodInfo($namespace, $method, $reflection, $module); |
|
84
|
1 |
|
if (NULL !== $mInfo) { |
|
85
|
1 |
|
$info[] = $mInfo; |
|
86
|
|
|
} |
|
87
|
|
|
} catch (Exception $e) { |
|
88
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
1 |
|
return $info; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|