Skip to content

Commit

Permalink
DI: replace deprecated nette/reflection with contributte/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 26, 2022
1 parent d2bd9ff commit 8bf9ca0
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 9 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
"require": {
"php": ">= 7.1.0",
"nette/http": "^3.0",
"nette/utils": "^3.0",
"nette/application": "^3.0",
"nette/di": "^3.0",
"nette/reflection": "^2.4",
"contributte/utils": "^0.5.2",
"doctrine/cache": "^1.6",
"doctrine/annotations": "~1.3"
},
Expand Down
12 changes: 5 additions & 7 deletions src/DI/ApiRouterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Contributte\ApiRouter\DI;

use Contributte\ApiRouter\ApiRoute;
use Contributte\Utils\Annotations;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
Expand All @@ -13,8 +14,7 @@
use Nette\DI\ContainerBuilder;
use Nette\DI\Definitions\Definition;
use Nette\PhpGenerator\ClassType as GClassType;
use Nette\Reflection\ClassType;
use Nette\Reflection\Method;
use ReflectionClass;
use ReflectionMethod;

class ApiRouterExtension extends CompilerExtension
Expand Down Expand Up @@ -110,7 +110,7 @@ private function findRoutes(ContainerBuilder $builder): array

private function findRoutesInPresenter(string $presenter, array &$routes): void
{
$r = ClassType::from($presenter);
$r = new ReflectionClass($presenter);

$route = $this->reader->getClassAnnotation($r, ApiRoute::class);

Expand All @@ -125,7 +125,7 @@ private function findRoutesInPresenter(string $presenter, array &$routes): void
$routes[$route->getPriority()] = [];
}

$route->setDescription($r->getAnnotation('description'));
$route->setDescription(Annotations::getAnnotation($r, 'description'));

if (!$route->getPresenter()) {
$route->setPresenter(preg_replace('/Presenter$/', '', $r->getShortName()));
Expand Down Expand Up @@ -170,9 +170,7 @@ private function findPresenterMethodRoute(
return;
}

if ($method_reflection instanceof Method) {
$action_route->setDescription($method_reflection->getAnnotation('description'));
}
$action_route->setDescription(Annotations::getAnnotation($method_reflection, 'description'));

/**
* Action route will inherit presenter name, priority, etc from parent route
Expand Down
60 changes: 60 additions & 0 deletions tests/Cases/ApiRouteExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Tests\Cases;

use Contributte\ApiRouter\DI\ApiRouterExtension;
use Nette\Application\Routers\RouteList;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tester\TestCase;
use Tests\Toolkit\Helpers;
use Tests\Toolkit\Tests;

require __DIR__ . '/../bootstrap.php';

/**
* @testCase
*/
final class ApiRouteExtensionTest extends TestCase
{

/** @var Container */
private $container;

protected function setUp()
{
parent::setUp();

$loader = new ContainerLoader(Tests::TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('api', new ApiRouterExtension());
$compiler->addConfig(Helpers::neon('
services:
router: Tests\Fixtures\DummyRouterFactory::create
- Tests\Fixtures\UsersController
'));
$compiler->addConfig([
'parameters' => [
'debugMode' => false,
'tempDir' => Tests::TEMP_PATH,
],
]);
}, __FILE__ . time());

$this->container = new $class();
$this->container->initialize();
}

public function testRouter(): void
{
/** @var RouteList $router */
$router = $this->container->getByType(RouteList::class);

Assert::count(2, $router->getRouters());
}

}

(new ApiRouteExtensionTest())->run();
15 changes: 15 additions & 0 deletions tests/Fixtures/DummyRouterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures;

use Nette\Application\Routers\RouteList;

final class DummyRouterFactory
{

public static function create(): RouteList
{
return new RouteList();
}

}
50 changes: 50 additions & 0 deletions tests/Fixtures/UsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures;

use Contributte\ApiRouter\ApiRoute;
use Nette\Application\UI\Presenter;

/**
* @ApiRoute(
* "/api/users[/<id>]",
* parameters={
* "id"={
* "requirement": "\d+",
* "default": 10
* }
* }
* )
*/
class UsersController extends Presenter
{

/**
* Get user detail
*
* @ApiRoute(
* "/api/users/<id>[/<foo>-<bar>]",
* parameters={
* "id"={
* "requirement": "\d+"
* }
* },
* method="GET"
* )
*/
public function actionRead($id, $foo = null, $bar = null)
{
$this->sendJson(['id' => $id, 'foo' => $foo, 'bar' => $bar]);
}

public function actionUpdate($id)
{
$this->sendJson(['id' => $id]);
}

public function actionDelete($id)
{
$this->sendJson(['id' => $id]);
}

}
19 changes: 19 additions & 0 deletions tests/Toolkit/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Tests\Toolkit;

use Nette\DI\Config\Adapters\NeonAdapter;
use Nette\Neon\Neon;

final class Helpers
{

/**
* @return mixed[]
*/
public static function neon(string $str): array
{
return (new NeonAdapter())->process((array) Neon::decode($str));
}

}
11 changes: 11 additions & 0 deletions tests/Toolkit/Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace Tests\Toolkit;

final class Tests
{

public const APP_PATH = __DIR__ . '/..';
public const TEMP_PATH = __DIR__ . '/../tmp';

}

0 comments on commit 8bf9ca0

Please sign in to comment.