Skip to content

Commit

Permalink
Generate Subscriber #1203
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 24, 2023
1 parent 17c2d39 commit 1fbbfdc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/registry/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
'field' => \Windwalker\Core\Generator\SubCommand\FieldSubCommand::class,
'policy' => \Windwalker\Core\Generator\SubCommand\PolicySubCommand::class,
'middleware' => \Windwalker\Core\Generator\SubCommand\MiddlewareSubCommand::class,
'subscriber' => \Windwalker\Core\Generator\SubCommand\SubscriberSubCommand::class,
'command' => \Windwalker\Core\Generator\SubCommand\CommandSubCommand::class,
];
67 changes: 67 additions & 0 deletions src/Core/Generator/SubCommand/SubscriberSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Windwalker\Core\Generator\SubCommand;

use Symfony\Component\Console\Command\Command;
use Windwalker\Console\CommandWrapper;
use Windwalker\Console\IOInterface;

/**
* The GenEnumSubCommand class.
*/
#[CommandWrapper(
description: 'Generate Windwalker event subscriber.'
)]
class SubscriberSubCommand extends AbstractGeneratorSubCommand
{
protected string $defaultNamespace = 'Subscriber';

protected string $defaultDir = 'Subscriber';

protected bool $requireDest = false;

/**
* configure
*
* @param Command $command
*
* @return void
*/
public function configure(Command $command): void
{
parent::configure($command);
}

/**
* Executes the current command.
*
* @param IOInterface $io
*
* @return int Return 0 is success, 1-255 is failure.
*/
public function execute(IOInterface $io): int
{
[, $name] = $this->getNameParts($io);
$force = $io->getOption('force');

if (!$name) {
$io->errorStyle()->error('No subscriber name');

return 255;
}

$this->codeGenerator->from($this->getViewPath('subscriber/*.tpl'))
->replaceTo(
$this->getDestPath($io),
[
'name' => $name,
'ns' => $this->getNamespace($io),
],
$force
);

return 0;
}
}
18 changes: 18 additions & 0 deletions views/code/subscriber/{% pascal($name) %}Subscriber.php.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% $phpOpen %}

declare(strict_types=1);

namespace {% $ns %};

use Windwalker\Event\Attributes\EventSubscriber;
use Windwalker\Event\Attributes\ListenTo;

#[EventSubscriber]
class {% pascal($name) %}Subscriber
{
#[ListenTo(FooEvent::class)]
public function foo($event): void
{
//
}
}

0 comments on commit 1fbbfdc

Please sign in to comment.