33namespace Ensi \LaravelOpenApiServerGenerator \Generators ;
44
55use cebe \openapi \SpecObjectInterface ;
6+ use Ensi \LaravelOpenApiServerGenerator \DTO \ParsedRouteHandler ;
67use RuntimeException ;
8+ use stdClass ;
79
810class PoliciesGenerator extends BaseGenerator implements GeneratorInterface
911{
@@ -13,7 +15,6 @@ public function generate(SpecObjectInterface $specObject): void
1315 $ this ->createPoliciesFiles ($ policies , $ this ->templatesManager ->getTemplate ('Policy.template ' ));
1416 }
1517
16- // TODO: предварительная версия, необходим рефакторинг и доп. проверки
1718 protected function extractPolicies (SpecObjectInterface $ specObject ): array
1819 {
1920 $ openApiData = $ specObject ->getSerializableData ();
@@ -22,36 +23,31 @@ protected function extractPolicies(SpecObjectInterface $specObject): array
2223 $ paths = $ openApiData ->paths ?: [];
2324 foreach ($ paths as $ routes ) {
2425 foreach ($ routes as $ route ) {
25- if (!empty ( $ route ->{ ' x-lg-skip-policy-generation ' } )) {
26+ if (!$ this -> routeValidation ( $ route )) {
2627 continue ;
2728 }
2829
29- if (empty ($ route ->{'x-lg-handler ' })) {
30- continue ;
31- }
32-
33- $ response = $ route ->responses ->{403 } ?? null ;
34- if (!$ response ) {
30+ $ handler = $ this ->routeHandlerParser ->parse ($ route ->{'x-lg-handler ' });
31+ if (!$ this ->handlerValidation ($ handler )) {
3532 continue ;
3633 }
3734
38- $ handler = $ this ->routeHandlerParser ->parse ($ route ->{'x-lg-handler ' });
39-
4035 try {
41- $ namespace = $ this ->getReplacedNamespace ($ handler ->namespace , 'Controllers ' , 'Policies ' );
42- $ className = $ handler ->class . 'Policy ' ;
36+ $ namespace = $ this ->getReplacedNamespace (
37+ $ handler ->namespace ,
38+ 'Controllers ' ,
39+ 'Policies '
40+ );
4341 } catch (RuntimeException ) {
4442 continue ;
4543 }
4644
47- if (empty ($ handler ->method )) {
48- continue ;
49- }
45+ $ className = $ handler ->class . 'Policy ' ;
46+ $ methods = [$ handler ->method ];
5047
5148 if (isset ($ policies ["$ namespace \\$ className " ])) {
52- $ policies ["$ namespace \\$ className " ]['methods ' ][] = $ handler -> method ;
49+ $ policies ["$ namespace \\$ className " ]['methods ' ][] = $ methods [ 0 ] ;
5350 } else {
54- $ methods = [$ handler ->method ];
5551 $ policies ["$ namespace \\$ className " ] = compact ('className ' , 'namespace ' , 'methods ' );
5652 }
5753 }
@@ -60,7 +56,6 @@ protected function extractPolicies(SpecObjectInterface $specObject): array
6056 return $ policies ;
6157 }
6258
63- // TODO: протестировать
6459 protected function createPoliciesFiles (array $ policies , string $ template ): void
6560 {
6661 foreach ($ policies as ['className ' => $ className , 'namespace ' => $ namespace , 'methods ' => $ methods ]) {
@@ -74,24 +69,40 @@ protected function createPoliciesFiles(array $policies, string $template): void
7469 $ this ->replacePlaceholders ($ template , [
7570 '{{ namespace }} ' => $ namespace ,
7671 '{{ className }} ' => $ className ,
77- '{{ methods }} ' => $ this ->convertToString ($ methods ),
72+ '{{ methods }} ' => $ this ->convertMethodsToString ($ methods ),
7873 ])
7974 );
80-
81- die ();
8275 }
8376 }
8477
85- private function convertToString (array $ methods ): string
78+ private function routeValidation (stdClass $ route ): bool
79+ {
80+ return match (true ) {
81+ !empty ($ route ->{'x-lg-skip-policy-generation ' }),
82+ empty ($ route ->{'x-lg-handler ' }),
83+ empty ($ route ->responses ->{403 }) => false ,
84+ default => true
85+ };
86+ }
87+
88+ private function handlerValidation (ParsedRouteHandler $ handler ): bool
89+ {
90+ return match (true ) {
91+ empty ($ handler ->namespace ),
92+ empty ($ handler ->class ),
93+ empty ($ handler ->method ) => false ,
94+ default => true
95+ };
96+ }
97+
98+ private function convertMethodsToString (array $ methods ): string
8699 {
87100 $ methodsStrings = [];
88101
89102 foreach ($ methods as $ method ) {
90103 $ methodsStrings [] = $ this ->replacePlaceholders (
91104 $ this ->templatesManager ->getTemplate ('PolicyGate.template ' ),
92- [
93- '{{ method }} ' => $ method ,
94- ]
105+ ['{{ method }} ' => $ method ]
95106 );
96107 }
97108
0 commit comments