Yuriy Belenko e8ac630ca5 [Slim] Encode path to support non-latin characters (#1687)
* [Slim] Add encodePath method

* [Slim] Add tests for encodePath method

* [Slim] Use unescaped path in router

Both variables basePathWithoutHost and path are already urlEncoded in
codegen itself. Builtin html encoding in mustache is redundant. We can
use these raw codegen values with no fear.

* [Slim] Refresh samples
2018-12-31 10:02:45 +08:00

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' => '/%20%27%20%22%20%3Dend%20--%20%5C%5Cr%5C%5Cn%20%5C%5Cn%20%5C%5Cr/v2%20*_/%20%27%20%22%20%3Dend%20--%20%5C%5Cr%5C%5Cn%20%5C%5Cn%20%5C%5Cr',
'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;
}
}