@@ -48,7 +48,7 @@ |
||
| 48 | 48 | /** |
| 49 | 49 | * Shared implementation of hook reading logic. |
| 50 | 50 | * |
| 51 | - * @param object $target Object to read annotations from. |
|
| 51 | + * @param Annotatable $target Object to read annotations from. |
|
| 52 | 52 | * @param \Closure $condition Whether the annotation should be registered. |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
@@ -11,66 +11,66 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class HooksReader { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Construct a new HooksReader instance. |
|
| 16 | - */ |
|
| 17 | - private function __construct() { |
|
| 18 | - $this->reader = new AnnotationReader(); |
|
| 19 | - AnnotationRegistry::registerFile( |
|
| 20 | - __DIR__ . '/Annotations.php' |
|
| 21 | - ); |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * Construct a new HooksReader instance. |
|
| 16 | + */ |
|
| 17 | + private function __construct() { |
|
| 18 | + $this->reader = new AnnotationReader(); |
|
| 19 | + AnnotationRegistry::registerFile( |
|
| 20 | + __DIR__ . '/Annotations.php' |
|
| 21 | + ); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the Annotation reader. |
|
| 26 | - * |
|
| 27 | - * @return AnnotationReader |
|
| 28 | - */ |
|
| 29 | - private function reader() { |
|
| 30 | - return $this->reader; |
|
| 31 | - } |
|
| 24 | + /** |
|
| 25 | + * Get the Annotation reader. |
|
| 26 | + * |
|
| 27 | + * @return AnnotationReader |
|
| 28 | + */ |
|
| 29 | + private function reader() { |
|
| 30 | + return $this->reader; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Get the shared reader instance. |
|
| 35 | - * |
|
| 36 | - * @return HooksReader |
|
| 37 | - */ |
|
| 38 | - static private function instance() { |
|
| 39 | - static $instance; |
|
| 33 | + /** |
|
| 34 | + * Get the shared reader instance. |
|
| 35 | + * |
|
| 36 | + * @return HooksReader |
|
| 37 | + */ |
|
| 38 | + static private function instance() { |
|
| 39 | + static $instance; |
|
| 40 | 40 | |
| 41 | - if ( ! $instance ) { |
|
| 42 | - $instance = new HooksReader(); |
|
| 43 | - } |
|
| 41 | + if ( ! $instance ) { |
|
| 42 | + $instance = new HooksReader(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - return $instance; |
|
| 46 | - } |
|
| 45 | + return $instance; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Shared implementation of hook reading logic. |
|
| 50 | - * |
|
| 51 | - * @param object $target Object to read annotations from. |
|
| 52 | - * @param \Closure $condition Whether the annotation should be registered. |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - static public function read( $target, $condition ) { |
|
| 56 | - $reader = static::instance()->reader(); |
|
| 57 | - $rmethods = (new ReflectionClass( $target ))->getMethods(); |
|
| 48 | + /** |
|
| 49 | + * Shared implementation of hook reading logic. |
|
| 50 | + * |
|
| 51 | + * @param object $target Object to read annotations from. |
|
| 52 | + * @param \Closure $condition Whether the annotation should be registered. |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + static public function read( $target, $condition ) { |
|
| 56 | + $reader = static::instance()->reader(); |
|
| 57 | + $rmethods = (new ReflectionClass( $target ))->getMethods(); |
|
| 58 | 58 | |
| 59 | - $hooks = []; |
|
| 59 | + $hooks = []; |
|
| 60 | 60 | |
| 61 | - foreach ( $rmethods as $rmethod ) { |
|
| 62 | - foreach ( $reader->getMethodAnnotations( $rmethod ) as $annotation ) { |
|
| 63 | - if ( $condition( $annotation ) ) { |
|
| 64 | - $hooks[] = [ |
|
| 65 | - 'hook' => $annotation->hook, |
|
| 66 | - 'method' => $rmethod->getName(), |
|
| 67 | - 'priority' => $annotation->priority, |
|
| 68 | - 'args' => $annotation->args, |
|
| 69 | - ]; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - } |
|
| 61 | + foreach ( $rmethods as $rmethod ) { |
|
| 62 | + foreach ( $reader->getMethodAnnotations( $rmethod ) as $annotation ) { |
|
| 63 | + if ( $condition( $annotation ) ) { |
|
| 64 | + $hooks[] = [ |
|
| 65 | + 'hook' => $annotation->hook, |
|
| 66 | + 'method' => $rmethod->getName(), |
|
| 67 | + 'priority' => $annotation->priority, |
|
| 68 | + 'args' => $annotation->args, |
|
| 69 | + ]; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return $hooks; |
|
| 75 | - } |
|
| 74 | + return $hooks; |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -8,27 +8,27 @@ discard block |
||
| 8 | 8 | * @Target({"METHOD"}) |
| 9 | 9 | */ |
| 10 | 10 | class Action { |
| 11 | - /** |
|
| 12 | - * Hook to attach method to. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - * @Required |
|
| 16 | - */ |
|
| 17 | - public $hook; |
|
| 11 | + /** |
|
| 12 | + * Hook to attach method to. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + * @Required |
|
| 16 | + */ |
|
| 17 | + public $hook; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Priority level for the hook. |
|
| 21 | - * |
|
| 22 | - * @var int |
|
| 23 | - */ |
|
| 24 | - public $priority = 10; |
|
| 19 | + /** |
|
| 20 | + * Priority level for the hook. |
|
| 21 | + * |
|
| 22 | + * @var int |
|
| 23 | + */ |
|
| 24 | + public $priority = 10; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Arguments to call the hook with. |
|
| 28 | - * |
|
| 29 | - * @var int |
|
| 30 | - */ |
|
| 31 | - public $args = 1; |
|
| 26 | + /** |
|
| 27 | + * Arguments to call the hook with. |
|
| 28 | + * |
|
| 29 | + * @var int |
|
| 30 | + */ |
|
| 31 | + public $args = 1; |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | /** |
@@ -38,25 +38,25 @@ discard block |
||
| 38 | 38 | * @Target({"METHOD"}) |
| 39 | 39 | */ |
| 40 | 40 | class Filter { |
| 41 | - /** |
|
| 42 | - * Hook to attach method to. |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - * @Required |
|
| 46 | - */ |
|
| 47 | - public $hook; |
|
| 41 | + /** |
|
| 42 | + * Hook to attach method to. |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + * @Required |
|
| 46 | + */ |
|
| 47 | + public $hook; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Priority level for the hook. |
|
| 51 | - * |
|
| 52 | - * @var int |
|
| 53 | - */ |
|
| 54 | - public $priority = 10; |
|
| 49 | + /** |
|
| 50 | + * Priority level for the hook. |
|
| 51 | + * |
|
| 52 | + * @var int |
|
| 53 | + */ |
|
| 54 | + public $priority = 10; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Arguments to call the hook with. |
|
| 58 | - * |
|
| 59 | - * @var int |
|
| 60 | - */ |
|
| 61 | - public $args = 1; |
|
| 56 | + /** |
|
| 57 | + * Arguments to call the hook with. |
|
| 58 | + * |
|
| 59 | + * @var int |
|
| 60 | + */ |
|
| 61 | + public $args = 1; |
|
| 62 | 62 | } |
@@ -11,25 +11,25 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | trait Annotatable { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Read the class methods and return the filter hooks. |
|
| 16 | - * |
|
| 17 | - * @return array |
|
| 18 | - */ |
|
| 19 | - public function filter_hooks() { |
|
| 20 | - return HooksReader::read( $this, function( $annotation ) { |
|
| 21 | - return $annotation instanceof Annotation\Filter; |
|
| 22 | - }); |
|
| 23 | - } |
|
| 14 | + /** |
|
| 15 | + * Read the class methods and return the filter hooks. |
|
| 16 | + * |
|
| 17 | + * @return array |
|
| 18 | + */ |
|
| 19 | + public function filter_hooks() { |
|
| 20 | + return HooksReader::read( $this, function( $annotation ) { |
|
| 21 | + return $annotation instanceof Annotation\Filter; |
|
| 22 | + }); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Read the class methods and return the action hooks. |
|
| 27 | - * |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - public function action_hooks() { |
|
| 31 | - return HooksReader::read( $this, function( $annotation ) { |
|
| 32 | - return $annotation instanceof Annotation\Action; |
|
| 33 | - }); |
|
| 34 | - } |
|
| 25 | + /** |
|
| 26 | + * Read the class methods and return the action hooks. |
|
| 27 | + * |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + public function action_hooks() { |
|
| 31 | + return HooksReader::read( $this, function( $annotation ) { |
|
| 32 | + return $annotation instanceof Annotation\Action; |
|
| 33 | + }); |
|
| 34 | + } |
|
| 35 | 35 | } |