Skip to content
Permalink
Browse files

Like a gloooove

  • Loading branch information
rodolfoberrios committed Mar 7, 2020
1 parent db6d0fe commit de21a3848dfd60526ba91d2d04c53f8fb7a06aa8
@@ -83,6 +83,14 @@ return PhpCsFixer\Config::create()
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => [
'const',
'class',
'function',
]
]
])
->setRiskyAllowed(true)
->setFinder($finder)
@@ -15,29 +15,21 @@

use Chevere\Components\Controller\Interfaces\ControllerInterface;
use Chevere\Components\Filesystem\Dir;
use Chevere\Components\Filesystem\Interfaces\Dir\DirInterface;
use Chevere\Components\Filesystem\Path;
use Chevere\Components\Http\Interfaces\MethodInterface;
use Chevere\Components\Message\Message;
use Chevere\Components\Route\Exceptions\EndpointException;
use Chevere\Components\Route\Interfaces\EndpointInterface;
use Chevere\Components\Str\Str;
use ReflectionClass;

abstract class Endpoint implements EndpointInterface
{
/** @var string Absoltue path to the endpoint file */
private string $whereIs;

/** @var string The path, like `/api/articles/{id}` */
private string $path;

/** @var MethodInterface The inherithed method, taken from the file basename */
private MethodInterface $method;

/** @var DirInterface The root dir, used to determine $path */
private DirInterface $root;

abstract public function getController(): ControllerInterface;

final public function __construct()
@@ -55,7 +47,6 @@ abstract class Endpoint implements EndpointInterface
->toString()
);
}
$this->handleSetPath();
$this->method = new $method;
}

@@ -64,31 +55,8 @@ abstract class Endpoint implements EndpointInterface
return $this->whereIs;
}

final public function path(): string
{
return $this->path;
}

final public function method(): MethodInterface
{
return $this->method;
}

final public function withRootDir(DirInterface $root): EndpointInterface
{
$new = clone $this;
$new->root = $root;
$new->handleSetPath();

return $new;
}

private function handleSetPath(): void
{
$this->path = (string) (new Str(dirname($this->whereIs)))
->replaceFirst(
rtrim($this->root->path()->absolute(), '/'),
''
);
}
}
@@ -50,11 +50,6 @@ public function getController(): ControllerInterface;
*/
public function whereIs(): string;

/**
* Provides access to the endpoint path string, like `/api/articles/{id}`
*/
public function path(): string;

/**
* Provides access to the MethodInterface instance.
*/
@@ -0,0 +1,19 @@
<?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\Route\Interfaces;

interface RoutePathIteratorInterface
{
const ROUTE_NAME_BASENAME = 'RouteName.php';
}
@@ -0,0 +1,97 @@
<?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\Route;

use Chevere\Components\Filesystem\File;
use Chevere\Components\Filesystem\Interfaces\Dir\DirInterface;
use Chevere\Components\Filesystem\Path;
use Chevere\Components\Filesystem\PhpFile;
use Chevere\Components\Filesystem\PhpFileReturn;
use Chevere\Components\Route\Interfaces\RoutePathIteratorInterface;
use Chevere\Components\Str\Str;
use RecursiveDirectoryIterator;
use RecursiveFilterIterator;
use RecursiveIteratorIterator;
use SplObjectStorage;

