-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add method to manage withRouting options #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1a32fc3
1ec1f91
8071f7f
05bac0d
b3a142e
08bb80f
a47a861
d3835da
58ade35
b307beb
bfd5cb5
c513952
57580d9
7c35d34
e12f780
b08bfbe
1bb478b
b2f486c
d6c0d71
0c71342
0cebe13
d5b78c8
6da6f6d
37a62d3
58d7380
50acbe6
f0d48df
769d14e
7a57ad2
13a8f09
5e41979
dd730fd
fee25ee
3445a5f
0f2c548
3092f2d
ecd449f
f3a0605
9bba11b
7aaa525
dd1402c
a57198d
94a51ea
78d54df
751d0a8
1427f86
d550bee
b7a9305
b9002a1
235bed2
7be3a6b
1aef897
14f8da6
cf81906
0fd37c4
b0928d8
5917f78
0de859c
b6c67d6
17ad6a7
dbb2703
48fbb27
4c3c2fe
7647f88
2e17f0f
5619273
c806788
0609ce7
f90a799
90dfe63
96d468a
821815b
e5d10db
5d9abb8
9ada00c
a8f4ece
ab48bc1
2abe022
4415785
043fac8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Nodes; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use RonasIT\Larabuilder\Traits\PreformattedNodesHelperTrait; | ||
|
|
||
| /** | ||
| * Used to insert expression code with saving original formatting | ||
| */ | ||
| class PreformattedExpression extends Expr | ||
| { | ||
| use PreformattedNodesHelperTrait; | ||
|
|
||
| public function __construct( | ||
| public string $value, | ||
| public array $attributes = [], | ||
| ) { | ||
| parent::__construct($this->attributes); | ||
|
|
||
| $this->initPreformattedNode($this->value); | ||
| } | ||
|
|
||
| public function getSubNodeNames(): array | ||
| { | ||
| return ['value']; | ||
| } | ||
|
|
||
| public function getType(): string | ||
| { | ||
| return 'Expr_PreformattedExpression'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Traits; | ||
|
|
||
| use Illuminate\Support\Str; | ||
| use PhpParser\Node\Stmt\Nop; | ||
| use PhpParser\ParserFactory; | ||
|
|
||
| trait PreformattedNodesHelperTrait | ||
| { | ||
| public readonly array $code; | ||
|
|
||
| protected function initPreformattedNode(string &$value): void | ||
| { | ||
| $value = Str::chopStart($value, '<?php'); | ||
|
|
||
| $this->code = $this->parsePHPCode($value); | ||
| } | ||
|
|
||
| protected function parsePHPCode(string $code): array | ||
| { | ||
| $nodes = new ParserFactory()->createForHostVersion()->parse("<?php\n{$code};"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller passes invalid preformatted code, for example an invalid Useful? React with 👍 / 👎. |
||
|
|
||
| if (end($nodes) instanceof Nop) { | ||
| array_pop($nodes); | ||
| } | ||
|
artengin marked this conversation as resolved.
|
||
|
|
||
| return $nodes; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\ValueOptions; | ||
|
|
||
| use Illuminate\Foundation\Configuration\ApplicationBuilder; | ||
| use InvalidArgumentException; | ||
| use ReflectionMethod; | ||
|
|
||
| readonly class RoutingOption | ||
| { | ||
| public function __construct(string $key) | ||
| { | ||
| $this->validateKey($key); | ||
| } | ||
|
|
||
| private function validateKey(string $key): void | ||
| { | ||
| $params = new ReflectionMethod(ApplicationBuilder::class, 'withRouting')->getParameters(); | ||
|
|
||
| $allowed = array_map(fn ($param) => $param->getName(), $params); | ||
|
|
||
| if (!in_array($key, $allowed)) { | ||
| $list = implode("\n", $allowed); | ||
|
|
||
| throw new InvalidArgumentException("Unknown routing key `{$key}`.\nAllowed keys:\n{$list}"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\ValueOptions; | ||
|
|
||
| use Illuminate\Console\Scheduling\Event; | ||
| use Illuminate\Console\Scheduling\ManagesAttributes; | ||
| use Illuminate\Console\Scheduling\ManagesFrequencies; | ||
| use InvalidArgumentException; | ||
| use ReflectionClass; | ||
| use ReflectionMethod; | ||
|
|
||
| readonly class ScheduleOption | ||
| { | ||
| public function __construct( | ||
| public string $method, | ||
| public array $arguments = [], | ||
| ) { | ||
| $this->validateMethod($this->method); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The library is responsible for generating syntactically valid code, not for enforcing Laravel's runtime contracts. Ensuring correct arguments is the caller's responsibility, just as with any code generation tool. |
||
| } | ||
|
|
||
| private function validateMethod(string $method): void | ||
| { | ||
| $methods = array_merge( | ||
| $this->getMethods(ManagesAttributes::class), | ||
| $this->getMethods(ManagesFrequencies::class), | ||
| $this->getMethods(Event::class), | ||
| ); | ||
|
artengin marked this conversation as resolved.
|
||
|
|
||
| if (!in_array($method, $methods, true)) { | ||
| $methods = implode("\n", $methods); | ||
|
|
||
| throw new InvalidArgumentException("Unknown schedule method `{$method}`.\nAllowed methods:\n{$methods}"); | ||
| } | ||
| } | ||
|
|
||
| private function getMethods(string $class): array | ||
| { | ||
| $schedulePublicMethods = new ReflectionClass($class)->getMethods(ReflectionMethod::IS_PUBLIC); | ||
|
|
||
| return array_map(fn (ReflectionMethod $method) => $method->getName(), $schedulePublicMethods); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.