Skip to content
Permalink
Browse files

//

  • Loading branch information
rodolfoberrios committed Mar 24, 2020
1 parent b83a094 commit e7c5329c0a891fb3ab3493e9c98949b3fc917bf8
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Router\Interfaces;

use Chevere\Components\Cache\Interfaces\CacheInterface;
use Chevere\Components\Router\ResolverCache;
use Chevere\Components\Router\RouteResolve;

interface ResolverCacheInterface
{
public function __construct(CacheInterface $cache);

public function has(int $id): bool;

public function get(int $id): RouteResolve;

public function put(int $id, RouteResolve $routeResolve): void;

public function remove(int $id): void;

public function puts(): array;
}
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Router\Interfaces;

use Chevere\Components\Router\Exceptions\RouteNotFoundException;
use Chevere\Components\Router\Exceptions\RouterException;
use Psr\Http\Message\UriInterface;

interface ResolverInterface
{
/**
* Returns a RoutedInterface for the given UriInterface.
*
* @throws RouterException if the router encounters any fatal error (UnserializeException, TypeError, etc)
* @throws RouteNotFoundException if no route resolves the given UriInterface
*/
public function resolve(UriInterface $uri): RoutedInterface;
}
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Router\Interfaces;

use Chevere\Components\Route\Interfaces\RouteWildcardsInterface;

