Page MenuHomePhabricator

D589.id1885.diff
No OneTemporary

D589.id1885.diff

diff --git a/core/Path.php b/core/Path.php
--- a/core/Path.php
+++ b/core/Path.php
@@ -1,7 +1,9 @@
<?php namespace spitfire\core;
+use BadMethodCallException;
use spitfire\App;
use spitfire\core\router\Pattern;
+use spitfire\mvc\Controller;
/**
* The Path represents the set of data that Spitfire can obtain by parsing the
@@ -65,14 +67,18 @@
* the expected response format and the parameters it took from the URL.
*
*
- * @param string[]|Pattern[]|Pattern|string|null $controller
+ * @param string|Controller $controller
* @param string|Pattern|null $action
* @param string[]|Pattern[]|string|Pattern|null $object
* @param string[]|string $format
* @param string[]|Pattern[] $parameters
*/
- public function __construct($controller, $action, $object, $format = 'php', $parameters = Array()) {
- $this->controller = array_filter(is_array($controller)? $controller : [$controller]);
+ public function __construct($controller, $action, $object, $format = 'php', $parameters = Array())
+ {
+
+ if ($controller instanceof Pattern) { throw new BadMethodCallException('Controller wildcarding is forbidden'); }
+
+ $this->controller = $controller instanceof Controller? get_class($controller) : $controller;
$this->action = $action;
$this->object = array_filter(is_array($object)? $object : [$object]);
$this->format = $format;
@@ -86,7 +92,7 @@
* @return string[]
*/
public function getController() {
- return $this->controller ? $this->controller : (array) Environment::get('default_controller');
+ return $this->controller ? $this->controller : Environment::get('default_controller');
}
/**
@@ -143,7 +149,7 @@
* @return Path
*/
public function setController($controller) {
- $this->controller = array_filter(is_array($controller)? $controller : [$controller]);
+ $this->controller = $controller instanceof Controller? get_class($controller) : $controller;
return $this;
}
@@ -206,6 +212,7 @@
* the task to a subcontroller as it hasn't been able to find any method able
* to handle the request with this Path.
*
+ * @deprecated since v0.2
*/
public function shift() {
$this->controller[] = $this->action;
diff --git a/core/router/ParametrizedPath.php b/core/router/ParametrizedPath.php
--- a/core/router/ParametrizedPath.php
+++ b/core/router/ParametrizedPath.php
@@ -121,7 +121,7 @@
* internal data.
*/
$p = new Parameters();
- $p->addParameters($fn($this->getController(), $from->getController()));
+ $p->addParameters($fn([$this->getController()], [$from->getController()]));
$p->addParameters($fn([$this->getAction()], [$from->getAction()]));
$p->addParameters($fn($this->getObject(), $from->getObject(), true));
$p->addParameters($fn([$this->getParameters()], [$from->getParameters()]));
diff --git a/tests/router/ParametrizedPathTest.php b/tests/router/ParametrizedPathTest.php
--- a/tests/router/ParametrizedPathTest.php
+++ b/tests/router/ParametrizedPathTest.php
@@ -3,6 +3,7 @@
use PHPUnit\Framework\TestCase;
use spitfire\core\router\ParametrizedPath;
use spitfire\core\router\Pattern;
+use spitfire\mvc\Controller;
/*
* The MIT License
@@ -32,18 +33,18 @@
{
public function testReplacement() {
- $pp = new ParametrizedPath(new Pattern(':controller'), new Pattern(':action'), new Pattern(':object'));
+ $pp = new ParametrizedPath(Controller::class, new Pattern(':action'), new Pattern(':object'));
$path = $pp->replace(['controller' => 'b', 'action' => 'c', 'object' => 'd']);
- $this->assertEquals('b', $path->getController()[0]);
+ $this->assertEquals('d', $path->getObject()[0]);
$this->assertEquals('c', $path->getAction());
}
public function testReplacementArrays() {
- $pp = new ParametrizedPath([new Pattern(':c2'), new Pattern(':c1')], new Pattern(':action'), new Pattern(':object'));
+ $pp = new ParametrizedPath(Controller::class, new Pattern(':action'), new Pattern(':object'));
$path = $pp->replace(['c1' => 'b1', 'c2' => 'b2', 'action' => 'c', 'object' => 'd']);
- $this->assertEquals('b2', $path->getController()[0]);
+ $this->assertEquals(Controller::class, $path->getController());
$this->assertEquals('c', $path->getAction());
}

File Metadata

Mime Type
text/plain
Expires
Tue, Apr 13, 2:55 PM (4 w, 4 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5812
Default Alt Text
D589.id1885.diff (4 KB)

Event Timeline