|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\Api; |
| 15 | + |
| 16 | +use Cake\Console\CommandCollection; |
| 17 | +use Cake\Core\BasePlugin; |
| 18 | +use Cake\Core\Configure; |
| 19 | +use Cake\Core\ContainerInterface; |
| 20 | +use CakeDC\Api\Command\ServiceRoutesCommand; |
| 21 | + |
| 22 | +/** |
| 23 | + * Api plugin |
| 24 | + */ |
| 25 | +class ApiPlugin extends BasePlugin |
| 26 | +{ |
| 27 | + protected array $middlewares = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @inheritDoc |
| 31 | + */ |
| 32 | + public function routes($routes): void |
| 33 | + { |
| 34 | + $middlewares = Configure::read('Api.Middleware', []); |
| 35 | + foreach ($middlewares as $alias => $middleware) { |
| 36 | + $class = $middleware['class']; |
| 37 | + if (array_key_exists('request', $middleware)) { |
| 38 | + $requestClass = $middleware['request']; |
| 39 | + $request = new $requestClass(); |
| 40 | + if (array_key_exists('method', $middleware)) { |
| 41 | + $request = $request->{$middleware['method']}(); |
| 42 | + } |
| 43 | + if (array_key_exists('params', $middleware)) { |
| 44 | + $options = $middleware['params']; |
| 45 | + $this->registerMiddleware($routes, $alias, new $class($request, $options)); |
| 46 | + } else { |
| 47 | + $this->registerMiddleware($routes, $alias, new $class($request)); |
| 48 | + } |
| 49 | + } else { |
| 50 | + if (array_key_exists('params', $middleware)) { |
| 51 | + $options = $middleware['params']; |
| 52 | + $this->registerMiddleware($routes, $alias, new $class($options)); |
| 53 | + } else { |
| 54 | + $this->registerMiddleware($routes, $alias, new $class()); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + parent::routes($routes); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Middleware registrator and holder. |
| 64 | + * |
| 65 | + * @param \Cake\Routing\RouteBuilder $routes Routes. |
| 66 | + * @param string $alias Middleware alias. |
| 67 | + * @param string $class Middleware class instance. |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + protected function registerMiddleware($routes, $alias, $class) |
| 71 | + { |
| 72 | + $routes->registerMiddleware($alias, $class); |
| 73 | + $this->middlewares[$alias] = $class; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Register container services for this plugin. |
| 78 | + * |
| 79 | + * @param \Cake\Core\ContainerInterface $container The container to add services to. |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function services(ContainerInterface $container): void |
| 83 | + { |
| 84 | + if (array_key_exists('apiParser', $this->middlewares)) { |
| 85 | + $this->middlewares['apiParser']->setContainer($container); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Add console commands for the plugin. |
| 91 | + * |
| 92 | + * @param \Cake\Console\CommandCollection $commands The command collection to update |
| 93 | + * @return \Cake\Console\CommandCollection |
| 94 | + */ |
| 95 | + public function console(CommandCollection $commands): CommandCollection |
| 96 | + { |
| 97 | + return $commands->add('service routes', ServiceRoutesCommand::class); |
| 98 | + } |
| 99 | +} |
0 commit comments