From 040bbaca51b2468224c37f4626161c7cedc57793 Mon Sep 17 00:00:00 2001 From: Max Gfeller Date: Thu, 19 Dec 2013 07:54:16 +0100 Subject: [PATCH 1/5] Don't throw an exception if the called script is not one of the event-scripts. This makes it possible for one to define their own scripts like "make-release" etc. --- src/Composer/Command/RunScriptCommand.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Composer/Command/RunScriptCommand.php b/src/Composer/Command/RunScriptCommand.php index c4a3a3563..4317ba08c 100644 --- a/src/Composer/Command/RunScriptCommand.php +++ b/src/Composer/Command/RunScriptCommand.php @@ -56,8 +56,6 @@ EOT if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) { throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script)); } - - throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $script)); } $this->getComposer()->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev')); From f1c1ba27b4456adbf9c3ab38be9c7c5f5e4f1282 Mon Sep 17 00:00:00 2001 From: Max Gfeller Date: Thu, 19 Dec 2013 07:58:58 +0100 Subject: [PATCH 2/5] Throw an exception if no listeners have been found for given event. --- src/Composer/EventDispatcher/EventDispatcher.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Composer/EventDispatcher/EventDispatcher.php b/src/Composer/EventDispatcher/EventDispatcher.php index 9c2aee91f..2f19673c4 100644 --- a/src/Composer/EventDispatcher/EventDispatcher.php +++ b/src/Composer/EventDispatcher/EventDispatcher.php @@ -118,6 +118,10 @@ class EventDispatcher { $listeners = $this->getListeners($event); + if(sizeof($listeners) === 0) { + throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $event->getName())); + } + foreach ($listeners as $callable) { if (!is_string($callable) && is_callable($callable)) { call_user_func($callable, $event); From 421b09dc447036e4f2ef03dc07734d6a20170944 Mon Sep 17 00:00:00 2001 From: Max Gfeller Date: Thu, 19 Dec 2013 08:46:36 +0100 Subject: [PATCH 3/5] Check if a given event has registered any listeners. If not the script is not defined in the composer.json file --- src/Composer/Command/RunScriptCommand.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Composer/Command/RunScriptCommand.php b/src/Composer/Command/RunScriptCommand.php index 4317ba08c..ceb3204f3 100644 --- a/src/Composer/Command/RunScriptCommand.php +++ b/src/Composer/Command/RunScriptCommand.php @@ -58,6 +58,12 @@ EOT } } + $hasListeners = $this->getComposer()->getEventDispatcher()->hasEventListeners(new \Composer\Script\CommandEvent($script, $this->getComposer(), $this->getIO())); + + if(!$hasListeners) { + throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $script)); + } + $this->getComposer()->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev')); } } From a12ed492ef52c72575e8c3c1a89d6837fa8885f7 Mon Sep 17 00:00:00 2001 From: Max Gfeller Date: Thu, 19 Dec 2013 08:47:55 +0100 Subject: [PATCH 4/5] Don't throw exception in the doDispatch method. --- src/Composer/EventDispatcher/EventDispatcher.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Composer/EventDispatcher/EventDispatcher.php b/src/Composer/EventDispatcher/EventDispatcher.php index 2f19673c4..9c2aee91f 100644 --- a/src/Composer/EventDispatcher/EventDispatcher.php +++ b/src/Composer/EventDispatcher/EventDispatcher.php @@ -118,10 +118,6 @@ class EventDispatcher { $listeners = $this->getListeners($event); - if(sizeof($listeners) === 0) { - throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $event->getName())); - } - foreach ($listeners as $callable) { if (!is_string($callable) && is_callable($callable)) { call_user_func($callable, $event); From 067a8e764fdb281bd49cf20a03480c22725a716e Mon Sep 17 00:00:00 2001 From: Max Gfeller Date: Thu, 19 Dec 2013 08:48:41 +0100 Subject: [PATCH 5/5] Added a new method to check if an event has any listeners registered. --- src/Composer/EventDispatcher/EventDispatcher.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Composer/EventDispatcher/EventDispatcher.php b/src/Composer/EventDispatcher/EventDispatcher.php index 9c2aee91f..6abb9162a 100644 --- a/src/Composer/EventDispatcher/EventDispatcher.php +++ b/src/Composer/EventDispatcher/EventDispatcher.php @@ -220,6 +220,19 @@ class EventDispatcher return call_user_func_array('array_merge', $listeners[$event->getName()]); } + /** + * Checks if an event has listeners registered + * + * @param Event $event + * @return boolean + */ + public function hasEventListeners(Event $event) + { + $listeners = $this->getListeners($event); + + return (sizeof($listeners) > 0); + } + /** * Finds all listeners defined as scripts in the package *