Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Router rewrite for PSR-7 #40

Open
wants to merge 47 commits into
base: develop
Choose a base branch
from

Conversation

Xerkus
Copy link
Member

@Xerkus Xerkus commented Nov 4, 2017

Work in progress

Goal of the rewrite is to switch component to the native support of PSR-7.

Console routing and http request routing are too different. Common interface proved to be limiting and of little practical value: routes were not shared between the two and consumers had to accept one and outright reject another in most cases.
Next implementation of zend-mvc will be PSR-7 based as well and zend-mvc-console will be dropped.

For that reason, support for Zend\Http\RequestInterface will be dropped as too incompatible with PSR-7 and support for the non-http routing with Zend\Stdlib\RequestInterface will not be retained as impractical.
Effectively that will make zend-router a PSR-7 only router.

Also, this PR bumps minimum php to 7.1, introduces strict types, scalar and return typehinting

Note: this PR is a subject for rebases and commit squashes. Ask before using as a basis for your work.

@Xerkus Xerkus self-assigned this Nov 4, 2017
@Xerkus Xerkus added this to the 4.0.0 milestone Nov 4, 2017
@Xerkus Xerkus changed the base branch from master to develop November 4, 2017 03:14
@Xerkus Xerkus force-pushed the make-mvc-great-again branch 2 times, most recently from b19ed4a to c5cfea4 Compare November 4, 2017 03:32
@Xerkus Xerkus force-pushed the make-mvc-great-again branch 4 times, most recently from f1ed732 to 1eba2d3 Compare November 16, 2017 01:45
@Xerkus
Copy link
Member Author

Xerkus commented Nov 16, 2017

Small note:
I wanted to keep RouteMatch as a separate thing from RouteResult to keep it more or less backward compatible.
It is used extensively in zend-mvc, but consumers should really be using PSR-7 request attributes for matched parameters. While it will cause additional migration challenges for zend-mvc users, it will be more in line with zend-expressive and the rest of the industry.
Besides, attempts to keep RouteMatch and request attributes in sync are error prone and pretty much guaranteed to produce subtle bugs.

@Xerkus
Copy link
Member Author

Xerkus commented Nov 16, 2017

Routes doing partial match conditionally and depending on the concrete implementation are violating RouteInterface contract.
To make things more straightforward, RouteInterface is now not allowed to return partial match. PartialRouteInterface extending RouteInterface was introduced instead.
Chain, Hostname, Literal, Method, Regex, Scheme and Segment are now partial routes.
Chain was changed to accept partial routes only, Part was changed to accept only partial route for its base route.

@zendframework zendframework deleted a comment from coveralls Nov 16, 2017
@zendframework zendframework deleted a comment from coveralls Nov 16, 2017
@Xerkus Xerkus mentioned this pull request Nov 17, 2017
11 tasks
@Xerkus Xerkus force-pushed the make-mvc-great-again branch 6 times, most recently from f6e49ca to bf40d4f Compare November 22, 2017 16:21
@@ -18,20 +18,18 @@
"forum": "https://discourse.zendframework.com/c/questions/components"
},
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.1",
"container-interop/container-interop": "^1.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use psr/container (PSR-11), or we need first SMv4?

@@ -54,7 +61,7 @@ class Regex implements RouteInterface
* @param string $spec
* @param array $defaults
*/
public function __construct($regex, $spec, array $defaults = [])
public function __construct(string $regex, $spec, array $defaults = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have typehint on $spec? In PHPDocs I can see "string" only.

@@ -51,7 +58,7 @@ public function __construct($scheme, array $defaults = [])
* @return Scheme
* @throws Exception\InvalidArgumentException
*/
public static function factory($options = [])
public static function factory($options = []) : RouteInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have "iterable" type hint here?

@@ -121,7 +128,7 @@ public function __construct($route, array $constraints = [], array $defaults = [
* @return Segment
* @throws Exception\InvalidArgumentException
*/
public static function factory($options = [])
public static function factory($options = []) : RouteInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"iterable" typehint here?

@@ -154,7 +161,7 @@ public static function factory($options = [])
* @return array
* @throws Exception\RuntimeException
*/
protected function parseRouteDefinition($def)
protected function parseRouteDefinition($def) : array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"string" typehint on param

@@ -229,7 +236,7 @@ protected function parseRouteDefinition($def)
* @param int $groupIndex
* @return string
*/
protected function buildRegex(array $parts, array $constraints, &$groupIndex = 1)
protected function buildRegex(array $parts, array $constraints, &$groupIndex = 1) : string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add type hint on the last param here, please?

@@ -64,7 +66,7 @@ public function __construct(RoutePluginManager $routePluginManager = null)
* @return SimpleRouteStack
* @throws Exception\InvalidArgumentException
*/
public static function factory($options = [])
public static function factory($options = []) : RouteInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... why not "SimpleRouteStack" as return type in signature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not possible

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-router; a new issue has been opened at laminas/laminas-router#6.

@weierophinney
Copy link
Member

This repository has been moved to laminas/laminas-router. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:

  • Squash all commits in your branch (git rebase -i origin/{branch})
  • Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
  • Run the laminas/laminas-migration tool on the code.
  • Clone laminas/laminas-router to another directory.
  • Copy the files from the second bullet point to the clone of laminas/laminas-router.
  • In your clone of laminas/laminas-router, commit the files, push to your fork, and open the new PR.
    We will be providing tooling via laminas/laminas-migration soon to help automate the process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants