Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 37 additions & 38 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@ name: CI
on: [push, pull_request]

jobs:
build-test:
runs-on: ubuntu-latest
strategy:
matrix:
php_version: [8.0, 8.1, 8.2]
composer_flags: ['', '--prefer-lowest']

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
extensions: xdebug

- uses: php-actions/composer@v5
with:
php_version: ${{ matrix.php_version }}
args: ${{ matrix.composer_flags }}
command: update

- name: Run tests
run: ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml
env:
XDEBUG_MODE: coverage

# - name: Submit coverage to Coveralls
# # We use php-coveralls library for this, as the official Coveralls GitHub Action lacks support for clover reports:
# # https://github.com/coverallsapp/github-action/issues/15
# env:
# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# COVERALLS_PARALLEL: true
# COVERALLS_FLAG_NAME: ${{ github.job }}-PHP-${{ matrix.php_version }} ${{ matrix.composer_flags }}
# run: |
# composer global require php-coveralls/php-coveralls
# ~/.composer/vendor/bin/php-coveralls -v

build-test:
runs-on: ubuntu-latest
strategy:
matrix:
php_version: [8.4]
composer_flags: ["", "--prefer-lowest"]

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
extensions: xdebug

- uses: php-actions/composer@v5
with:
php_version: ${{ matrix.php_version }}
args: ${{ matrix.composer_flags }}
command: update

- name: Run tests
run: ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml
env:
XDEBUG_MODE: coverage

# - name: Submit coverage to Coveralls
# # We use php-coveralls library for this, as the official Coveralls GitHub Action lacks support for clover reports:
# # https://github.com/coverallsapp/github-action/issues/15
# env:
# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# COVERALLS_PARALLEL: true
# COVERALLS_FLAG_NAME: ${{ github.job }}-PHP-${{ matrix.php_version }} ${{ matrix.composer_flags }}
# run: |
# composer global require php-coveralls/php-coveralls
# ~/.composer/vendor/bin/php-coveralls -v
23 changes: 12 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
}
],
"require": {
"php": "^7.3||^8.0",
"altorouter/altorouter": "^2.0.2",
"mindplay/middleman": "^3.0.3",
"php-di/invoker": "^2.3.0",
"psr/container": "^1.0",
"psr/http-message": "^1.0",
"php": "^8.4",
"altorouter/altorouter": "^2.0.3",
"laminas/laminas-diactoros": "^3.6.0",
"mindplay/middleman": "^4.0.4",
"php-di/invoker": "^2.3.6",
"psr/container": "^2.0.2",
"psr/http-message": "^2.0",
"psr/http-server-middleware": "^1.0",
"spatie/macroable": "^1.0",
"laminas/laminas-diactoros": "^2.4"
"spatie/macroable": "^1.0"
},
"require-dev": {
"php-di/php-di": "^6.3.4",
"phpunit/phpunit": "^9.5",
"php-coveralls/php-coveralls": "^2.4",
"mockery/mockery": "^1.4.3",
"php-coveralls/php-coveralls": "^2.4",
"php-di/php-di": "^7.1.1",
"phpunit/phpunit": "^12",
"rector/rector": "^2.1",
"squizlabs/php_codesniffer": "^3.6.0"
},
"autoload": {
Expand Down
31 changes: 11 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Rareloop Router Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Rareloop Router Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
13 changes: 13 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets(php84: true);
4 changes: 2 additions & 2 deletions src/Helpers/Formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Formatting
{
public static function removeTrailingSlash($input)
{
return rtrim($input, '/\\');
return rtrim((string) $input, '/\\');
}

public static function addTrailingSlash($input)
Expand All @@ -16,7 +16,7 @@ public static function addTrailingSlash($input)

public static function removeLeadingSlash($input)
{
return ltrim($input, '/\\');
return ltrim((string) $input, '/\\');
}

public static function addLeadingSlash($input)
Expand Down
17 changes: 5 additions & 12 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,27 @@ class Route
use Macroable;

private $uri;
private $methods = [];
private $routeAction;
private $name;
private $invoker = null;
private $middlewareResolver = null;
private $middleware = [];
private $paramConstraints = [];
private $controllerName = null;
private $controllerMethod = null;

public function __construct(
array $methods,
private array $methods,
string $uri,
$action,
Invoker $invoker = null,
MiddlewareResolver $resolver = null
private ?\Rareloop\Router\Invoker $invoker = null,
private ?\Rareloop\Router\MiddlewareResolver $middlewareResolver = null
) {
$this->invoker = $invoker;
$this->middlewareResolver = $resolver;

$this->methods = $methods;
$this->setUri($uri);
$this->setAction($action);
}

private function setUri($uri)
{
$this->uri = rtrim($uri, ' /');
$this->uri = rtrim((string) $uri, ' /');
}

private function setAction($action)
Expand Down Expand Up @@ -77,7 +70,7 @@ public function handle(ServerRequest $request, RouteParams $params): ResponseInt
return $this->middlewareResolver->resolve($name);
});