interface RouteResolveInterface
{
public function name(): string;

public function routeWildcards(): RouteWildcardsInterface;
}
@@ -17,7 +17,7 @@ interface RoutedInterface
{
public function __construct(string $routeName, array $wildcards);

public function routeName(): string;
public function name(): string;

/**
* @return array [wildcardName => matchedValue]
@@ -29,9 +29,9 @@ public function getRegex(): RouterRegexInterface;

public function getIndex(): RouterIndexInterface;

public function put(RouterInterface $router): RouterCacheInterface;
public function put(RouterInterface $router): void;

public function remove(): RouterCacheInterface;
public function remove(): void;

public function puts(): array;
}

This file was deleted.

@@ -21,9 +21,9 @@ public function has(string $routeName): bool;

public function get(string $routeName): RouteInterface;

public function put(RouteInterface $route): RoutesCacheInterface;
public function put(RouteInterface $route): void;

public function remove(string $routeName): RoutesCacheInterface;
public function remove(string $routeName): void;

public function puts(): array;
}
@@ -13,36 +13,36 @@

namespace Chevere\Components\Router;

use Chevere\Components\App\Interfaces\ResolverInterface;
use Chevere\Components\Cache\CacheKey;
use Chevere\Components\Message\Message;
use Chevere\Components\Router\Exceptions\RouteNotFoundException;
use Chevere\Components\Router\Exceptions\RouterException;
use Chevere\Components\Router\Interfaces\ResolverCacheInterface;
use Chevere\Components\Router\Interfaces\RoutedInterface;
use Chevere\Components\Router\Interfaces\RouterRegexInterface;
use OutOfBoundsException;
use Psr\Http\Message\UriInterface;
use Throwable;

final class Resolver
final class Resolver implements ResolverInterface
{
private RouterRegexInterface $routerRegex;

private ResolveCache $resolveCache;
private ResolverCacheInterface $resolverCache;

public function __construct(
RouterRegexInterface $routerRegex,
ResolveCache $resolveCache
ResolverCacheInterface $resolveCache
) {
$this->routerRegex = $routerRegex;
$this->resolveCache = $resolveCache;
$this->resolverCache = $resolveCache;
}

/**
* Returns a RoutedInterface for the given UriInterface.
*
* @throws RouterException if the router encounters any fatal error
* @throws UnserializeException if the route string object can't be unserialized
* @throws TypeError if the found route doesn't implement the RouteInterface
* @throws RouterException if the router encounters any fatal error (UnserializeException, TypeError, etc)
* @throws RouteNotFoundException if no route resolves the given UriInterface
*/
public function resolve(UriInterface $uri): RoutedInterface
@@ -55,8 +55,8 @@ public function resolve(UriInterface $uri): RoutedInterface
throw new RouterException($e->getMessage(), $e->getCode(), $e);
}
throw new RouteNotFoundException(
(new Message('No route found for %path%'))
->code('%path%', $uri->getPath())
(new Message('No route found for %uriPath%'))
->code('%uriPath%', $uri->getPath())
->toString()
);
}
@@ -70,7 +70,7 @@ private function resolver(array $matches): RoutedInterface
$id = $matches['MARK'];
unset($matches['MARK']);
array_shift($matches);
if (!$this->resolveCache->has($id)) {
if (!$this->resolverCache->has($id)) {
throw new OutOfBoundsException(
(new Message('No cache for regex tag id %id%'))
->code('%id%', (string) $id)
@@ -90,6 +90,6 @@ private function resolver(array $matches): RoutedInterface

private function getRouteResolve(int $id): RouteResolve
{
return $this->resolveCache->get($id);
return $this->resolverCache->get($id);
}
}
@@ -18,12 +18,13 @@
use Chevere\Components\Message\Message;
use Chevere\Components\Router\Exceptions\RouteCacheNotFoundException;
use Chevere\Components\Router\Exceptions\RouteCacheTypeException;
use Chevere\Components\Router\Interfaces\ResolverCacheInterface;
use Chevere\Components\Router\Interfaces\RouteableInterface;
use Chevere\Components\Type\Type;
use Chevere\Components\Variable\VariableExport;
use Throwable;

final class ResolveCache
final class ResolverCache implements ResolverCacheInterface
{
private CacheInterface $cache;

@@ -62,25 +63,21 @@ public function get(int $id): RouteResolve
return $item->var();
}

public function put(int $id, RouteResolve $routeResolve): ResolveCache
public function put(int $id, RouteResolve $routeResolve): void
{
$this->cache = $this->cache
->withPut(
new CacheKey((string) $id),
new VariableExport($routeResolve)
);

return $this;
}

public function remove(int $id): ResolveCache
public function remove(int $id): void
{
$this->cache = $this->cache
->withRemove(
new CacheKey((string) $id)
);

return $this;
}

public function puts(): array
@@ -27,7 +27,7 @@ public function __construct(string $name, array $arguments)
$this->arguments = $arguments;
}

public function routeName(): string
public function name(): string
{
return $this->name;
}
@@ -34,7 +34,7 @@ final class RouterCache implements RouterCacheInterface

private RoutesCacheInterface $routesCache;

private ResolveCache $resolveCache;
private ResolverCache $resolverCache;

private CacheKeyInterface $keyRegex;

@@ -44,7 +44,7 @@ public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
$this->routesCache = new RoutesCache($this->cache->getChild('routes/'));
$this->resolveCache = new ResolveCache($this->cache->getChild('resolve/'));
$this->resolverCache = new ResolverCache($this->cache->getChild('resolve/'));
$this->keyRegex = new CacheKey(self::KEY_REGEX);
$this->keyIndex = new CacheKey(self::KEY_INDEX);
}
@@ -54,9 +54,9 @@ public function routesCache(): RoutesCacheInterface
return $this->routesCache;
}

public function resolveCache(): ResolveCache
public function resolverCache(): ResolverCache
{
return $this->resolveCache;
return $this->resolverCache;
}

public function hasRegex(): bool
@@ -89,7 +89,7 @@ public function getIndex(): RouterIndexInterface
return $item->var();
}

public function put(RouterInterface $router): RouterCacheInterface
public function put(RouterInterface $router): void
{
$this->cache = $this->cache
->withPut(
@@ -108,28 +108,24 @@ public function put(RouterInterface $router): RouterCacheInterface
$route = $routeable->route();
$pos++;
$this->routesCache->put($route);
$this->resolveCache->put(
$this->resolverCache->put(
$pos,
new RouteResolve(
$route->name()->toString(),
$route->path()->routeWildcards()
)
);
}

return $this;
}

public function remove(): RouterCacheInterface
public function remove(): void
{
$this->cache = $this->cache
->withRemove($this->keyRegex)
->withRemove($this->keyIndex);
foreach (array_keys($this->routesCache->puts()) as $routeName) {
$this->routesCache->remove($routeName);
}

return $this;
}

public function puts(): array

0 comments on commit e7c5329

Please sign in to comment.
You can’t perform that action at this time.