-
Notifications
You must be signed in to change notification settings - Fork 0
feat: ability generate withSchedule for boostrap/app.php file #43
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
0de859c
17ad6a7
48fbb27
4c3c2fe
7647f88
90dfe63
96d468a
821815b
e5d10db
9ada00c
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 |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |
|
|
||
| namespace RonasIT\Larabuilder\Builders; | ||
|
|
||
| use RonasIT\Larabuilder\ValueOptions\ScheduleOption; | ||
| use RonasIT\Larabuilder\Visitors\AppBootstrapVisitors\AddExceptionsRender; | ||
| use RonasIT\Larabuilder\Visitors\AppBootstrapVisitors\AddScheduleCommand; | ||
|
|
||
| class AppBootstrapBuilder extends PHPFileBuilder | ||
| { | ||
|
|
@@ -15,7 +17,7 @@ public function addExceptionsRender(string $exceptionClass, string $renderBody, | |
| { | ||
| $this->traverser->addVisitor(new AddExceptionsRender($exceptionClass, $renderBody, $includeRequestArg)); | ||
|
|
||
| $imports = [$exceptionClass]; | ||
| $imports = ['Illuminate\Foundation\Configuration\Exceptions', $exceptionClass]; | ||
|
|
||
| if ($includeRequestArg) { | ||
| $imports[] = 'Illuminate\Http\Request'; | ||
|
|
@@ -25,4 +27,15 @@ public function addExceptionsRender(string $exceptionClass, string $renderBody, | |
|
|
||
| return $this; | ||
| } | ||
|
|
||
|
AZabolotnikov marked this conversation as resolved.
|
||
| public function addScheduleCommand(string $command, ScheduleOption ...$options): self | ||
| { | ||
| $this->traverser->addVisitor(new AddScheduleCommand($command, ...$options)); | ||
|
|
||
| $this->addImports([ | ||
| 'Illuminate\Support\Facades\Schedule', | ||
| ]); | ||
|
Comment on lines
+35
to
+37
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
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 provides
Comment on lines
+35
to
+37
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.
Adding Useful? React with 👍 / 👎. |
||
|
|
||
| return $this; | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
|
|
||
| private function validateMethod(string $method): void | ||
| { | ||
| $methods = array_merge( | ||
| $this->getMethods(ManagesAttributes::class), | ||
| $this->getMethods(ManagesFrequencies::class), | ||
| $this->getMethods(Event::class), | ||
| ); | ||
|
Comment on lines
+23
to
+27
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 👍 / 👎. |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Visitors\AppBootstrapVisitors; | ||
|
|
||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use RonasIT\Larabuilder\Support\NodeValueFactory; | ||
| use RonasIT\Larabuilder\ValueOptions\ScheduleOption; | ||
|
|
||
|
AZabolotnikov marked this conversation as resolved.
|
||
| class AddScheduleCommand extends AbstractAppBootstrapVisitor | ||
| { | ||
| protected Expression $scheduleStatement; | ||
| protected array $options = []; | ||
|
|
||
| public function __construct( | ||
| protected string $command, | ||
| ScheduleOption ...$options, | ||
| ) { | ||
| $this->options = $options; | ||
|
|
||
| $this->scheduleStatement = $this->buildScheduleCall(); | ||
|
|
||
| parent::__construct( | ||
| parentMethod: 'withSchedule', | ||
| targetMethod: 'command', | ||
| ); | ||
| } | ||
|
|
||
| protected function buildScheduleCall(): Expression | ||
| { | ||
| $call = new StaticCall( | ||
| class: new Name('Schedule'), | ||
| name: new Identifier('command'), | ||
| args: [ | ||
| new Arg(NodeValueFactory::make($this->command)->node), | ||
| ], | ||
| ); | ||
|
|
||
| foreach ($this->options as $option) { | ||
| $arguments = array_map(fn ($argument) => new Arg(NodeValueFactory::make($argument)->node), $option->arguments); | ||
|
|
||
| $call = new MethodCall( | ||
| var: $call, | ||
| name: new Identifier($option->method), | ||
| args: $arguments, | ||
| ); | ||
| } | ||
|
|
||
| return new Expression($call); | ||
| } | ||
|
|
||
| protected function getInsertableNode(): Expression | ||
| { | ||
| return $this->scheduleStatement; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.