Skip to content

Commit 4fcc2d4

Browse files
authored
Merge pull request #58 from settermjd/refine-routes-list-functionality
Refine routes list functionality
2 parents 375ea79 + a5189f2 commit 4fcc2d4

17 files changed

+596
-1095
lines changed

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,24 @@ The command supports several options, listed in the table below.
6161
| `--format` | `-f` | Set the format of the command's output. The supported values are `table`, which is the default, and `json`. If you set the format to json, then we recommend using [jq](https://stedolan.github.io/jq/manual/) to query/filter the command's output. |
6262
| `--sort` | `-s` | Sort the command's output. The supported values are `name` and `path`. |
6363
| `--supports-method` | `-m` | Accepts a comma-separated list of one or more HTTP methods, and filters out routes which don't support those methods. |
64-
| `--has-path` | `-p` | Accepts a comma-separated list of one or more paths, and filters out routes with paths that don't match. The paths can be a regular expression, supported by the `preg_*` functions. For example, "/,/api/ping,*/ping". |
65-
| `--has-name` | `-n` | Accepts a comma-separated list of one or more names, and filters out routes with names that don't match. The names can be fixed strings, or regular expressions supported by the `preg_*` functions. For example, "user,user.register,*.register,user*". |
64+
| `--has-path` | `-p` | Accepts a comma-separated list of one or more paths, and filters out routes with paths that don't match. The paths can be a regular expression, supported by the `preg_*` functions. For example, "/,/api/ping,*/ping". |
65+
| `--has-name` | `-n` | Accepts a comma-separated list of one or more names, and filters out routes with names that don't match. The names can be fixed strings, or regular expressions supported by the `preg_*` functions. For example, "user,user.register,*.register,user*". |
6666
| `--has-middleware` | `-w` | Accepts a comma-separated list of one or more middleware classes, and filters out routes that do not require those classes. The classes can be fully-qualified, unqualified, or a regular expression, supported by the preg_* functions. For example, "\Mezzio\Middleware\LazyLoadingMiddleware,LazyLoadingMiddleware,\Mezzio*". |
6767
<!-- markdownlint-enable MD037 -->
6868

69+
##### Configuration
70+
71+
By default, `Mezzio\Tooling\Routes\DefaultRoutesConfigLoaderFactory` registers a `ConfigLoaderInterface` service with the application's DI container, which retrieves the application's routes from two sources:
72+
73+
- `config/routes.php`
74+
- Routes registered by any loaded `ConfigProvider` class
75+
76+
However, this is a default/fallback implementation.
77+
If you don't store any routes in `config/routes.php` or need a custom implementation, then you need to do two things:
78+
79+
1. Write a custom loader implementation that implements `Mezzio\Tooling\Routes\ConfigLoaderInterface`
80+
2. Register it with the application's DI container as an alias for `Mezzio\Tooling\Routes\ConfigLoaderInterface`
81+
6982
##### Usage Example
7083

7184
Here is an example of what you can expect from running the command.

phpunit.xml.dist

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@
77
beStrictAboutOutputDuringTests="true"
88
colors="true"
99
cacheDirectory=".phpunit.cache"
10-
beStrictAboutCoverageMetadata="true">
10+
beStrictAboutCoverageMetadata="true"
11+
displayDetailsOnPhpunitDeprecations="true"
12+
displayDetailsOnTestsThatTriggerDeprecations="true"
13+
displayDetailsOnTestsThatTriggerErrors="true"
14+
displayDetailsOnTestsThatTriggerNotices="true"
15+
displayDetailsOnTestsThatTriggerWarnings="true"
16+
displayDetailsOnSkippedTests="true"
17+
displayDetailsOnIncompleteTests="true"
18+
failOnPhpunitDeprecation="true"
19+
failOnEmptyTestSuite="true"
20+
failOnWarning="true"
21+
failOnNotice="true"
22+
failOnDeprecation="true"
23+
>
1124
<testsuites>
1225
<testsuite name="mezzio-tooling">
1326
<directory>./test</directory>

src/ConfigProvider.php

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Mezzio\Tooling\Module\DeregisterCommandFactory;
2626
use Mezzio\Tooling\Module\RegisterCommand;
2727
use Mezzio\Tooling\Module\RegisterCommandFactory;
28+
use Mezzio\Tooling\Routes\ConfigLoaderInterface;
29+
use Mezzio\Tooling\Routes\DefaultRoutesConfigLoaderFactory;
2830
use Mezzio\Tooling\Routes\ListRoutesCommand;
2931
use Mezzio\Tooling\Routes\ListRoutesCommandFactory;
3032