/**
* Iterates over the target dir for files matching RouteName.php
*/
final class RoutePathIterator implements RoutePathIteratorInterface
{
private RecursiveDirectoryIterator $directoryIterator;

private RecursiveIteratorIterator $recursiveIterator;

private SplObjectStorage $objects;

public function __construct(DirInterface $dir)
{
$this->directoryIterator = new RecursiveDirectoryIterator(
$dir->path()->absolute(),
RecursiveDirectoryIterator::SKIP_DOTS
| RecursiveDirectoryIterator::KEY_AS_PATHNAME
);
$this->recursiveIterator = new RecursiveIteratorIterator($this->routeNameFilter());
$this->objects = new SplObjectStorage;
$this->recursiveIterator->rewind();
while ($this->recursiveIterator->valid()) {
$pathName = $this->recursiveIterator->current()->getPathName();
$fileReturn = new PhpFileReturn(
new PhpFile(
new File(
new Path($pathName)
)
)
);
$fileReturn = $fileReturn->withStrict(false);
$routePath = (string) (new Str(dirname($pathName) . '/'))
->replaceFirst(
rtrim($dir->path()->absolute(), '/'),
''
);
$this->objects->attach(
new RoutePath($routePath),
$fileReturn->var()
);

$this->recursiveIterator->next();
}
}

public function recursiveIterator(): RecursiveIteratorIterator
{
return $this->recursiveIterator;
}

public function objects(): SplObjectStorage
{
return $this->objects;
}

private function routeNameFilter(): RecursiveFilterIterator
{
return new class($this->directoryIterator) extends RecursiveFilterIterator
{
public function accept(): bool
{
if ($this->hasChildren()) {
return true;
}

return $this->current()->getFilename() === RoutePathIterator::ROUTE_NAME_BASENAME;
}
};
}
}
@@ -18,8 +18,6 @@
use Chevere\Components\Filesystem\Path;
use Chevere\Components\Http\Methods\GetMethod;
use Chevere\Components\Route\Exceptions\EndpointException;
use Chevere\Components\Route\Interfaces\RouteWildcardsInterface;
use Chevere\Components\Route\RouteWildcard;
use PHPUnit\Framework\TestCase;

final class EndpointTest extends TestCase
@@ -36,26 +34,9 @@ public function testPath(): void
$absolute = $this->resourcesDir->path()->getChild('routes/articles/Get.php')->absolute();
$endpoint = include $absolute;
$this->assertSame($absolute, $endpoint->whereIs());
$this->assertSame('', $endpoint->path());
// $this->assertInstanceOf(RouteWildcardsInterface::class, $endpoint->routeWildcards());
$this->assertInstanceOf(GetMethod::class, $endpoint->method());
}

// public function testPathWildcard(): void
// {
// $endpoint = include $this->resourcesDir->path()->getChild('routes/articles/{id}/Get.php')->absolute();
// $collection = $endpoint->routeWildcards();
// $this->assertTrue($collection->has(new RouteWildcard('id')));
// }

public function testWithRoot(): void
{
$locator = 'routes/articles/{id}';
$endpoint = include $this->resourcesDir->path()->getChild($locator . '/Get.php')->absolute();
$endpoint = $endpoint->withRootDir($this->resourcesDir);
$this->assertSame('/' . $locator, $endpoint->path());
}

public function testWrongFilename(): void
{
$this->expectException(EndpointException::class);
@@ -0,0 +1,62 @@
<?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\Route\Tests;

use Chevere\Components\Filesystem\Dir;
use Chevere\Components\Filesystem\Interfaces\Dir\DirInterface;
use Chevere\Components\Filesystem\Path;
use Chevere\Components\Route\Interfaces\RouteNameInterface;
use Chevere\Components\Route\Interfaces\RoutePathInterface;
use Chevere\Components\Route\Interfaces\RoutePathIteratorInterface;
use Chevere\Components\Route\RoutePathIterator;
use PHPUnit\Framework\TestCase;

final class RoutePathIteratorTest extends TestCase
{
public function testRecursiveIterator(): void
{
$rootDir = new Dir(new Path(__DIR__ . '/_resources/routes/'));
$routePathIterator = new RoutePathIterator($rootDir);
$recursiveIterator = $routePathIterator->recursiveIterator();
$this->assertCount(2, $recursiveIterator);
$recursiveIterator->rewind();
while ($recursiveIterator->valid()) {
$this->assertStringEndsWith(
RoutePathIteratorInterface::ROUTE_NAME_BASENAME,
$recursiveIterator->current()->getPathName()
);
$recursiveIterator->next();
}
}

public function testObjects(): void
{
$rootDir = new Dir(new Path(__DIR__ . '/_resources/routes/'));
$routePathIterator = new RoutePathIterator($rootDir);
$objectStorage = $routePathIterator->objects();
$this->assertCount(2, $objectStorage);
$objectStorage->rewind();
while ($objectStorage->valid()) {
$this->assertInstanceOf(
RoutePathInterface::class,
$objectStorage->current()
);
$this->assertInstanceOf(
RouteNameInterface::class,
$objectStorage->getInfo()
);
$objectStorage->next();
}
}
}

0 comments on commit de21a38

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