Skip to content

Commit

Permalink
update commit
Browse files Browse the repository at this point in the history
- improve code quality for performance
- addef more support for group routing
- fixed issues with array not able to convert into callables
- fixed to support Nette Framework and other php project without biurad/biurad-http compulsory required.
  • Loading branch information
divineniiquaye committed May 1, 2020
1 parent 156c2cf commit 7853e02
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 285 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
],
"require": {
"php": "^7.1.3",
"ext-libxml": "*",
"biurad/biurad-helpers": "^0.1",
"fig/http-message-util": "^1.1",
"psr/http-factory": "^1.0",
Expand Down
49 changes: 21 additions & 28 deletions src/Concerns/CallableResolver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpComposerExtensionStubsInspection */

declare(strict_types=1);

Expand All @@ -19,6 +19,9 @@

namespace Flight\Routing\Concerns;

use Closure;
use RuntimeException;
use stdClass;
use TypeError;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -27,15 +30,12 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

use function is_countable;
use function is_object;
use function method_exists;
use function is_callable;
use function is_string;
use function get_class;
use function class_exists;
use function preg_match;
use function strpos;
use function json_encode;
use function stripos;

Expand All @@ -47,7 +47,7 @@
*/
class CallableResolver implements CallableResolverInterface
{
const CALLABLE_PATTERN = '!^([^\:]+)(:|::|@)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';
public const CALLABLE_PATTERN = '!^([^\:]+)(:|::|@)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';

/**
* @var ContainerInterface|null
Expand All @@ -57,7 +57,7 @@ class CallableResolver implements CallableResolverInterface
/**
* @var object|null
*/
protected $instance = null;
protected $instance;

/**
* @param ContainerInterface|null $container
Expand All @@ -84,32 +84,24 @@ public function resolve($toResolve): callable
{
$resolved = $toResolve;

if (
is_object($toResolve) && method_exists($toResolve, '__invoke') &&
!$toResolve instanceof \Closure
) {
$resolved = $this->resolveCallable($toResolve);
if (is_string($resolved) && preg_match(self::CALLABLE_PATTERN, $toResolve, $matches)) {
// check for slim callable as "class:method", "class::method" and "class@method"
$resolved = $this->resolveCallable($matches[1], $matches[3]);
}

if (is_string($toResolve) && (!is_callable($toResolve) || false !== strpos($toResolve, '::'))) {
// check for slim callable as "class:method", "class::method" and "class@method"
if (preg_match(self::CALLABLE_PATTERN, $toResolve, $matches)) {
$resolved = $this->resolveCallable($matches[1], $matches[3]);
}
if (is_array($resolved) && !is_callable($resolved) && is_string($resolved[0])) {
$resolved = $this->resolveCallable($resolved[0], $resolved[1]);
}

if (
is_array($resolved) && !$resolved instanceof \Closure &&
is_countable($resolved) && is_string($toResolve[0])
) {
if (is_array($resolved) && !$resolved instanceof Closure && is_string($toResolve[0])) {
$resolved = $this->resolveCallable($resolved[0], $resolved[1]);
}

// Checks if indeed what wwe want to return is a callable.
$resolved = $this->assertCallable($resolved);

// Bind new Instance or $this->container to \Closure
if ($resolved instanceof \Closure) {
if ($resolved instanceof Closure) {
if (null !== $binded = $this->instance) {
$resolved = $resolved->bindTo($binded);
}
Expand All @@ -134,7 +126,7 @@ public function returnType($controllerResponse, ResponseInterface $response): Re

if (is_string($controllerResponse) || is_numeric($controllerResponse)) {
$response->getBody()->write((string) $controllerResponse);
} elseif (is_array($controllerResponse) || $controllerResponse instanceof \stdClass) {
} elseif (is_array($controllerResponse) || $controllerResponse instanceof stdClass) {
$response->getBody()->write(json_encode((array) $controllerResponse));
}

Expand Down Expand Up @@ -172,10 +164,10 @@ protected function resolveCallable($class, $method = '__invoke'): callable
{
$instance = $class;

if ($this->container instanceof ContainerInterface && is_string($class)) {
if ($this->container instanceof ContainerInterface && is_string($instance)) {
$instance = $this->container->get($class);
} else {
if (is_string($class) && !class_exists($class)) {
if (!is_object($class) && !class_exists($class)) {
throw new InvalidControllerException(sprintf('Callable %s does not exist', $class));
}

Expand All @@ -198,12 +190,13 @@ protected function resolveCallable($class, $method = '__invoke'): callable
/**
* @param Callable $callable
*
* @throws \RuntimeException if the callable is not resolvable
* @return Callable
* @throws RuntimeException if the callable is not resolvable
*/
protected function assertCallable($callable)
protected function assertCallable($callable): callable
{
// Maybe could be a class object or RequestHandlerInterface instance
if (!$callable instanceof \Closure && is_object($callable) || is_string($callable)) {
if ((!$callable instanceof Closure && is_object($callable)) || is_string($callable)) {
$callable = $this->resolveCallable($callable);
}

Expand All @@ -225,7 +218,7 @@ private function isJson(StreamInterface $stream): bool
}
$stream->rewind();

json_decode($stream->getContents());
json_decode($stream->getContents(), true);

return JSON_ERROR_NONE === json_last_error();
}
Expand Down
15 changes: 7 additions & 8 deletions src/Concerns/RouteValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use function strlen;
use function substr;

use const PHP_VERSION_ID;

trait RouteValidation
{
/**
Expand All @@ -41,10 +39,10 @@ trait RouteValidation
protected function compareMethod($routeMethod, string $requestMethod): bool
{
if (is_array($routeMethod)) {
return in_array($requestMethod, $routeMethod);
return in_array($requestMethod, $routeMethod, true);
}

return $routeMethod == $requestMethod;
return $routeMethod === $requestMethod;
}

/**
Expand All @@ -58,7 +56,7 @@ protected function compareMethod($routeMethod, string $requestMethod): bool
*/
protected function compareDomain(?string $routeDomain, string $requestDomain, array &$parameters): bool
{
return ($routeDomain == null || empty($routeDomain)) || preg_match($routeDomain, $requestDomain, $parameters);
return ($routeDomain === null || empty($routeDomain)) || preg_match($routeDomain, $requestDomain, $parameters);
}

/**
Expand All @@ -68,7 +66,7 @@ protected function compareDomain(?string $routeDomain, string $requestDomain, ar
* @param string $requestUri
* @param array $parameters
*
* @return bool
* @return bool|int
*/
protected function compareUri(string $routeUri, string $requestUri, array &$parameters)
{
Expand All @@ -86,7 +84,6 @@ protected function compareUri(string $routeUri, string $requestUri, array &$para
*/
protected function compareRedirection(string $routeUri, string $requestUri): ?string
{

// Resolve Request Uri.
$newRequestUri = '/' === $requestUri ? '/' : rtrim($requestUri, '/');
$newRouteUri = '/' === $routeUri ? $routeUri : rtrim($routeUri, '/');
Expand All @@ -98,7 +95,9 @@ protected function compareRedirection(string $routeUri, string $requestUri): ?st

if (!empty($paths['route']) && $paths['route'] !== $paths['path']) {
return $newRequestUri . $paths['route'];
} elseif (empty($paths['route']) && $paths['route'] !== $paths['path']) {
}

if (empty($paths['route']) && $paths['route'] !== $paths['path']) {
return $newRequestUri;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/InvalidControllerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

namespace Flight\Routing\Exceptions;

use DomainException;
use Flight\Routing\Interfaces\ExceptionInterface;

/**
* Class InvalidControllerException
*/
class InvalidControllerException extends \DomainException implements ExceptionInterface
class InvalidControllerException extends DomainException implements ExceptionInterface
{
//
}
20 changes: 18 additions & 2 deletions src/Exceptions/InvalidMiddlewareException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,28 @@

namespace Flight\Routing\Exceptions;

use DomainException;
use Flight\Routing\Interfaces\ExceptionInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Class InvalidMiddlewareException
*/
class InvalidMiddlewareException extends \DomainException implements ExceptionInterface
class InvalidMiddlewareException extends DomainException implements ExceptionInterface
{
//
/**
* @param mixed $middleware The middleware that does not fulfill the
* expectations of MiddlewarePipe::pipe
*/
public static function forMiddleware($middleware) : self
{
return new self(sprintf(
'Middleware "%s" is neither a string service name, a PHP callable,'
. ' a %s instance, a %s instance, or an array of such arguments',
is_object($middleware) ? get_class($middleware) : gettype($middleware),
MiddlewareInterface::class,
RequestHandlerInterface::class
));
}
}
27 changes: 27 additions & 0 deletions src/Exceptions/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This code is under BSD 3-Clause "New" or "Revised" License.
*
* PHP version 7 and above required
*
* @category RoutingManager
*
* @author Divine Niiquaye Ibok <[email protected]>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* @link https://www.biurad.com/projects/routingmanager
* @since Version 0.1
*/

namespace Flight\Routing\Exceptions;

/**
* HTTP 405 exception.
*/
class MethodNotAllowedException extends RouteNotFoundException
{
}
5 changes: 2 additions & 3 deletions src/Exceptions/RouteNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@

namespace Flight\Routing\Exceptions;

use BiuradPHP\Http\Exceptions\ClientExceptions\NotFoundException;
use Flight\Routing\Interfaces\ExceptionInterface;
use DomainException;

/**
* Class RouteNotFoundException
*/
class RouteNotFoundException extends NotFoundException implements ExceptionInterface
class RouteNotFoundException extends DomainException
{
}
3 changes: 3 additions & 0 deletions src/Interfaces/CallableResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface CallableResolverInterface
* This instance added will be binded to CLosure
*
* @param object $instance
* @return CallableResolverInterface
*/
public function addInstanceToClosure(object $instance): CallableResolverInterface;

Expand All @@ -53,6 +54,8 @@ public function resolve($toResolve): callable;
*
* @param string|int|array|ResponseInterface $controllerResponse
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public function returnType($controllerResponse, ResponseInterface $response): ResponseInterface;
}
19 changes: 10 additions & 9 deletions src/Interfaces/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Flight\Routing\Interfaces;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
Expand All @@ -29,7 +30,7 @@ interface ResourceController
/**
* Display a listing of the resource.
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function index();

Expand All @@ -39,23 +40,23 @@ public function index();
* @param Request $request
* @param int|mixed $id
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function show(Request $request, $id);

/**
* Store a newly created resource in storage.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param Request $request
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function store(Request $request);

/**
* Show the form for creating a new resource.
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function create();

Expand All @@ -65,7 +66,7 @@ public function create();
* @param Request $request
* @param int|mixed $id
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function edit(Request $request, $id);

Expand All @@ -75,17 +76,17 @@ public function edit(Request $request, $id);
* @param Request $request
* @param int|mixed $id
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function update(Request $request, $id);

/**
* Remove the specified resource from storage.
*
* @param Request $request
* @param id|mixed $id
* @param string|mixed $id
*
* @return \Psr\Http\Message\ResponseInterface|string
* @return ResponseInterface|string
*/
public function destroy(Request $request, $id);

Expand Down
Loading

0 comments on commit 7853e02

Please sign in to comment.