Skip to content

Commit a52607a

Browse files
authored
Merge pull request #10 from mindplay-dk/3.0.0
3.0.0
2 parents 6defdfa + 0be7b63 commit a52607a

20 files changed

+286
-707
lines changed

.github/workflows/ci.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
CI:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
php:
14+
- '8.0'
15+
- '8.1'
16+
- '8.2'
17+
- '8.3'
18+
steps:
19+
- uses: shivammathur/setup-php@2.28.0
20+
with:
21+
php-version: ${{ matrix.php }}
22+
- uses: actions/checkout@v3
23+
- run: composer install --no-progress --no-ansi --no-interaction --dev --prefer-dist
24+
- run: php test/test.php

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/.idea
1+
.*
2+
!.git*
23
/vendor
34
/test/build

LICENSE LICENSE.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Copyright (c) 2023, Rasmus Schultz
12
Copyright (c) 2015, Vadim Baryshev
23
All rights reserved.
34

README.md

+25-78
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# mindplay/timber
22

3-
[![PHP Version](https://img.shields.io/badge/php-7.3%2B-blue.svg)](https://packagist.org/packages/mindplay/timber)
4-
[![Build Status](https://travis-ci.org/mindplay-dk/timber.svg)](https://travis-ci.org/mindplay-dk/timber)
3+
[![PHP Version](https://img.shields.io/badge/php-8.0%2B-blue.svg)](https://packagist.org/packages/mindplay/timber)
4+
[![Build status](https://github.com/mindplay-dk/timber/actions/workflows/ci.yml/badge.svg)
55

6-
Timber is a request router with regular expression support, high performance, and a
6+
Timber is a router library with regular expression support, high performance, and a
77
developer-friendly, human-readable API.
88

9-
Originally a fork of [TreeRoute](https://github.com/baryshev/TreeRoute) by
10-
[Vadim Baryshev](https://github.com/baryshev), the API and feature-set quickly
11-
grew into something else entirely. What does carry over from the original fork,
12-
is great performance.
13-
149

1510
## Installation
1611

@@ -19,28 +14,22 @@ Install the latest version with `composer require mindplay/timber`
1914

2015
## Introduction
2116

22-
The core router system is independent of some other optional features - this includes
23-
a Dispatcher and a UrlHelper, both of which work great together with the router, but
24-
the router itself is not dependent on either of these components.
17+
This package provides a minimal router facility: a place to register path patterns, and
18+
a means of resolving these to controller names.
2519

2620

2721
## Usage
2822

2923
In the examples below, we assume that handlers (attached using `get()`, `post()`, etc.)
30-
are controller class-names - the included Dispatcher (see below) uses a class-per-action
31-
strategy, which may not be everyone's cup of tea, but the point is that, the meaning or
32-
interpretation of the handler string isn't baked into the router; it's supported by the
33-
Dispatcher only, and as such, you can easily implement any convention or strategy you
34-
like, either by doing your own dispatch (without using our Dispatcher) or by extending
35-
the Dispatcher and overriding the `toCallable()` function.
24+
are controller class-names - in your application, they might be component IDs for a DI
25+
container, file-names, or whatever else you like.
3626

37-
Note the use of the magic `::class` constant in the following examples - the library is
38-
compatible with PHP 5.4, but this syntax is only supported by PHP 5.5 and up.
39-
40-
Basic usage of the router, without Dispatcher, goes a little something like this:
27+
Basic usage of the router looks like this:
4128

4229
```PHP
4330
use mindplay\timber\Router;
31+
use mindplay\timber\Result;
32+
use mindplay\timber\Error;
4433

4534
require __DIR__ . '/vendor/autoload.php';
4635

@@ -71,28 +60,17 @@ $url = '/news/1';
7160

7261
$result = $router->resolve($method, $url);
7362

74-
if (!$result->error) {
75-
$handler = $result->handler;
76-
$params = $result->params;
77-
// Do something with handler and params
63+
if ($result instanceof Error) {
64+
header("HTTP/1.1 {$result->status} ${result->message}");
65+
// ...error response here...
66+
return;
7867
} else {
79-
switch ($result->error->code) {
80-
case 404 :
81-
// Not found handler here
82-
break;
83-
case 405 :
84-
// Method not allowed handler here
85-
$allowedMethods = $result->error->allowed;
86-
if ($method == 'OPTIONS') {
87-
// OPTIONS method handler here
88-
}
89-
break;
90-
}
68+
// ...dispatch $result->handler with $result->params...
9169
}
9270
```
9371

94-
If you're building a set of routes under the same parent route, you can continue routing
95-
from by keeping a temporary reference to a parent route - for example:
72+
If you're building a set of routes under the same parent route, you can continue building
73+
from a parent route - for example:
9674

9775
```PHP
9876
$admin = $router->route('admin')->get(AdminMenu::class);
@@ -103,8 +81,7 @@ $admin->route('groups')->get(AdminGroupList::class);
10381

10482
This example will route `/admin` to `AdminMenu`, and `/admin/users` to `AdminUserList`, etc.
10583

106-
Continuing from a parent route avoids the overhead of repeatedly parsing the same parent path,
107-
and also enables modular reuse of route definitions - for example:
84+
This also feature enables modular reuse of route definitions - for example:
10885

10986
```PHP
11087
$build_comment_routes = function (Route $parent) {
@@ -120,43 +97,6 @@ This example creates two identical sets of routes for displaying and posting com
12097
different parent routes.
12198

12299

123-
### Dispatch
124-
125-
We also provide a [Dispatcher](src/Dispatcher.php) class and [Controller](src/Controller.php)
126-
interface, following a simple convention of handler names being class-names, and a
127-
class-per-action strategy:
128-
129-
```PHP
130-
use mindplay\timber\Router;
131-
use mindplay\timber\Controller;
132-
use mindplay\timber\Dispatcher;
133-
134-
class ShowNews implements Controller
135-
{
136-
public function run($id) {
137-
// load and display news article...
138-
}
139-
}
140-
141-
$router = new Router();
142-
$router->route('news/<id:int>', ShowNews::class);
143-
144-
$dispatcher = new Dispatcher($router);
145-
146-
$dispatcher->run('GET', '/news');
147-
```
148-
149-
The named parameter in this example will be converted to an integer and provided
150-
to the `run()` method.
151-
152-
153-
### URL Creation
154-
155-
In addition, we provide a [base class for URL creation helpers](src/UrlHelper.php) -
156-
this has no direct relationship with the router as such, it's provided for
157-
convenience, to support our overall strategy of creating testable URL helpers.
158-
159-
160100
## Optimization
161101

162102
You can save and restore the defined routes:
@@ -218,3 +158,10 @@ after some discussion, we decided URL creation provides only a small benefit, gu
218158
is consistent with defined patterns; but also, we value the freedom to fully customize URL creation on a
219159
case-by-case basis using simpler code (as per [case 3](https://gist.github.com/mindplay-dk/feb4768dbb118c651ba0#file-router-3-php))
220160
and as such the absence of URL creation can actually be seen as a benefit.
161+
162+
## Acknowledgements
163+
164+
Timber started as a fork of [TreeRoute](https://github.com/baryshev/TreeRoute) by
165+
[Vadim Baryshev](https://github.com/baryshev), the API and feature-set quickly
166+
grew into something else entirely. What does carry over from the original fork,
167+
is great performance.

UPGRADING.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Upgrade Notes
2+
3+
### `2.x` to `3.x`
4+
5+
Version 3 doesn't really attempt to be backwards compatible - I haven't been actively
6+
using PHP for some years, so this release is a revived and modernized package for PHP 8,
7+
with a smaller scope.
8+
9+
Earlier versions of this package came with a Dispatcher and a URL builder, both of which
10+
have been removed from version `3.0.0` of this package:
11+
12+
- The Dispatcher had no real connection with the Router itself, and was more like a
13+
proof of concept than a full-blown implementation - if you wish to upgrade to `3.0.0`,
14+
you can copy the dispatcher from the
15+
[old version](https://github.com/mindplay-dk/timber/blob/6defdfaea55bb59171b54a9abcf388e836e18e66/src/Dispatcher.php)
16+
and use this as a basis for your own dispatcher.
17+
18+
- The URL builder also had no direct connection with the Router, as has been removed -
19+
you can find third-party packages on Packagist specifically for this purpose.
20+
21+
Major breaking changes:
22+
23+
- `Router::resolve()` now returns a union type of either `Result` or `Error`.
24+
- `Registry` is now called `PatternRegistry`, and `Symbol` is called `Pattern`.
25+
26+
Minor breaking changes:
27+
28+
- Static type-hints were added.
29+
30+
Other than that, what remains is more or less the same.

composer.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@
77
],
88
"license": "BSD-3-Clause",
99
"authors": [
10-
{
11-
"name": "Vadim Baryshev",
12-
"email": "vadimbaryshev@gmail.com"
13-
},
1410
{
1511
"name": "Rasmus Schultz",
1612
"email": "rasmus@mindplay.dk"
1713
}
1814
],
1915
"require": {
20-
"php": "^7.3 || ^8.0"
16+
"php": "^8.0"
2117
},
2218
"require-dev": {
23-
"mindplay/testies": "^1.0"
19+
"mindplay/testies": "^1.1.2"
2420
},
25-
"suggest": {
26-
"ext-intl": "provides better/faster support for slug-creation in URL helpers"
21+
"scripts": {
22+
"test": "php test/test.php"
2723
},
2824
"autoload": {
2925
"psr-4": {

composer.lock

+15-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controller.php

-19
This file was deleted.

0 commit comments

Comments
 (0)