Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"require" : {
"xp-framework/core": "^11.0 | ^10.0 | ^9.0 | ^8.0 | ^7.0",
"xp-framework/http": "^10.0 | ^9.0 | ^8.0 | ^7.0",
"xp-framework/reflection": "^2.0",
"xp-forge/web": "^3.0 | ^2.9",
"xp-forge/json": "^5.0 | ^4.0",
"php": ">=7.0.0"
Expand Down
36 changes: 19 additions & 17 deletions src/main/php/web/frontend/Delegate.class.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace web\frontend;

use lang\IllegalArgumentException;
use lang\reflection\Method;
use lang\{IllegalArgumentException, Reflection};
use web\frontend\View;
use lang\reflect\Method;

class Delegate {
private static $SOURCES;
Expand All @@ -25,11 +25,11 @@ static function __static() {
* Creates a new delegate
*
* @param object $instance
* @param string|lang.reflect.Method $method
* @param string|lang.reflection.Method $method
*/
public function __construct($instance, $method) {
$this->instance= $instance;
$this->method= $method instanceof Method ? $method : typeof($instance)->getMethod($method);
$this->method= $method instanceof Method ? $method : Reflection::type($instance)->method($method);
}

/** @return string */
Expand All @@ -40,7 +40,7 @@ public function group() {

/** @return string */
public function name() {
return nameof($this->instance).'::'.$this->method->getName();
return nameof($this->instance).'::'.$this->method->name();
}

/**
Expand All @@ -52,26 +52,28 @@ public function name() {
public function parameters() {
if (null === $this->parameters) {
$this->parameters= [];
foreach ($this->method->getParameters() as $param) {
if ($annotations= $param->getAnnotations()) {
foreach ($annotations as $from => $value) {
$source= self::$SOURCES[$from] ?? self::$SOURCES['default'];
}
foreach ($this->method->parameters() as $param) {

// Check for parameter annotations...
foreach ($param->annotations() as $from => $value) {
$source= self::$SOURCES[$from] ?? self::$SOURCES['default'];
$name= $value->argument(0) ?? $param->name();

$name= null === $value ? $param->getName() : $value;
if ($param->isOptional()) {
$default= $param->getDefaultValue();
$this->parameters[$name]= function($req, $name) use($source, $default) {
return $source($req, $name) ?? $default;
if ($param->optional()) {
$this->parameters[$name]= function($req, $name) use($source, $param) {
return $source($req, $name) ?? $param->default();
};
} else {
$this->parameters[$name]= $source;
}
} else {
$this->parameters[$param->getName()]= self::$SOURCES['segment'];
continue 2;
}

// ...falling back to selecting the parameter from the segments
$this->parameters[$param->name()]= self::$SOURCES['segment'];
}
}

return $this->parameters;
}

Expand Down
13 changes: 6 additions & 7 deletions src/main/php/web/frontend/Delegates.class.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php namespace web\frontend;

use lang\IllegalArgumentException;
use lang\{IllegalArgumentException, Reflection};

/**
* Matches request and routes to correct delegate
*/
/** Matches request and routes to correct delegate */
class Delegates {
public $patterns= [];

Expand All @@ -22,9 +20,10 @@ public function with($instance, $base= '/') {
}

$base= rtrim($base, '/');
foreach (typeof($instance)->getMethods() as $method) {
$name= $method->getName();
foreach ($method->getAnnotations() as $verb => $segment) {
foreach (Reflection::type($instance)->methods() as $method) {
$name= $method->name();
foreach ($method->annotations() as $verb => $annotation) {
$segment= $annotation->argument(0);
$pattern= preg_replace(
['/\{([^:}]+):([^}]+)\}/', '/\{([^}]+)\}/'],
['(?<$1>$2)', '(?<$1>[^/]+)'],
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/web/frontend/Frontend.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace web\frontend;

use lang\reflect\TargetInvocationException;
use lang\reflection\TargetException;
use web\{Error, Handler, Request};

/**
Expand Down Expand Up @@ -98,7 +98,7 @@ private function view($req, $res, $delegate, $matches= []) {
}

return $delegate->invoke($args);
} catch (TargetInvocationException $e) {
} catch (TargetException $e) {
return $this->errors()->handle($e->getCause());
}
}
Expand Down