Skip to content
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

[make:schedule] Add transport name for attribute #1568

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Maker/MakeSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class MakeSchedule extends AbstractMaker
{
private string $scheduleName;
private ?string $message = null;
private ?string $transportName = null;

public function __construct(
private FileManager $fileManager,
Expand Down Expand Up @@ -82,6 +83,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
}
}

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

$scheduleNameHint = 'MainSchedule';

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

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/skeleton/scheduler/Schedule.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<?= $use_statements; ?>

#[AsSchedule]
#[AsSchedule<?php if ($has_transport_name): ?>('<?= $transport_name; ?>')<?php endif ?>]
final class <?= $class_name; ?> implements ScheduleProviderInterface
{
public function __construct(
Expand Down
19 changes: 19 additions & 0 deletions tests/Maker/MakeScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,26 @@ protected function getMakerClass(): string

public function getTestDetails(): \Generator
{
yield 'it_generates_a_schedule_with_transport_name' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'dummy', // use transport name "dummy"
'', // use default schedule name "MainSchedule"
]);

$this->assertStringContainsString('Success', $output);

self::assertFileEquals(
\dirname(__DIR__).'/fixtures/make-schedule/expected/DefaultScheduleWithTransportName.php',
$runner->getPath('src/Scheduler/MainSchedule.php')
);
}),
];

yield 'it_generates_a_schedule' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'', // use default transport name
'', // use default schedule name "MainSchedule"
]);

Expand All @@ -48,6 +65,7 @@ public function getTestDetails(): \Generator
})
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'', // Use the default transport name
0, // Select "Empty Schedule"
'MySchedule', // Go with the default name "MainSchedule"
]);
Expand All @@ -70,6 +88,7 @@ public function getTestDetails(): \Generator
})
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'', // Use the default transport name
1, // Select "MyMessage" from choice
'', // Go with the default name "MessageFixtureSchedule"
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Scheduler;

use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;

#[AsSchedule('dummy')]
final class MainSchedule implements ScheduleProviderInterface
{
public function __construct(
private CacheInterface $cache,
) {
}

public function getSchedule(): Schedule
{
return (new Schedule())
->add(
// @TODO - Create a Message to schedule
// RecurringMessage::every('1 hour', new App\Message\Message()),
)
->stateful($this->cache)
;
}
}
Loading