44
55use cebe \openapi \SpecObjectInterface ;
66use Ensi \LaravelOpenApiServerGenerator \DTO \ParsedRouteHandler ;
7+ use InvalidArgumentException ;
78use RuntimeException ;
89use stdClass ;
910
1011class PoliciesGenerator extends BaseGenerator implements GeneratorInterface
1112{
1213 public function generate (SpecObjectInterface $ specObject ): void
1314 {
14- $ policies = $ this ->extractPolicies ($ specObject );
15+ $ namespaceData = $ this ->options ['policies ' ]['namespace ' ] ?? null ;
16+ if (!is_array ($ namespaceData )) {
17+ throw new InvalidArgumentException ("PoliciesGenerator must be configured with array as 'namespace' " );
18+ }
19+
20+ $ policies = $ this ->extractPolicies ($ specObject , $ namespaceData );
1521 $ this ->createPoliciesFiles ($ policies , $ this ->templatesManager ->getTemplate ('Policy.template ' ));
1622 }
1723
18- protected function extractPolicies (SpecObjectInterface $ specObject ): array
24+ protected function extractPolicies (SpecObjectInterface $ specObject, array $ namespaceData ): array
1925 {
26+ $ replaceFromNamespace = array_keys ($ namespaceData )[0 ];
27+ $ replaceToNamespace = array_values ($ namespaceData )[0 ];
28+
2029 $ openApiData = $ specObject ->getSerializableData ();
2130
2231 $ policies = [];
@@ -35,8 +44,8 @@ protected function extractPolicies(SpecObjectInterface $specObject): array
3544 try {
3645 $ namespace = $ this ->getReplacedNamespace (
3746 $ handler ->namespace ,
38- ' Controllers ' ,
39- ' Policies '
47+ $ replaceFromNamespace ,
48+ $ replaceToNamespace
4049 );
4150 } catch (RuntimeException ) {
4251 continue ;
@@ -108,4 +117,4 @@ private function convertMethodsToString(array $methods): string
108117
109118 return implode ("\n\n " , $ methodsStrings );
110119 }
111- }
120+ }
0 commit comments