@@ -66,6 +68,7 @@ public function getDependencies(): array
6668
{
6769
return [
6870
'factories' => [
71+
ConfigLoaderInterface::class => DefaultRoutesConfigLoaderFactory::class,
6972
Create::class => CreateFactory::class,
7073
CreateActionCommand::class => CreateActionCommandFactory::class,
7174
CreateCommand::class => CreateCommandFactory::class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mezzio\Tooling\Routes;
6+
7+
use Mezzio\Application;
8+
use Mezzio\MiddlewareFactory;
9+
use Psr\Container\ContainerInterface;
10+
11+
/**
12+
* This class provides a default factory implementation for retrieving an app's routes
13+
14+
* The assumption with this "default" config loader factory is that you're
15+
* loading routes from config/routes.php in your Mezzio application. So, if
16+
* that's not the case in your application, create a custom loader
17+
* implementation and override the alias in the DiC.
18+
*/
19+
final class DefaultRoutesConfigLoaderFactory
20+
{
21+
public function __invoke(ContainerInterface $container): ConfigLoaderInterface
22+
{
23+
/** @var Application $application */
24+
$application = $container->get(Application::class);
25+
26+
/** @var MiddlewareFactory $factory */
27+
$factory = $container->get(MiddlewareFactory::class);
28+
29+
return new RoutesFileConfigLoader(
30+
'config/routes.php',
31+
$application,
32+
$factory,
33+
$container
34+
);
35+
}
36+
}

src/Routes/Filter/RouteFilterOptions.php

+17-72
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,30 @@
44

55
namespace Mezzio\Tooling\Routes\Filter;
66

7-
use function get_object_vars;
8-
use function in_array;
9-
use function is_array;
10-
use function is_string;
7+
use function array_map;
8+
use function strtoupper;
119

10+
/** @internal */
1211
final class RouteFilterOptions
1312
{
14-
private string $middleware;
15-
private string $name;
16-
private string $path;
17-
18-
/** @var array<array-key,string> */
19-
private array $methods = [];
13+
/** @var list<non-empty-string> */
14+
public readonly array $methods;
2015

2116
/**
22-
* @param string|array<array-key,string> $methods
17+
* @param non-empty-string|null $middleware
18+
* @param non-empty-string|null $name
19+
* @param non-empty-string|null $path
20+
* @param list<non-empty-string> $methods
2321
*/
2422
public function __construct(
25-
string $middleware = '',
26-
string $name = '',
27-
string $path = '',
28-
$methods = []
23+
public readonly string|null $middleware,
24+
public readonly string|null $name,
25+
public readonly string|null $path,
26+
array $methods
2927
) {
30-
if (is_string($methods)) {
31-
$this->methods = [$methods];
32-
}
33-
if (is_array($methods)) {
34-
$this->methods = $methods;
35-
}
36-
$this->middleware = $middleware;
37-
$this->name = $name;
38-
$this->path = $path;
39-
}
40-
41-
public function has(string $filterOption): bool
42-
{
43-
if (in_array($filterOption, ['middleware', 'name', 'path'])) {
44-
return $this->$filterOption !== null;
45-
}
46-
47-
if ($filterOption === 'methods') {
48-
return [] !== $this->methods;
49-
}
50-
51-
return false;
52-
}
53-
54-
public function getMiddleware(): string
55-
{
56-
return $this->middleware;
57-
}
58-
59-
public function getName(): string
60-
{
61-
return $this->name;
62-
}
63-
64-
public function getPath(): string
65-
{
66-
return $this->path;
67-
}
68-
69-
/**
70-
* @return array<array-key,string>
71-
*/
72-
public function getMethods(): array
73-
{
74-
return $this->methods;
75-
}
76-
77-
public function toArray(): array
78-
{
79-
$values = [];
80-
foreach (get_object_vars($this) as $key => $value) {
81-
if (! empty($value)) {
82-
$values[$key] = $value;
83-
}
84-
}
85-
86-
return $values;
28+
/** Psalm does not like a first-class callable here */
29+
$this->methods = array_map(static function (string $method): string {
30+
return strtoupper($method);
31+
}, $methods);
8732
}
8833
}

0 commit comments

Comments
 (0)