|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Maker; |
| 13 | + |
| 14 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 15 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 16 | +use Symfony\Bundle\MakerBundle\FileManager; |
| 17 | +use Symfony\Bundle\MakerBundle\Generator; |
| 18 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 19 | +use Symfony\Bundle\MakerBundle\Str; |
| 20 | +use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Finder\Finder; |
| 24 | +use Symfony\Component\Process\Process; |
| 25 | +use Symfony\Component\Scheduler\Attribute\AsSchedule; |
| 26 | +use Symfony\Component\Scheduler\RecurringMessage; |
| 27 | +use Symfony\Component\Scheduler\Schedule; |
| 28 | +use Symfony\Component\Scheduler\ScheduleProviderInterface; |
| 29 | +use Symfony\Contracts\Cache\CacheInterface; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Jesse Rushlow <jr@rushlow.dev> |
| 33 | + * |
| 34 | + * @internal |
| 35 | + */ |
| 36 | +final class MakeSchedule extends AbstractMaker |
| 37 | +{ |
| 38 | + private string $scheduleName; |
| 39 | + private ?string $message = null; |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + private FileManager $fileManager, |
| 43 | + private Finder $finder = new Finder(), |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + public static function getCommandName(): string |
| 48 | + { |
| 49 | + return 'make:schedule'; |
| 50 | + } |
| 51 | + |
| 52 | + public static function getCommandDescription(): string |
| 53 | + { |
| 54 | + return 'Create a scheduler component'; |
| 55 | + } |
| 56 | + |
| 57 | + public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
| 58 | + { |
| 59 | + $command |
| 60 | + ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeScheduler.txt')) |
| 61 | + ; |
| 62 | + } |
| 63 | + |
| 64 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 65 | + { |
| 66 | + if (!class_exists(AsSchedule::class)) { |
| 67 | + $io->writeln('Running composer require symfony/scheduler'); |
| 68 | + $process = Process::fromShellCommandline('composer require symfony/scheduler'); |
| 69 | + $process->run(); |
| 70 | + $io->writeln('Scheduler successfully installed!'); |
| 71 | + } |
| 72 | + |
| 73 | + // Loop over existing src/Message/* and ask which message the user would like to schedule |
| 74 | + $availableMessages = ['Empty Schedule']; |
| 75 | + $messageDir = $this->fileManager->getRootDirectory().'/src/Message'; |
| 76 | + |
| 77 | + if ($this->fileManager->fileExists($messageDir)) { |
| 78 | + $finder = $this->finder->in($this->fileManager->getRootDirectory().'/src/Message'); |
| 79 | + |
| 80 | + foreach ($finder->files() as $file) { |
| 81 | + $availableMessages[] = $file->getFilenameWithoutExtension(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + $scheduleNameHint = 'MainSchedule'; |
| 86 | + |
| 87 | + // If the count is 1, no other messages were found - don't ask to create a message |
| 88 | + if (1 !== \count($availableMessages)) { |
| 89 | + $selectedMessage = $io->choice('Select which message', $availableMessages); |
| 90 | + |
| 91 | + if ('Empty Schedule' !== $selectedMessage) { |
| 92 | + $this->message = $selectedMessage; |
| 93 | + |
| 94 | + // We don't want SomeMessageSchedule, so remove the "Message" suffix to give us SomeSchedule |
| 95 | + $scheduleNameHint = sprintf('%sSchedule', Str::removeSuffix($selectedMessage, 'Message')); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Ask the name of the new schedule |
| 100 | + $this->scheduleName = $io->ask(question: 'What should we call the new schedule?', default: $scheduleNameHint); |
| 101 | + } |
| 102 | + |
| 103 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
| 104 | + { |
| 105 | + $scheduleClassDetails = $generator->createClassNameDetails( |
| 106 | + $this->scheduleName, |
| 107 | + 'Scheduler\\', |
| 108 | + ); |
| 109 | + |
| 110 | + $useStatements = new UseStatementGenerator([ |
| 111 | + AsSchedule::class, |
| 112 | + RecurringMessage::class, |
| 113 | + Schedule::class, |
| 114 | + ScheduleProviderInterface::class, |
| 115 | + CacheInterface::class, |
| 116 | + ]); |
| 117 | + |
| 118 | + if (null !== $this->message) { |
| 119 | + $useStatements->addUseStatement('App\\Message\\'.$this->message); |
| 120 | + } |
| 121 | + |
| 122 | + $generator->generateClass( |
| 123 | + $scheduleClassDetails->getFullName(), |
| 124 | + 'scheduler/Schedule.tpl.php', |
| 125 | + [ |
| 126 | + 'use_statements' => $useStatements, |
| 127 | + 'has_custom_message' => null !== $this->message, |
| 128 | + 'message_class_name' => $this->message, |
| 129 | + ], |
| 130 | + ); |
| 131 | + |
| 132 | + $generator->writeChanges(); |
| 133 | + |
| 134 | + $this->writeSuccessMessage($io); |
| 135 | + } |
| 136 | + |
| 137 | + public function configureDependencies(DependencyBuilder $dependencies): void |
| 138 | + { |
| 139 | + } |
| 140 | +} |
0 commit comments