forked from loafle/openapi-generator-original
* [Slim] Add abstract prefix to API controllers * [Slim] Add userClassname property to codegen * [Slim] Add src folder to Composer autoload * [Slim] Change template to produce abstract controllers * [Slim] Update API tests template * [Slim] Add implementation note * [Slim] Remove deprecated AbstractApiController * [Slim] Refresh samples
144 lines
4.5 KiB
PHP
144 lines
4.5 KiB
PHP
<?php
|
|
/**
|
|
* SlimRouter
|
|
*
|
|
* PHP version 5
|
|
*
|
|
* @category Class
|
|
* @package OpenAPIServer
|
|
* @author OpenAPI Generator team
|
|
* @link https://github.com/openapitools/openapi-generator
|
|
*/
|
|
|
|
/**
|
|
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
|
|
*
|
|
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
|
|
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
|
|
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
|
|
* Generated by: https://github.com/openapitools/openapi-generator.git
|
|
*/
|
|
|
|
/**
|
|
* NOTE: This class is auto generated by the openapi generator program.
|
|
* https://github.com/openapitools/openapi-generator
|
|
* Do not edit the class manually.
|
|
*/
|
|
namespace OpenAPIServer;
|
|
|
|
use Slim\App;
|
|
use Psr\Container\ContainerInterface;
|
|
use InvalidArgumentException;
|
|
use Tuupola\Middleware\HttpBasicAuthentication;
|
|
|
|
/**
|
|
* SlimRouter Class Doc Comment
|
|
*
|
|
* PHP version 5
|
|
*
|
|
* @category Class
|
|
* @package OpenAPIServer
|
|
* @author OpenAPI Generator team
|
|
* @link https://github.com/openapitools/openapi-generator
|
|
*/
|
|
class SlimRouter
|
|
{
|
|
|
|
/**
|
|
* @var $slimApp Slim\App instance
|
|
*/
|
|
private $slimApp;
|
|
|
|
/** @var array[] list of all api operations */
|
|
private $operations = [
|
|
[
|
|
'httpMethod' => 'PUT',
|
|
'basePathWithoutHost' => '/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r',
|
|
'path' => '/fake',
|
|
'apiPackage' => 'OpenAPIServer\Api',
|
|
'classname' => 'AbstractFakeApi',
|
|
'userClassname' => 'FakeApi',
|
|
'operationId' => 'testCodeInjectEndRnNR',
|
|
'authMethods' => [
|
|
],
|
|
],
|
|
];
|
|
|
|
/**
|
|
* Class constructor
|
|
*
|
|
* @param ContainerInterface|array $container Either a ContainerInterface or an associative array of app settings
|
|
* @throws InvalidArgumentException when no container is provided that implements ContainerInterface
|
|
*/
|
|
public function __construct($container = [])
|
|
{
|
|
$this->slimApp = new App($container);
|
|
|
|
$basicAuth = new HttpBasicAuthentication([
|
|
"secure" => false,
|
|
"authenticator" => function ($arguments) {
|
|
$user = $arguments["user"];
|
|
$password = $arguments["password"];
|
|
return false;
|
|
}
|
|
]);
|
|
|
|
foreach ($this->operations as $operation) {
|
|
$callback = function ($request, $response, $arguments) use ($operation) {
|
|
$message = "How about extending {$operation['classname']} by {$operation['apiPackage']}\\{$operation['userClassname']} class implementing {$operation['operationId']} as a {$operation['httpMethod']} method?";
|
|
throw new \Exception($message);
|
|
return $response->withStatus(501)->write($message);
|
|
};
|
|
$middlewares = [];
|
|
|
|
if (class_exists("\\{$operation['apiPackage']}\\{$operation['userClassname']}")) {
|
|
$callback = "\\{$operation['apiPackage']}\\{$operation['userClassname']}:{$operation['operationId']}";
|
|
}
|
|
|
|
|
|
foreach ($operation['authMethods'] as $authMethod) {
|
|
if ($authMethod['type'] === 'http') {
|
|
$middlewares[] = $basicAuth;
|
|
}
|
|
}
|
|
|
|
$this->addRoute(
|
|
[$operation['httpMethod']],
|
|
"{$operation['basePathWithoutHost']}{$operation['path']}",
|
|
$callback,
|
|
$middlewares
|
|
)->setName($operation['operationId']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add route with multiple methods
|
|
*
|
|
* @param string[] $methods Numeric array of HTTP method names
|
|
* @param string $pattern The route URI pattern
|
|
* @param callable|string $callable The route callback routine
|
|
* @param array|null $middlewares List of middlewares
|
|
*
|
|
* @return Slim\Interfaces\RouteInterface
|
|
*
|
|
* @throws InvalidArgumentException if the route pattern isn't a string
|
|
*/
|
|
public function addRoute($methods, $pattern, $callable, $middlewares = [])
|
|
{
|
|
$route = $this->slimApp->map($methods, $pattern, $callable);
|
|
foreach ($middlewares as $middleware) {
|
|
$route->add($middleware);
|
|
}
|
|
return $route;
|
|
}
|
|
|
|
/**
|
|
* Returns Slim Framework instance
|
|
* @return App
|
|
*/
|
|
public function getSlimApp()
|
|
{
|
|
return $this->slimApp;
|
|
}
|
|
}
|