Skip to content

Commit

Permalink
Merge 3ac452f into e30de6b
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Apr 6, 2018
2 parents e30de6b + 3ac452f commit 45a568a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Event/RegisterCommandsEvent.php
@@ -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;
}
}
19 changes: 19 additions & 0 deletions src/MageApplication.php
Expand Up @@ -11,7 +11,9 @@
namespace Mage;

use Mage\Command\AbstractCommand;
use Mage\Event\RegisterCommandsEvent;
use Mage\Runtime\Runtime;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Monolog\Logger;
Expand Down Expand Up @@ -55,7 +57,15 @@ public function __construct($file)
});

$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);
}
}

/**
Expand Down Expand Up @@ -89,6 +99,15 @@ public function configure()
throw new RuntimeException(sprintf('The configured log_dir "%s" does not exists or is not a directory.', $config['magephp']['log_dir']));
}

// Subscribers
if (array_key_exists('subscribers', $config['magephp'])) {
foreach ((array) $config['magephp']['subscribers'] as $subscriber) {
if ($subscriber instanceof EventSubscriberInterface) {
$this->runtime->getEventDispatcher()->addSubscriber($subscriber);
}
}
}

$this->runtime->setConfiguration($config['magephp']);
$this->runtime->setLogger($logger);
return;
Expand Down
21 changes: 21 additions & 0 deletions src/Runtime/Runtime.php
Expand Up @@ -15,6 +15,7 @@
use Mage\Deploy\Strategy\StrategyInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Process\Process;
use Mage\Runtime\Exception\RuntimeException;

Expand Down Expand Up @@ -46,6 +47,11 @@ class Runtime
*/
protected $stage;

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* @var string|null The host being deployed to
*/
Expand Down Expand Up @@ -316,6 +322,21 @@ public function getStage()
return $this->stage;
}

/**
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}

/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* Retrieve the defined Tasks for the current Environment and Stage
*
Expand Down

0 comments on commit 45a568a

Please sign in to comment.