Skip to content

Register event listeners via config #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ final class Application
*/
private $bgColour = 'black';

/**
* @var string
*/
private $frameworkConfigLocation = __DIR__ . '/../app/config.php';

/**
* It should be instantiated with the title of
* the workshop and the path to the DI configuration file.
Expand Down Expand Up @@ -156,24 +161,7 @@ public function setBgColour($colour)
*/
public function run()
{
$containerBuilder = new ContainerBuilder;
$containerBuilder->addDefinitions(__DIR__ . '/../app/config.php');
$containerBuilder->addDefinitions($this->diConfigFile);

$containerBuilder->addDefinitions(array_merge(
[
'workshopTitle' => $this->workshopTitle,
'exercises' => $this->exercises,
'workshopLogo' => $this->logo,
'bgColour' => $this->bgColour,
'fgColour' => $this->fgColour,
]
));

$containerBuilder->useAutowiring(false);
$containerBuilder->useAnnotations(false);

$container = $containerBuilder->build();
$container = $this->getContainer();

foreach ($this->exercises as $exercise) {
if (false === $container->has($exercise)) {
Expand Down Expand Up @@ -229,4 +217,33 @@ public function run()
}
return $exitCode;
}

/**
* @return \DI\Container
*/
private function getContainer()
{
$containerBuilder = new ContainerBuilder;
$containerBuilder->addDefinitions(
array_merge_recursive(
require $this->frameworkConfigLocation,
require $this->diConfigFile
)
);

$containerBuilder->addDefinitions(
[
'workshopTitle' => $this->workshopTitle,
'exercises' => $this->exercises,
'workshopLogo' => $this->logo,
'bgColour' => $this->bgColour,
'fgColour' => $this->fgColour,
]
);

$containerBuilder->useAutowiring(false);
$containerBuilder->useAnnotations(false);

return $containerBuilder->build();
}
}
2 changes: 1 addition & 1 deletion src/CommandRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function addCommand(CommandDefinition $c)
*
* @param array $args
* @return int
* @throws CliRouteNotExists
* @throws CliRouteNotExistsException
*/
public function route(array $args = null)
{
Expand Down
55 changes: 55 additions & 0 deletions src/Factory/EventDispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Interop\Container\ContainerInterface;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
Expand All @@ -20,6 +21,7 @@ class EventDispatcherFactory
/**
* @param ContainerInterface $container
* @return EventDispatcher
* @throws InvalidArgumentException
*/
public function __invoke(ContainerInterface $container)
{
Expand All @@ -37,6 +39,59 @@ public function __invoke(ContainerInterface $container)

$dispatcher->listen('verify.post.check', $container->get(SelfCheckListener::class));

//add listeners from config
$eventListeners = $container->get('eventListeners') ?: [];

if (!is_array($eventListeners)) {
throw InvalidArgumentException::typeMisMatch('array', $eventListeners);
}

array_walk($eventListeners, function ($listeners, $eventName) use ($dispatcher, $container) {
if (!is_array($listeners)) {
throw InvalidArgumentException::typeMisMatch('array', $listeners);
}

$this->attachListeners($eventName, $listeners, $container, $dispatcher);
});

return $dispatcher;
}

/**
* @param string $eventName
* @param array $listeners
* @param ContainerInterface $container
* @param EventDispatcher $dispatcher
* @throws \PhpSchool\PhpWorkshop\Exception\InvalidArgumentException
*/
private function attachListeners(
$eventName,
array $listeners,
ContainerInterface $container,
EventDispatcher $dispatcher
) {
array_walk($listeners, function ($listener) use ($eventName, $dispatcher, $container) {
if (is_callable($listener)) {
return $dispatcher->listen($eventName, $listener);
}

if (!is_string($listener)) {
throw new InvalidArgumentException(
sprintf('Listener must be a callable or a container entry for a callable service.')
);
}

if (!$container->has($listener)) {
throw new InvalidArgumentException(sprintf('Container has no entry named: "%s"', $listener));
}

$listener = $container->get($listener);

if (!is_callable($listener)) {
throw InvalidArgumentException::typeMisMatch('callable', $listener);
}

return $dispatcher->listen($eventName, $listener);
});
}
}
62 changes: 62 additions & 0 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace PhpSchool\PhpWorkshopTest;

use PhpSchool\PhpWorkshop\Application;
use PHPUnit_Framework_TestCase;

/**
* @author Aydin Hassan <[email protected]>
*/
class ApplicationTest extends PHPUnit_Framework_TestCase
{
public function testEventListenersFromLocalAndWorkshopConfigAreMerged()
{

$frameworkFileContent = '<?php return [';
$frameworkFileContent .= " 'eventListeners' => [";
$frameworkFileContent .= " 'event1' => [";
$frameworkFileContent .= " 'entry1',";
$frameworkFileContent .= " 'entry2',";
$frameworkFileContent .= ' ]';
$frameworkFileContent .= ' ]';
$frameworkFileContent .= '];';

$localFileContent = '<?php return [';
$localFileContent .= " 'eventListeners' => [";
$localFileContent .= " 'event1' => [";
$localFileContent .= " 'entry3',";
$localFileContent .= ' ]';
$localFileContent .= ' ]';
$localFileContent .= '];';

$localFile = sprintf('%s/%s', sys_get_temp_dir(), uniqid($this->getName(), true));
$frameworkFile = sprintf('%s/%s', sys_get_temp_dir(), uniqid($this->getName(), true));
file_put_contents($frameworkFile, $frameworkFileContent);
file_put_contents($localFile, $localFileContent);

$app = new Application('Test App', $localFile);

$rm = new \ReflectionMethod($app, 'getContainer');
$rm->setAccessible(true);

$rp = new \ReflectionProperty(Application::class, 'frameworkConfigLocation');
$rp->setAccessible(true);
$rp->setValue($app, $frameworkFile);

$container = $rm->invoke($app);

$eventListeners = $container->get('eventListeners');

$this->assertEquals(
[
'event1' => [
'entry1',
'entry2',
'entry3',
]
],
$eventListeners
);
}
}
Loading