Skip to content

Commit 44a618c

Browse files
committed
add Middleware Maker command
1 parent 3585a2f commit 44a618c

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

src/Maker/MakeMiddleware.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Generator;
17+
use Symfony\Bundle\MakerBundle\InputConfiguration;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Messenger\MessageBusInterface;
22+
23+
/**
24+
* @author Imad ZAIRIG <[email protected]>
25+
*
26+
* @internal
27+
*/
28+
final class MakeMiddleware extends AbstractMaker
29+
{
30+
public static function getCommandName(): string
31+
{
32+
return 'make:middleware';
33+
}
34+
35+
public function configureCommand(Command $command, InputConfiguration $inputConfig)
36+
{
37+
$command
38+
->setDescription('Creates a new messenger middleware')
39+
->addArgument('name', InputArgument::OPTIONAL, 'The name of the middleware class (e.g. <fg=yellow>CustomMiddleware</>)')
40+
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeMessage.txt'));
41+
}
42+
43+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
44+
{
45+
$middlewareClassNameDetails = $generator->createClassNameDetails(
46+
$input->getArgument('name'),
47+
'Middleware\\',
48+
'Middleware'
49+
);
50+
51+
$generator->generateClass(
52+
$middlewareClassNameDetails->getFullName(),
53+
'middleware/Middleware.tpl.php'
54+
);
55+
56+
$generator->writeChanges();
57+
58+
$this->writeSuccessMessage($io);
59+
60+
$io->text([
61+
'Next: Open your new middleware class and add the code you need.',
62+
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/messenger.html</>',
63+
]);
64+
}
65+
66+
public function configureDependencies(DependencyBuilder $dependencies)
67+
{
68+
$dependencies->addClassDependency(
69+
MessageBusInterface::class,
70+
'messenger'
71+
);
72+
}
73+
}

src/Resources/config/makers.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
<tag name="maker.command" />
5858
</service>
5959

60+
<service id="maker.maker.make_middleware" class="Symfony\Bundle\MakerBundle\Maker\MakeMiddleware">
61+
<tag name="maker.command" />
62+
</service>
63+
6064
<service id="maker.maker.make_registration_form" class="Symfony\Bundle\MakerBundle\Maker\MakeRegistrationForm">
6165
<argument type="service" id="maker.file_manager" />
6266
<argument type="service" id="maker.renderer.form_type_renderer" />

src/Resources/help/MakeMiddleware.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The <info>%command.name%</info> command generates a new Middleware class.
2+
3+
<info>php %command.full_name% CustomMiddleware</info>
4+
5+
If the argument is missing, the command will ask for the message class interactively.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $namespace; ?>;
4+
5+
use Symfony\Component\Messenger\Envelope;
6+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
7+
use Symfony\Component\Messenger\Middleware\StackInterface;
8+
9+
final class <?= $class_name; ?> implements MiddlewareInterface
10+
{
11+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
12+
{
13+
// ...
14+
return $stack->next()->handle($envelope, $stack);
15+
}
16+
}

0 commit comments

Comments
 (0)