Skip to content

Commit a60355a

Browse files
committed
Add possibility to explicitely pass target delegate and parameters
1 parent 90ea672 commit a60355a

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Web frontends change log
33

44
## ?.?.? / ????-??-??
55

6+
* Added possibility to explicitely pass target delegate and parameters
7+
to frontend handling, bypassing the request URI based routing logic.
8+
(@thekid)
69
* Added accessors for delegates and templates to `Frontend` - @thekid
710
* Merged PR #36: Migrate to new testing library - @thekid
811

src/main/php/web/frontend/Frontend.class.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace web\frontend;
22

33
use lang\reflect\TargetInvocationException;
4-
use web\{Error, Handler};
4+
use web\{Error, Handler, Request};
55

66
/**
77
* Annotation-based frontend
@@ -53,6 +53,23 @@ public function errors(): Errors { return $this->errors ?? $this->errors= new Ra
5353
/** Returns security */
5454
public function security(): Security { return $this->security ?? $this->security= new Security(); }
5555

56+
/**
57+
* Selects a target for a given method and path
58+
*
59+
* @param util.URI|web.Request|string $arg
60+
* @return ?var[]
61+
*/
62+
public function target($method, $arg= '/') {
63+
if ($arg instanceof URI) {
64+
$path= $arg->path();
65+
} else if ($arg instanceof Request) {
66+
$path= $arg->uri()->path();
67+
} else {
68+
$path= (string)$arg;
69+
}
70+
return $this->delegates->target($method, $path);
71+
}
72+
5673
/**
5774
* Determines view to be displayed, handling errors while going along.
5875
*

src/test/php/web/frontend/unittest/FrontendTest.class.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use lang\IllegalArgumentException;
44
use test\{Assert, Before, Expect, Test, Values};
55
use web\frontend\unittest\actions\Users;
6-
use web\frontend\{Exceptions, Frontend, RaiseErrors, Security, Templates, MethodsIn};
6+
use web\frontend\{Delegate, Exceptions, Frontend, RaiseErrors, Security, Templates, MethodsIn};
77

88
class FrontendTest {
99
private $templates;
@@ -74,4 +74,22 @@ public function security() {
7474
$s= new Security();
7575
Assert::equals($s, (new Frontend(new Users(), $this->templates))->enacting($s)->security());
7676
}
77+
78+
#[Test]
79+
public function all_target() {
80+
$users= new Users();
81+
Assert::equals(
82+
[new Delegate($users, 'all'), ['get/users']],
83+
(new Frontend($users, $this->templates))->target('get', '/users')
84+
);
85+
}
86+
87+
#[Test]
88+
public function find_target() {
89+
$users= new Users();
90+
Assert::equals(
91+
[new Delegate($users, 'find'), [0 => 'get/users/me', 1 => 'me', 'id' => 'me']],
92+
(new Frontend($users, $this->templates))->target('get', '/users/me')
93+
);
94+
}
7795
}

0 commit comments

Comments
 (0)