Skip to content

Commit f919b0e

Browse files
committed
#103127 Include config namespace & update readme
1 parent 771d331 commit f919b0e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,23 @@ You can also specify `response_key` for resource: add `x-lg-resource-response-ke
114114
When specifying `response_key`, you can use the "dot" syntax to specify nesting, for example `data.field`
115115
You can exclude resource generation using `x-lg-skip-resource-generation: true` in route.
116116
You can rename resource Class using `x-lg-resource-class-name: FooResource` in object.
117-
If a resource file already exists it is NOT overriden.
117+
If a resource file already exists it is NOT overridden.
118118
Resource file contains a set of fields according to the specification.
119119
You also need to specify mixin DocBlock to autocomplete resource.
120120

121+
### 'policies' => PoliciesGenerator::class
122+
123+
Generates Laravel Policies for routes.
124+
Destination must be configured with array as namespace instead of string. E.g:
125+
```php
126+
'policies' => [
127+
'namespace' => ["Controllers" => "Policies"]
128+
],
129+
```
130+
* The path must contain a 403 response or the policy will not be generated.
131+
* You can exclude policy generation using `x-lg-skip-policy-generation: true` in route.
132+
* If a policy file already exists it is NOT overridden.
133+
121134
## Contributing
122135

123136
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

src/Generators/PoliciesGenerator.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@
44

55
use cebe\openapi\SpecObjectInterface;
66
use Ensi\LaravelOpenApiServerGenerator\DTO\ParsedRouteHandler;
7+
use InvalidArgumentException;
78
use RuntimeException;
89
use stdClass;
910

1011
class 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

Comments
 (0)