mirror of
https://github.com/andres-montanez/Magallanes
synced 2024-05-04 23:13:16 +02:00
Allowed registering custom commands
This commit is contained in:
parent
e531fa585a
commit
8708f8e386
56
src/Event/RegisterCommandsEvent.php
Normal file
56
src/Event/RegisterCommandsEvent.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of the Magallanes package.
|
||||
*
|
||||
* (c) Andrés Montañez <andres@andresmontanez.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mage\Event;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
class RegisterCommandsEvent extends Event
|
||||
{
|
||||
const EVENT_NAME = 'mage.event.register_commands';
|
||||
|
||||
/**
|
||||
* @var Command[]
|
||||
*/
|
||||
private $commands;
|
||||
|
||||
/**
|
||||
* @return Command[]
|
||||
*/
|
||||
public function getCommands()
|
||||
{
|
||||
return $this->commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $commands
|
||||
*
|
||||
* @return RegisterCommandsEvent
|
||||
*/
|
||||
public function setCommands(array $commands)
|
||||
{
|
||||
$this->commands = [];
|
||||
|
||||
foreach ($commands as $command) {
|
||||
$this->addCommand($command);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Command $command
|
||||
*/
|
||||
public function addCommand(Command $command)
|
||||
{
|
||||
$this->commands[] = $command;
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@
|
|||
namespace Mage;
|
||||
|
||||
use Mage\Command\AbstractCommand;
|
||||
use Mage\Event\RegisterCommandsEvent;
|
||||
use Mage\Runtime\Runtime;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@ -58,6 +59,13 @@ class MageApplication extends Application
|
|||
$this->runtime = $this->instantiateRuntime();
|
||||
$this->runtime->setEventDispatcher($dispatcher);
|
||||
$this->loadBuiltInCommands();
|
||||
|
||||
$event = new RegisterCommandsEvent();
|
||||
$dispatcher->dispatch(RegisterCommandsEvent::EVENT_NAME, $event);
|
||||
|
||||
foreach ($event->getCommands() as $command) {
|
||||
$this->add($command);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue