From d0a9c699882ee260b255449ee66fe036b026cad3 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 29 Apr 2018 18:50:58 +0200 Subject: [PATCH 1/2] Allow Delegates *or* objects as parameters to frontend --- src/main/php/web/frontend/Frontend.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/php/web/frontend/Frontend.class.php b/src/main/php/web/frontend/Frontend.class.php index 9935dd6..8c33bfd 100755 --- a/src/main/php/web/frontend/Frontend.class.php +++ b/src/main/php/web/frontend/Frontend.class.php @@ -12,12 +12,12 @@ class Frontend implements Handler { /** * Instantiates a new frontend * - * @param object $handler + * @param web.frontend.Delegates|object $arg * @param web.frontend.Templates $templates * @param string $base */ - public function __construct($handler, Templates $templates, $base= '') { - $this->delegates= new Delegates($handler); + public function __construct($arg, Templates $templates, $base= '') { + $this->delegates= $arg instanceof Delegates ? $arg : new MethodsIn($arg); $this->templates= $templates; $this->base= rtrim($base, '/'); } From c82e53f9c086e67c0e4e8f37c597cbabea21f9e3 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 29 Apr 2018 18:51:18 +0200 Subject: [PATCH 2/2] Add two Delegates implementations: MethodsIn($object), ClassesIn($package) --- src/main/php/web/frontend/ClassesIn.class.php | 24 +++++++++++++++++++ src/main/php/web/frontend/Delegates.class.php | 15 ++++++++---- src/main/php/web/frontend/MethodsIn.class.php | 12 ++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100755 src/main/php/web/frontend/ClassesIn.class.php create mode 100755 src/main/php/web/frontend/MethodsIn.class.php diff --git a/src/main/php/web/frontend/ClassesIn.class.php b/src/main/php/web/frontend/ClassesIn.class.php new file mode 100755 index 0000000..d0f08ef --- /dev/null +++ b/src/main/php/web/frontend/ClassesIn.class.php @@ -0,0 +1,24 @@ +getClasses() as $class) { + if ($class->reflect()->isInstantiable()) { + $this->with($new ? $new($class) : $class->newInstance()); + } + } + } +} \ No newline at end of file diff --git a/src/main/php/web/frontend/Delegates.class.php b/src/main/php/web/frontend/Delegates.class.php index 52f87c9..717484f 100755 --- a/src/main/php/web/frontend/Delegates.class.php +++ b/src/main/php/web/frontend/Delegates.class.php @@ -8,8 +8,14 @@ class Delegates { private $patterns= []; - /** @param object $instance */ - public function __construct($instance) { + /** + * Routes to instance methods based on annotations + * + * @param object $instance + * @return self + * @throws lang.IllegalArgumentException + */ + public function with($instance) { if (!is_object($instance)) { throw new IllegalArgumentException('Expected an object, have '.typeof($instance)); } @@ -17,13 +23,14 @@ public function __construct($instance) { foreach (typeof($instance)->getMethods() as $method) { $name= $method->getName(); foreach ($method->getAnnotations() as $verb => $segment) { - $p= $segment + $pattern= $segment ? preg_replace(['/\{([^:}]+):([^}]+)\}/', '/\{([^}]+)\}/'], ['(?<$1>$2)', '(?<$1>[^/]+)'], $segment) : '.+' ; - $this->patterns['#'.$verb.$p.'$#']= new Delegate($instance, $method); + $this->patterns['#'.$verb.$pattern.'$#']= new Delegate($instance, $method); } } + return $this; } /** diff --git a/src/main/php/web/frontend/MethodsIn.class.php b/src/main/php/web/frontend/MethodsIn.class.php new file mode 100755 index 0000000..78f777e --- /dev/null +++ b/src/main/php/web/frontend/MethodsIn.class.php @@ -0,0 +1,12 @@ +with($instance); + } +} \ No newline at end of file