return $dispatcher->dispatch($request);
return $dispatcher->handle($request);
}

private function gatherMiddleware(): array
Expand Down
28 changes: 11 additions & 17 deletions src/RouteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RouteAction
* @param mixed $action
* @param Rareloop/Router/Invoker $invoker
*/
public function __construct($action, Invoker $invoker = null)
public function __construct($action, ?Invoker $invoker = null)
{
$this->invoker = $invoker;
$this->callable = $this->createCallableFromAction($action);
Expand Down Expand Up @@ -65,7 +65,7 @@ public function invoke(ServerRequestInterface $request, RouteParams $params)
* @param mixed $action
* @return callable
*/
private function createCallableFromAction($action) : callable
private function createCallableFromAction($action): callable
{
// Check if this looks like it could be a class/method string
if (!is_callable($action) && is_string($action)) {
Expand All @@ -80,7 +80,7 @@ private function createCallableFromAction($action) : callable
*
* @return boolean
*/
private function isControllerAction() : bool
private function isControllerAction(): bool
{
return !empty($this->controllerName) && !empty($this->controllerMethod);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ private function createControllerFromClassName($className)
*
* @return bool
*/
private function providesMiddleware() : bool
private function providesMiddleware(): bool
{
$controller = $this->getController();

Expand All @@ -143,23 +143,19 @@ private function providesMiddleware() : bool
*
* @return array
*/
public function getMiddleware() : array
public function getMiddleware(): array
{
if (!$this->providesMiddleware()) {
return [];
}

$allControllerMiddleware = array_filter(
$this->getController()->getControllerMiddleware(),
function (ControllerMiddleware $middleware) {
return !$middleware->excludedForMethod($this->controllerMethod);
}
fn(ControllerMiddleware $middleware) => !$middleware->excludedForMethod($this->controllerMethod)
);

return array_map(
function ($controllerMiddleware) {
return $controllerMiddleware->middleware();
},
fn($controllerMiddleware) => $controllerMiddleware->middleware(),
$allControllerMiddleware
);
}
Expand All @@ -170,12 +166,12 @@ function ($controllerMiddleware) {
* @param string $string e.g. `MyController@myMethod`
* @return Closure
*/
private function convertClassStringToFactory($string) : Closure
private function convertClassStringToFactory($string): Closure
{
$this->controllerName = null;
$this->controllerMethod = null;

@list($className, $method) = explode('@', $string);
@[$className, $method] = explode('@', $string);

if (!isset($className) || !isset($method)) {
throw new RouteClassStringParseException('Could not parse route controller from string: `' . $string . '`');
Expand Down Expand Up @@ -204,9 +200,7 @@ private function convertClassStringToFactory($string) : Closure
return [$controller, $method];
}

return function ($params = null) use ($controller, $method) {
return $controller->$method($params);
};
return fn($params = null) => $controller->$method($params);
};
}

Expand All @@ -224,7 +218,7 @@ public function getActionName()
}

if (is_callable($this->callable, false, $callableName)) {
list($controller, $method) = explode('::', $callableName);
[$controller, $method] = explode('::', $callableName);

if ($controller === 'Closure') {
return $controller;
Expand Down
5 changes: 1 addition & 4 deletions src/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
class RouteGroup implements Routable
{
use VerbShortcutsTrait, Macroable;

protected $router;
protected $prefix;
protected $middleware = [];

public function __construct($params, $router)
public function __construct($params, protected $router)
{
$prefix = null;
$middleware = [];
Expand All @@ -35,7 +33,6 @@ public function __construct($params, $router)
}

$this->prefix = is_string($prefix) ? trim($prefix, ' /') : null;
$this->router = $router;
}

private function appendPrefixToUri(string $uri)
Expand Down
4 changes: 1 addition & 3 deletions src/RouteParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
class RouteParams implements \Iterator
{
private $position = 0;
private $params = [];

public function __construct(array $params)
public function __construct(private array $params)
{
$this->params = $params;
}

public function __get($key)
Expand Down
Loading
Loading