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

x-router* support, refs #55 #65

Merged
merged 2 commits into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Document/OperationObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public function hasParameters()
}

/**
* @return object
* @return array
*/
public function getParameters()
{
return $this->definition->parameters;
return $this->hasParameters() ? $this->definition->parameters : [];
}

/**
Expand Down
171 changes: 117 additions & 54 deletions src/Routing/SwaggerRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace KleijnWeb\SwaggerBundle\Routing;

use KleijnWeb\SwaggerBundle\Document\DocumentRepository;
use KleijnWeb\SwaggerBundle\Document\OperationObject;
use KleijnWeb\SwaggerBundle\Document\SwaggerDocument;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -36,6 +38,19 @@ public function __construct(DocumentRepository $documentRepository)
$this->documentRepository = $documentRepository;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param mixed $resource
* @param string $type
*
* @return bool
*/
public function supports($resource, $type = null)
{
return 'swagger' === $type;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
Expand All @@ -55,64 +70,43 @@ public function load($resource, $type = null)

$routes = new RouteCollection();

foreach ($document->getPathDefinitions() as $path => $methods) {
$paths = $document->getPathDefinitions();
$router = 'swagger.controller';
foreach ($paths as $path => $pathSpec) {
if ($path === 'x-router') {
$router = $pathSpec;
unset($paths->$path);
}
}
foreach ($paths as $path => $methods) {
$relativePath = ltrim($path, '/');
$resourceName = strpos($relativePath, '/')
? substr($relativePath, 0, strpos($relativePath, '/'))
: $relativePath;
$routerController = null;
foreach ($methods as $methodName => $operationSpec) {
$operationName = $methodName;
$controllerKey = "swagger.controller.$resourceName:$operationName";
if (isset($operationSpec->operationId)) {
$operationName = $operationSpec->operationId;
if (false !== strpos($operationSpec->operationId, ':')) {
$controllerKey = $operationSpec->operationId;
} else {
$controllerKey = "swagger.controller.$resourceName:$operationName";
}
if ($methodName === 'x-router-controller') {
$routerController = $operationSpec;
unset($methods->$methodName);
}

}
foreach ($methods as $methodName => $operationSpec) {
$controllerKey = $this->resolveControllerKey(
$operationSpec,
$methodName,
$resourceName,
$router,
$routerController
);
$defaults = [
'_controller' => $controllerKey,
'_definition' => $resource,
'_swagger_path' => $path
];


$requirements = [];
$operationDefinition = $document->getOperationDefinition($path, $methodName);

if (isset($operationDefinition->parameters)) {
foreach ($operationDefinition->parameters as $paramDefinition) {
if ($paramDefinition->in === 'path' && isset($paramDefinition->type)) {
switch ($paramDefinition->type) {
case 'integer':
$requirements[$paramDefinition->name] = '\d+';
break;
case 'string':
if (isset($paramDefinition->pattern)) {
$requirements[$paramDefinition->name] = $paramDefinition->pattern;
break;
}
if (isset($paramDefinition->enum)) {
$requirements[$paramDefinition->name] = '(' .
implode('|', $paramDefinition->enum)
. ')';
break;
}
break;
default:
//NOOP
}
}
}
}

$route = new Route($path, $defaults, $requirements);
$route = new Route($path, $defaults, $this->resolveRequirements($document, $path, $methodName));
$route->setMethods($methodName);
$fileName = pathinfo($resource, PATHINFO_FILENAME);
$routeName = "swagger.{$fileName}.{$this->createRouteIdFromPath($path)}.$operationName";
$routes->add($routeName, $route);
$routes->add($this->createRouteId($resource, $path, $controllerKey), $route);
}
}

Expand All @@ -122,25 +116,94 @@ public function load($resource, $type = null)
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param mixed $resource
* @param string $type
* @param SwaggerDocument $document
* @param $path
* @param $methodName
*
* @return bool
* @return array
*/
public function supports($resource, $type = null)
private function resolveRequirements(SwaggerDocument $document, $path, $methodName)
{
return 'swagger' === $type;
$operationObject = $document->getOperationObject($path, $methodName);

$requirements = [];

foreach ($operationObject->getParameters() as $paramDefinition) {
if ($paramDefinition->in === 'path' && isset($paramDefinition->type)) {
switch ($paramDefinition->type) {
case 'integer':
$requirements[$paramDefinition->name] = '\d+';
break;
case 'string':
if (isset($paramDefinition->pattern)) {
$requirements[$paramDefinition->name] = $paramDefinition->pattern;
break;
}
if (isset($paramDefinition->enum)) {
$requirements[$paramDefinition->name] = '(' .
implode('|', $paramDefinition->enum)
. ')';
break;
}
break;
default:
//NOOP
}
}
}

return $requirements;
}

/**
* @param $operationSpec
* @param $methodName
* @param $resourceName
* @param string $router
* @param null $routerController
*
* @return string
*/
private function resolveControllerKey(
$operationSpec,
$methodName,
$resourceName,
$router,
$routerController = null
) {
$operationName = $methodName;
$diKey = "$router.$resourceName";
if (isset($operationSpec->operationId)) {
if (false !== strpos($operationSpec->operationId, ':')) {
return $operationSpec->operationId;
}
$operationName = $operationSpec->operationId;
}

if (property_exists($operationSpec, 'x-router-controller')) {
$diKey = $operationSpec->{'x-router-controller'};
} elseif ($routerController) {
$diKey = $routerController;
}

return "$diKey:$operationName";
}

/**
* @param string $resource
* @param string $path
*
* @param string $controllerKey
*
* @return string
*/
private function createRouteIdFromPath($path)
private function createRouteId($resource, $path, $controllerKey)
{
return strtolower(trim(preg_replace('/\W+/', '.', $path), '.'));
list(, $operationName) = explode(':', $controllerKey);
$fileName = pathinfo($resource, PATHINFO_FILENAME);
$normalizedPath = strtolower(trim(preg_replace('/\W+/', '.', $path), '.'));
$routeName = "swagger.{$fileName}.$normalizedPath.$operationName";

return $routeName;
}
}
Loading