Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Change how classes are handle in handler methods
Browse files Browse the repository at this point in the history
  • Loading branch information
NastuzziSamy committed Mar 3, 2020
1 parent f4a5bac commit 97a4e76
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions src/Observers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
}

Expand All @@ -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<BaseObserver>
*/
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<BaseObserver>
*/
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;
}

Expand Down

0 comments on commit 97a4e76

Please sign in to comment.