1
0
Fork 0
pull/3544/head
Rob Bast 2014-12-12 17:12:32 +01:00 committed by Rob
parent da881c118b
commit 113606be7b
2 changed files with 45 additions and 0 deletions

View File

@ -155,6 +155,7 @@ class EventDispatcher
$return = 0; $return = 0;
foreach ($listeners as $callable) { foreach ($listeners as $callable) {
if (!is_string($callable) && is_callable($callable)) { if (!is_string($callable) && is_callable($callable)) {
$event = $this->checkListenerExpectedEvent($callable, $event);
$return = false === call_user_func($callable, $event) ? 1 : 0; $return = false === call_user_func($callable, $event) ? 1 : 0;
} elseif ($this->isPhpScript($callable)) { } elseif ($this->isPhpScript($callable)) {
$className = substr($callable, 0, strpos($callable, '::')); $className = substr($callable, 0, strpos($callable, '::'));
@ -200,9 +201,37 @@ class EventDispatcher
*/ */
protected function executeEventPhpScript($className, $methodName, Event $event) protected function executeEventPhpScript($className, $methodName, Event $event)
{ {
$event = $this->checkListenerExpectedEvent(array($className, $methodName), $event);
return $className::$methodName($event); return $className::$methodName($event);
} }
/**
* @param mixed $target
* @param Event $event
* @return Event|CommandEvent
*/
protected function checkListenerExpectedEvent($target, Event $event)
{
if (!$event instanceof Script\Event) {
return $event;
}
try {
$reflected = new \ReflectionParameter($target, 0);
} catch (\ReflectionException $e) {
return $event;
}
$expected = $reflected->getClass()->name;
if (!$event instanceof $expected && $expected === 'Composer\Script\CommandEvent') {
$event = new CommandEvent($event->getName(), $event->getComposer(), $event->getIO(), $event->isDevMode(), $event->getArguments());
}
return $event;
}
/** /**
* Add a listener for a particular event * Add a listener for a particular event
* *

View File

@ -17,6 +17,7 @@ use Composer\EventDispatcher\EventDispatcher;
use Composer\Installer\InstallerEvents; use Composer\Installer\InstallerEvents;
use Composer\TestCase; use Composer\TestCase;
use Composer\Script\ScriptEvents; use Composer\Script\ScriptEvents;
use Composer\Script\CommandEvent;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
class EventDispatcherTest extends TestCase class EventDispatcherTest extends TestCase
@ -38,6 +39,16 @@ class EventDispatcherTest extends TestCase
$dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false); $dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false);
} }
public function testDispatcherCanConvertScriptEventToCommandEventForListener()
{
$io = $this->getMock('Composer\IO\IOInterface');
$dispatcher = $this->getDispatcherStubForListenersTest(array(
"Composer\Test\EventDispatcher\EventDispatcherTest::convertEvent"
), $io);
$this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
}
/** /**
* @dataProvider getValidCommands * @dataProvider getValidCommands
* @param string $command * @param string $command
@ -205,6 +216,11 @@ class EventDispatcherTest extends TestCase
throw new \RuntimeException(); throw new \RuntimeException();
} }
public static function convertEvent(CommandEvent $event)
{
return false;
}
public static function someMethod() public static function someMethod()
{ {
return true; return true;