diff --git a/src/Observers/BaseHandler.php b/src/Observers/BaseHandler.php index 2e2aab6..00177bf 100644 --- a/src/Observers/BaseHandler.php +++ b/src/Observers/BaseHandler.php @@ -121,11 +121,23 @@ protected function push(BaseObserver $observer, array &$observers) * @param string $name * @param \Closure $callback * @param integer $priority + * @param string $class * @return self */ - public function create($data, string $name, \Closure $callback, int $priority=BaseObserver::MEDIUM_PRIORITY) + public function create($data, string $name, \Closure $callback, + int $priority=BaseObserver::MEDIUM_PRIORITY, string $class=null) { - $this->add($observer = new $this->observerClass($name, $callback, $priority, $data)); + if (!\is_null($class)) { + $subClass = $this->getObserverClass(); + + if (!($class instanceof $subClass)) { + throw new \LogicException("The class `$class` must an instance of the observer class `$subClass`"); + } + } else { + $class = $this->getObserverClass(); + } + + $this->add($observer = new $class($name, $callback, $priority, $data)); return $observer; } @@ -134,13 +146,16 @@ public function create($data, string $name, \Closure $callback, int $priority=Ba * Return if an observe exists with the given name. * * @param string $name + * @param string $class * @return boolean */ - public function has(string $name): bool + public function has(string $name, string $class=null): bool { - foreach ($this->observers as $key => $observer) { + foreach ($this->observers as $observer) { if ($observer->getName() === $name) { - return true; + if (\is_null($class) || ($observer instanceof $class)) { + return true; + } } } @@ -151,70 +166,73 @@ public function has(string $name): bool * Return the first observer with the given name. * * @param string $name + * @param string $class * @return BaseObserver */ - public function get(string $name) + public function get(string $name, string $class=null) { foreach ($this->observers as $observer) { if ($observer->getName() === $name) { - return $observer; + if (\is_null($class) || ($observer instanceof $class)) { + return $observer; + } } } throw new \Exception("The observer `$name` does not exist"); } - /** - * Return all observers for a specific class name. - * - * @param string $class - * @return array - */ - public function allFromClass(string $class): array - { - return \array_filter($this->observers, function ($observer) use ($class) { - return $observer instanceof $class; - }); - } - /** * Return the number of the handled observers. * + * @param string $class * @return integer */ - public function count(): int + public function count(string $class=null): int { - return \count($this->observers); + return \count($this->all($class)); } /** * Return the list of the handled observers. * + * @param string $class * @return array */ - public function all(): array + public function all(string $class=null): array { - return $this->observers; + if (\is_null($class)) { + return $this->observers; + } + + return \array_filter($this->observers, function ($observer) use ($class) { + return $observer instanceof $class; + }); } /** * Remove an observer before it is locked. * * @param string $name + * @param string $class * @return self */ - public function remove(string $name) + public function remove(string $name, string $class=null) { $this->needsToBeUnlocked(); - foreach ($this->observers as $observer) { + foreach ($this->observers as $key => $observer) { if ($observer->getName() === $name) { - unset($this->observers); + if (\is_null($class) || ($observer instanceof $class)) { + unset($this->observers[$key]); + + $this->observers = \array_values($this->all()); + + break; + } } } - $this->observers = array_values($this->observers); - return $this; }