Skip to content

Commit

Permalink
Merge pull request #5 from xp-forge/feature/delegates
Browse files Browse the repository at this point in the history
Delegates
  • Loading branch information
thekid authored Apr 29, 2018
2 parents 0d79243 + c82e53f commit 81c788a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/main/php/web/frontend/ClassesIn.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace web\frontend;

use lang\reflect\Package;

/**
* Creates routing based on classes in a given package
*/
class ClassesIn extends Delegates {

/**
* Creates this delegates instance
*
* @param lang.reflect.Package|string $package
* @param function(lang.XPClass): object $new Optional function to create instances
*/
public function __construct($package, $new= null) {
$p= $package instanceof Package ? $package : Package::forName($package);
foreach ($p->getClasses() as $class) {
if ($class->reflect()->isInstantiable()) {
$this->with($new ? $new($class) : $class->newInstance());
}
}
}
}
15 changes: 11 additions & 4 deletions src/main/php/web/frontend/Delegates.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@
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));
}

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;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/php/web/frontend/Frontend.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/php/web/frontend/MethodsIn.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace web\frontend;

/**
* Creates routing based on a given instance
*/
class MethodsIn extends Delegates {

/** @param object $instance */
public function __construct($instance) {
$this->with($instance);
}
}

0 comments on commit 81c788a

Please sign in to comment.