Skip to content

Commit 3c42855

Browse files
authored
Merge pull request #74 from sunrise-php/release/v2.9.0
v2.9.0
2 parents e1642a2 + 141bef9 commit 3c42855

File tree

65 files changed

+1883
-1411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1883
-1411
lines changed

.circleci/config.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,39 @@ jobs:
1010
steps:
1111
- checkout
1212
- run: php -v
13-
- run: composer install --no-interaction --prefer-source --no-suggest
13+
- run: composer install --no-interaction --no-suggest --prefer-source
1414
- run: php vendor/bin/phpunit --colors=always
1515
php72:
1616
docker:
1717
- image: circleci/php:7.2-cli-node-browsers
1818
steps:
1919
- checkout
2020
- run: php -v
21-
- run: composer install --no-interaction --prefer-source --no-suggest
21+
- run: composer install --no-interaction --no-suggest --prefer-source
2222
- run: php vendor/bin/phpunit --colors=always
2323
php73:
2424
docker:
2525
- image: circleci/php:7.3-cli-node-browsers
2626
steps:
2727
- checkout
2828
- run: php -v
29-
- run: composer install --no-interaction --prefer-source --no-suggest
29+
- run: composer install --no-interaction --no-suggest --prefer-source
3030
- run: php vendor/bin/phpunit --colors=always
3131
php74:
3232
docker:
3333
- image: circleci/php:7.4-cli-node-browsers
3434
steps:
3535
- checkout
3636
- run: php -v
37-
- run: composer install --no-interaction --prefer-source --no-suggest
37+
- run: composer install --no-interaction --no-suggest --prefer-source
3838
- run: php vendor/bin/phpunit --colors=always
3939
php80:
4040
docker:
4141
- image: circleci/php:8.0-cli-node-browsers
4242
steps:
4343
- checkout
4444
- run: php -v
45-
- run: composer install --no-interaction --prefer-source --no-suggest
45+
- run: composer install --no-interaction --no-suggest --prefer-source
4646
- run: php vendor/bin/phpunit --colors=always
4747
workflows:
4848
version: 2

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ indent_size = 4
1111
trim_trailing_whitespace = true
1212
insert_final_newline = true
1313

14-
[*.md]
15-
trim_trailing_whitespace = false
16-
1714
[*.yml]
1815
indent_size = 2

README.md

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## Installation
1313

1414
```bash
15-
composer require 'sunrise/http-router:^2.8'
15+
composer require 'sunrise/http-router:^2.9'
1616
```
1717

