Skip to content

Commit 3197586

Browse files
Added transport name for attribute
1 parent 9c85a83 commit 3197586

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/Maker/MakeSchedule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class MakeSchedule extends AbstractMaker
3737
{
3838
private string $scheduleName;
3939
private ?string $message = null;
40+
private ?string $transportName = null;
4041

4142
public function __construct(
4243
private FileManager $fileManager,
@@ -82,6 +83,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
8283
}
8384
}
8485

86+
$this->transportName = $io->ask('What should we call the new transport? (To be used for the attribute #[AsSchedule(name)])');
87+
8588
$scheduleNameHint = 'MainSchedule';
8689

8790
// If the count is 1, no other messages were found - don't ask to create a message
@@ -126,6 +129,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
126129
'use_statements' => $useStatements,
127130
'has_custom_message' => null !== $this->message,
128131
'message_class_name' => $this->message,
132+
'has_transport_name' => null !== $this->transportName,
133+
'transport_name' => $this->transportName,
129134
],
130135
);
131136

src/Resources/skeleton/scheduler/Schedule.tpl.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
<?= $use_statements; ?>
66

7+
<?php if ($has_transport_name): ?>
8+
#[AsSchedule('<?= $transport_name; ?>')]
9+
<?php else: ?>
710
#[AsSchedule]
11+
<?php endif ?>
812
final class <?= $class_name; ?> implements ScheduleProviderInterface
913
{
1014
public function __construct(

tests/Maker/MakeScheduleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ protected function getMakerClass(): string
2424

2525
public function getTestDetails(): \Generator
2626
{
27+
yield 'it_generates_a_schedule_with_transport_name' => [$this->createMakerTest()
28+
->run(function (MakerTestRunner $runner) {
29+
$output = $runner->runMaker([
30+
'dummy', // use transport name "dummy"
31+
'', // use default schedule name "MainSchedule"
32+
]);
33+
34+
$this->assertStringContainsString('Success', $output);
35+
36+
self::assertFileEquals(
37+
\dirname(__DIR__).'/fixtures/make-schedule/expected/DefaultScheduleWithTransportName.php',
38+
$runner->getPath('src/Scheduler/MainSchedule.php')
39+
);
40+
}),
41+
];
42+
2743
yield 'it_generates_a_schedule' => [$this->createMakerTest()
2844
->run(function (MakerTestRunner $runner) {
2945
$output = $runner->runMaker([
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Scheduler;
4+
5+
use Symfony\Component\Scheduler\Attribute\AsSchedule;
6+
use Symfony\Component\Scheduler\RecurringMessage;
7+
use Symfony\Component\Scheduler\Schedule;
8+
use Symfony\Component\Scheduler\ScheduleProviderInterface;
9+
use Symfony\Contracts\Cache\CacheInterface;
10+
11+
#[AsSchedule('dummy')]
12+
final class MainSchedule implements ScheduleProviderInterface
13+
{
14+
public function __construct(
15+
private CacheInterface $cache,
16+
) {
17+
}
18+
19+
public function getSchedule(): Schedule
20+
{
21+
return (new Schedule())
22+
->add(
23+
// @TODO - Create a Message to schedule
24+
// RecurringMessage::every('1 hour', new App\Message\Message()),
25+
)
26+
->stateful($this->cache)
27+
;
28+
}
29+
}

0 commit comments

Comments
 (0)