Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge fe1f3a1 into 8a1974f
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrison Heck committed Mar 6, 2018
2 parents 8a1974f + fe1f3a1 commit 3c837af
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
"description": "Symfony routing using php-api-descriptions",
"autoload": {
"psr-4": {
"KleijnWeb\\PhpApi\\RoutingBundle\\Tests\\": "tests",
"KleijnWeb\\PhpApi\\RoutingBundle\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"KleijnWeb\\PhpApi\\RoutingBundle\\Tests\\": "tests/unit"
}
},
"keywords": [
"openapi",
"swagger"
Expand Down
11 changes: 10 additions & 1 deletion src/Routing/OpenApiRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ private function resolveControllerKey(
if (false !== strpos($operation->getId(), ':')) {
return $operation->getId();
}
if (method_exists($operation->getId(), '__invoke')) {
return $operation->getId();
}
$operationName = $operation->getId();
}

Expand All @@ -195,7 +198,13 @@ private function resolveControllerKey(
*/
private function createRouteId(string $resource, string $path, string $controllerKey): string
{
list(, $operationName) = explode(':', $controllerKey);
$controllerSegments = explode(':', $controllerKey);

$operationName = 'action';
if (count($controllerSegments) === 2) {
list(, $operationName) = $controllerSegments;
}

$fileName = pathinfo($resource, PATHINFO_FILENAME);
$normalizedPath = strtolower(trim(preg_replace('/\W+/', '.', $path), '.'));
$routeName = "$this->typeName.{$fileName}.$normalizedPath.$operationName";
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Routing/OpenApiRouteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace KleijnWeb\PhpApi\RoutingBundle\Tests\Routing;

use An\Inokable\Controller;
use KleijnWeb\PhpApi\Descriptions\Description\Description;
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
use KleijnWeb\PhpApi\Descriptions\Description\Parameter;
Expand Down Expand Up @@ -218,6 +219,31 @@ public function canUseOperationIdAsControllerKey()
$this->assertSame($expected, $actual->getDefault('_controller'));
}

/**
* @test
*/
public function canUseOperationIdAsInvokableControllerKey()
{
$expected = Controller::class;

$this->decriptionMock
->expects($this->any())
->method('getPaths')
->willReturn([
new Path(
'/a',
[new Operation('/a:get', '/a', 'get'), new Operation($expected, '/a', 'post'),]
),
new Path('/b', [new Operation('/b:get', '/b', 'get')]),
]);

$routes = $this->loader->load(self::DOCUMENT_PATH);

$actual = $routes->get('customname.path.a.action');
$this->assertNotNull($actual);
$this->assertSame($expected, $actual->getDefault('_controller'));
}

/**
* @test
*/
Expand Down Expand Up @@ -487,3 +513,15 @@ public function willAddRequirementsForStringEnumParams()
$this->assertSame($expected, $requirements['aString']);
}
}

namespace An\Inokable;

use Symfony\Component\HttpFoundation\Response;

class Controller
{
public function __invoke(): Response
{
return new Response();
}
}

0 comments on commit 3c837af

Please sign in to comment.