Skip to content

Commit

Permalink
improved quality of codes for performance
Browse files Browse the repository at this point in the history
This reverts commit 8a23ac2.
  • Loading branch information
divineniiquaye committed Apr 29, 2020
1 parent 8a23ac2 commit 156c2cf
Show file tree
Hide file tree
Showing 25 changed files with 450 additions and 353 deletions.
50 changes: 28 additions & 22 deletions src/Concerns/CallableResolver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php /** @noinspection PhpComposerExtensionStubsInspection */
<?php

declare(strict_types=1);

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

namespace Flight\Routing\Concerns;

use Closure;
use RuntimeException;
use stdClass;
use TypeError;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -30,13 +27,15 @@
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 @@ -48,7 +47,7 @@
*/
class CallableResolver implements CallableResolverInterface
{
public const CALLABLE_PATTERN = '!^([^\:]+)(:|::|@)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';
const CALLABLE_PATTERN = '!^([^\:]+)(:|::|@)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';

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

/**
* @param ContainerInterface|null $container
Expand All @@ -85,24 +84,32 @@ public function resolve($toResolve): callable
{
$resolved = $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_object($toResolve) && method_exists($toResolve, '__invoke') &&
!$toResolve instanceof \Closure
) {
$resolved = $this->resolveCallable($toResolve);
}

if (is_array($resolved) && !is_callable($resolved) && is_string($resolved[0])) {
$resolved = $this->resolveCallable($resolved[0], $resolved[1]);
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 (!$resolved instanceof Closure && method_exists($resolved, '__invoke')) {
$resolved = $this->resolveCallable($toResolve);
if (
is_array($resolved) && !$resolved instanceof \Closure &&
is_countable($resolved) && 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 @@ -127,7 +134,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 @@ -165,10 +172,10 @@ protected function resolveCallable($class, $method = '__invoke'): callable
{
$instance = $class;

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

Expand All @@ -191,13 +198,12 @@ protected function resolveCallable($class, $method = '__invoke'): callable
/**
* @param Callable $callable
*
* @return Callable
* @throws RuntimeException if the callable is not resolvable
* @throws \RuntimeException if the callable is not resolvable
*/
protected function assertCallable($callable): callable
protected function assertCallable($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 @@ -219,7 +225,7 @@ private function isJson(StreamInterface $stream): bool
}
$stream->rewind();

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

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

use const PHP_VERSION_ID;

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

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

/**
Expand All @@ -56,7 +58,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 @@ -66,7 +68,7 @@ protected function compareDomain(?string $routeDomain, string $requestDomain, ar
* @param string $requestUri
* @param array $parameters
*
* @return bool|int
* @return bool
*/
protected function compareUri(string $routeUri, string $requestUri, array &$parameters)
{
Expand Down Expand Up @@ -96,9 +98,7 @@ protected function compareRedirection(string $routeUri, string $requestUri): ?st

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

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

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

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
{
//
}
3 changes: 1 addition & 2 deletions src/Exceptions/InvalidMiddlewareException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

namespace Flight\Routing\Exceptions;

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

/**
* Class InvalidMiddlewareException
*/
class InvalidMiddlewareException extends DomainException implements ExceptionInterface
class InvalidMiddlewareException extends \DomainException implements ExceptionInterface
{
//
}
27 changes: 0 additions & 27 deletions src/Exceptions/MethodNotAllowedException.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/Exceptions/RouteNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

namespace Flight\Routing\Exceptions;

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

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

Expand All @@ -54,8 +53,6 @@ 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: 9 additions & 10 deletions src/Interfaces/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

namespace Flight\Routing\Interfaces;

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

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

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

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

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

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

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

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

Expand Down
Loading

0 comments on commit 156c2cf

Please sign in to comment.