This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Dhii/task/implement-psr-14
Implementations for PSR-14
- Loading branch information
Showing
2 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
namespace Dhii\WpEvents; | ||
|
||
use \Psr\EventManager\EventInterface; | ||
|
||
/** | ||
* Event. | ||
* | ||
* @author Miguel Muscat <[email protected]> | ||
*/ | ||
class Event implements EventInterface | ||
{ | ||
|
||
/** | ||
* The event name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* The parameters. | ||
* | ||
* @var array | ||
*/ | ||
protected $params; | ||
|
||
/** | ||
* The target context object. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $target; | ||
|
||
/** | ||
* The propagation flag. | ||
* | ||
* @var booleans | ||
*/ | ||
protected $propagation; | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param string $name The event name. | ||
* @param array $params The event parameters. | ||
* @param mixed $target The target object. Used for context. | ||
* @param boolean $propagation True to propagate the event, false to not. | ||
*/ | ||
public function __construct($name, array $params = array(), $target = null, $propagation = true) | ||
{ | ||
$this->setName($name) | ||
->setParams($params) | ||
->setTarget($target) | ||
->setPropagation($propagation); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParam($name) | ||
{ | ||
return isset($this->params[$name]) | ||
? $this->params[$name] | ||
: null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParams() | ||
{ | ||
return $this->params; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTarget() | ||
{ | ||
return $this->target; | ||
} | ||
|
||
/** | ||
* Gets whether or not the event propagates. | ||
* | ||
* @return boolean True if the event propagate, false if not. | ||
*/ | ||
public function getPropagation() | ||
{ | ||
return $this->propagation; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isPropagationStopped() | ||
{ | ||
return !$this->getPropagation(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setParams(array $params) | ||
{ | ||
$this->params = $params; | ||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setTarget($target) | ||
{ | ||
$this->target = $target; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the event propagation. | ||
* | ||
* @param boolean $propagation True to propagate, false to not. | ||
* @return Event This instance. | ||
*/ | ||
public function setPropagation($propagation) | ||
{ | ||
$this->propagation = $propagation; | ||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function stopPropagation($flag) | ||
{ | ||
$this->setPropagation(!$flag); | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Dhii\WpEvents; | ||
|
||
use \Psr\EventManager\EventInterface; | ||
use \Psr\EventManager\EventManagerInterface; | ||
|
||
/** | ||
* Event Manager. | ||
* | ||
* @author Miguel Muscat <[email protected]> | ||
*/ | ||
class EventManager implements EventManagerInterface | ||
{ | ||
|
||
/** | ||
* Constructs a new instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function attach($event, $callback, $priority = 10) | ||
{ | ||
$eventObject = $this->normalizeEvent($event); | ||
$numArgsToPass = $this->getCallableNumParams($callback); | ||
\add_filter($eventObject->getName(), $callback, $priority, $numArgsToPass + 1); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function detach($event, $callback) | ||
{ | ||
$eventObject = $this->normalizeEvent($event); | ||
\remove_filter($eventObject->getName(), $callback); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clearListeners($event) | ||
{ | ||
$eventObject = $this->normalizeEvent($event); | ||
\remove_all_filters($eventObject->getName()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function trigger($event, $target = null, $argv = array()) | ||
{ | ||
$eventObject = $this->normalizeEvent($event); | ||
$args = empty($argv) | ||
? array(null) | ||
: $argv; | ||
array_push($args, $target); | ||
\apply_filters_ref_array($eventObject->getName(), $args); | ||
} | ||
|
||
/** | ||
* Normalizes the given event into an Event instance. | ||
* | ||
* @param EventInterfaceevent string or Event instance. | ||
* @return EventInterface The event instance. | ||
*/ | ||
protected function normalizeEvent($event) | ||
{ | ||
return ($event instanceof EventInterfacevent) | ||
? $event | ||
: new Event($event); | ||
} | ||
|
||
/** | ||
* Gets the number of parameters for a callable. | ||
* | ||
* @param callable $callable The callable. | ||
* @return integer The number of parameters. | ||
*/ | ||
protected function getCallableNumParams($callable) | ||
{ | ||
return $this->getCallableReflection($callable)->getNumberOfParameter(); | ||
} | ||
|
||
/** | ||
* Gets the reflection instance for a callable. | ||
* | ||
* @param callable $callable The callable. | ||
* @return ReflectionFunction|ReflectionMethod The reflection instance. | ||
*/ | ||
protected function getCallableReflection($callable) | ||
{ | ||
return is_array($callable) ? | ||
new ReflectionMethod($callable[0], $callable[1]) : | ||
new ReflectionFunction($callable); | ||
} | ||
|
||
} |