1818
## QuickStart
@@ -103,6 +103,12 @@ $this->get('home', '/', new CallableRequestHandler(function ($request) {
103103

104104
#### Strategy loading routes from descriptors (annotations or attributes)
105105

106+
Install the [doctrine/annotations](https://github.com/doctrine/annotations) package if you will be use annotations:
107+
108+
```bash
109+
composer require doctrine/annotations
110+
```
111+
106112
```php
107113
use Doctrine\Common\Annotations\AnnotationRegistry;
108114
use Sunrise\Http\Router\Loader\DescriptorDirectoryLoader;
@@ -199,6 +205,64 @@ $router->addMiddleware(new CallableMiddleware(function ($request, $handler) {
199205
emit($router->run(ServerRequestFactory::fromGlobals()));
200206
```
201207

208+
#### Work with PSR-11 container
209+
210+
##### Collector
211+
212+
```php
213+
$collector = new RouteCollector();
214+
215+
/** @var \Psr\Container\ContainerInterface $container */
216+
217+
// Pass DI container to the collector...
218+
$collector->setContainer($container);
219+
220+
// Objects passed as strings will be initialized through the DI container...
221+
$route = $collector->get('home', '/', HomeController::class, [
222+
FooMiddleware::class,
223+
BarMiddleware::class,
224+
]);
225+
```
226+
227+
##### Config loader
228+
229+
```php
230+
$loader = new CollectableFileLoader();
231+
232+
/** @var \Psr\Container\ContainerInterface $container */
233+
234+
// Pass DI container to the loader...
235+
$loader->setContainer($container);
236+
237+
// All found objects which has been passed as strings will be initialized through the DI container...
238+
$routes = $loader->load();
239+
```
240+
241+
##### Descriptor loader
242+
243+
```php
244+
$loader = new DescriptorDirectoryLoader();
245+
246+
/** @var \Psr\Container\ContainerInterface $container */
247+
248+
// Pass DI container to the loader...
249+
$loader->setContainer($container);
250+
251+
// All found objects will be initialized through the DI container...
252+
$routes = $loader->load();
253+
```
254+
255+
#### Descriptors cache (PSR-16)
256+
257+
```php
258+
$loader = new DescriptorDirectoryLoader();
259+
260+
/** @var \Psr\SimpleCache\CacheInterface $cache */
261+
262+
// Pass a cache to the loader...
263+
$loader->setCache($cache);
264+
```
265+
202266
#### Route Annotation Example
203267

204268
##### Minimal annotation view
@@ -239,6 +303,18 @@ final class EntryUpdateRequestHandler implements RequestHandlerInterface
239303
final class EntryUpdateRequestHandler implements RequestHandlerInterface
240304
```
241305

306+
##### One method only
307+
308+
```php
309+
/**
310+
* @Route(
311+
* name="home",
312+
* path="/",
313+
* method="GET",
314+
* )
315+
*/
316+
```
317+
242318
#### Route Attribute Example
243319

244320
##### Minimal attribute view
@@ -349,13 +425,13 @@ $collector->group(function ($collector) {
349425
->addMiddleware(...); // add the middleware(s) to the route...
350426
})
351427
->addPrefix('/entry') // add the prefix to the group...
352-
->unshiftMiddleware(...); // add the middleware(s) to the group...
428+
->prependMiddleware(...); // add the middleware(s) to the group...
353429
})
354430
->addPrefix('/v1') // add the prefix to the group...
355-
->unshiftMiddleware(...); // add the middleware(s) to the group...
431+
->prependMiddleware(...); // add the middleware(s) to the group...
356432
})
357433
->addPrefix('/api') // add the prefix to the group...
358-
->unshiftMiddleware(...); // add the middleware(s) to the group...
434+
->prependMiddleware(...); // add the middleware(s) to the group...
359435
```
360436

361437
### Route patterns
@@ -364,6 +440,22 @@ $collector->group(function ($collector) {
364440
$collector->get('api.entry.read', '/api/v1/entry/{id<\d+>}(/{optional<\w+>})');
365441
```
366442

443+
##### Global route patterns
444+
445+
```php
446+
// @uuid pattern
447+
$collector->get('api.entry.read', '/api/v1/entry/{id<@uuid>}');
448+
449+
// @slug pattern
450+
$collector->get('api.entry.read', '/api/v1/entry/{slug<@slug>}');
451+
452+
// Custom patterns (available from version 2.9.0):
453+
\Sunrise\Http\Router\Route::$patterns['@id'] = '[1-9][0-9]*';
454+
455+
// Just use the custom pattern...
456+
$collector->get('api.entry.read', '/api/v1/entry/{id<@id>}');
457+
```
458+
367459
### Hosts (available from version 2.6.0)
368460

369461
> Note: if you don't assign a host for a route, it will be available on any hosts!
@@ -384,6 +476,14 @@ $collector->group(function ($collector) {
384476
->setHost('admin.host');
385477
```
386478

479+
### CLI commands
480+
481+
```php
482+
use Sunrise\Http\Router\Command\RouteListCommand;
483+
484+
new RouteListCommand($router);
485+
```
486+
387487
---
388488

389489
## Test run

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php7",
1717
"php8",
1818
"attributes",
19-
"php-attributes"
19+
"php-attributes",
20+
"swagger"
2021
],
2122
"authors": [
2223
{
@@ -32,13 +33,14 @@
3233
"psr/http-server-handler": "^1.0",
3334
"psr/http-server-middleware": "^1.0",
3435
"psr/simple-cache": "^1.0",
35-
"fig/http-message-util": "^1.1",
36-
"doctrine/annotations": "^1.6"
36+
"fig/http-message-util": "^1.1"
3737
},
3838
"require-dev": {
3939
"phpunit/phpunit": "7.5.20|9.5.0",
4040
"sunrise/coding-standard": "1.0.0",
41-
"sunrise/http-factory": "1.1.0"
41+
"sunrise/http-factory": "1.1.0",
42+
"doctrine/annotations": "^1.6",
43+
"symfony/console": "^4.4"
4244
},
4345
"autoload": {
4446
"files": [

functions/path_parse.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@
5252
'_' => 1,
5353
];
5454

55-
/**
56-
* Named patters
57-
*
58-
* @var array
59-
*/
60-
const NAMED_PATTERNS = [
61-
'@slug' => '[\da-z-]+',
62-
'@uuid' => '[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}',
63-
];
64-
6555
/**
6656
* Parses the given path
6757
*
@@ -179,8 +169,8 @@ function path_parse(string $path) : array
179169
);
180170
}
181171

182-
if (isset(NAMED_PATTERNS[$attributes[$attributeIndex]['pattern']])) {
183-
$attributes[$attributeIndex]['pattern'] = NAMED_PATTERNS[$attributes[$attributeIndex]['pattern']];
172+
if (isset(Route::$patterns[$attributes[$attributeIndex]['pattern']])) {
173+
$attributes[$attributeIndex]['pattern'] = Route::$patterns[$attributes[$attributeIndex]['pattern']];
184174
}
185175

186176
$cursorInPattern = false;

0 commit comments

Comments
 (0)