diff --git a/ruleset.xml b/ruleset.xml index 835fa82..2f9c705 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -2,7 +2,7 @@ - + diff --git a/src/DI/ApiDocuExtension.php b/src/DI/ApiDocuExtension.php index 3bd1fa3..0bc3cef 100644 --- a/src/DI/ApiDocuExtension.php +++ b/src/DI/ApiDocuExtension.php @@ -1,6 +1,4 @@ - - */ - protected $config; - - /** - * @var array> - */ - private $defaults = [ - 'apiDir' => '%wwwDir%/api', - 'httpAuth' => [ - 'user' => null, - 'password' => null, - ], - ]; - + public function getConfigSchema(): Schema + { + return Expect::structure([ + 'apiDir' => Expect::string()->default('%wwwDir%/api'), + 'httpAuth' => Expect::structure([ + 'user' => Expect::string(), + 'password' => Expect::string(), + ]), + ]); + } public function loadConfiguration(): void { $this->config = $this->prepareConfig(); } - public function beforeCompile(): void { $builder = $this->getContainerBuilder(); $config = $this->config; $builder->addDefinition($this->prefix('generator')) - ->setClass(Generator::class) - ->setArguments([$config['apiDir'], $config['httpAuth']]); + ->setType(Generator::class) + ->setArguments([$config->apiDir, (array) $config->httpAuth]); $builder->addDefinition($this->prefix('starter')) - ->setClass(Starter::class) + ->setType(Starter::class) ->setArguments([ $builder->getDefinition($this->prefix('generator')), $builder->getDefinition('router'), ]); } - public function afterCompile(ClassType $class): void { parent::afterCompile($class); @@ -64,19 +60,13 @@ public function afterCompile(ClassType $class): void ); } - - /** - * @return array - */ - protected function prepareConfig(): array + protected function prepareConfig(): stdClass { - $config = $this->validateConfig($this->defaults, $this->config); - - $config['apiDir'] = Helpers::expand( - $config['apiDir'], - $this->getContainerBuilder()->parameters - ); + /** @var stdClass $config */ + $config = $this->getConfig(); + $config->apiDir = Helpers::expand($config->apiDir, $this->getContainerBuilder()->parameters); return $config; } + } diff --git a/src/Generator.php b/src/Generator.php index bc85d89..c954c77 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -1,58 +1,45 @@ - - */ - private $httpAuth; + /** @var array{user: ?string, password: ?string} */ + private array $httpAuth; /** - * @param array $httpAuth + * @param array{user: ?string, password: ?string} $httpAuth */ public function __construct( string $appDir, array $httpAuth, - ITemplateFactory $templateFactory, - Http\Request $httpRequest - ) { + TemplateFactory $templateFactory, + Request $httpRequest + ) + { $this->appDir = $appDir; $this->httpAuth = $httpAuth; $this->templateFactory = $templateFactory; $this->httpRequest = $httpRequest; } - - public function generateAll(IRouter $router): void + public function generateAll(Router $router): void { $sections = $this->splitRoutesIntoSections($router); @@ -71,7 +58,7 @@ public function generateAll(IRouter $router): void foreach ($sections as $sectionName => $routes) { if (is_array($routes)) { foreach ($routes as $fileName => $route) { - $this->generateOne($route, $sections, "$sectionName.$fileName"); + $this->generateOne($route, $sections, $sectionName . '.' . $fileName); } } else { $this->generateOne($routes, $sections, (string) $sectionName); @@ -95,7 +82,7 @@ public function generateTarget(ApiRoute $route, array $parameters): void 'httpRequest' => $this->httpRequest, ]); - if (class_exists('Tracy\Debugger')) { + if (class_exists(Debugger::class)) { Debugger::$productionMode = true; } @@ -139,7 +126,6 @@ public function generateIndex(array $sections): void ); } - public function generateSuccess(): void { /** @var DefaultTemplate $template */ @@ -149,22 +135,17 @@ public function generateSuccess(): void 'apiDir' => $this->appDir, ]); - if (class_exists('Tracy\Debugger')) { + if (class_exists(Debugger::class)) { Debugger::$productionMode = true; } echo (string) $template; } - public function createTemplate(string $which): Template { - /** @var DefaultTemplate|null $template */ $template = $this->templateFactory->createTemplate(); - - if (!$template instanceof Template) { - throw new \UnexpectedValueException; - } + assert($template instanceof DefaultTemplate); $template->addFilter(null, 'Contributte\ApiDocu\TemplateFilters::common'); @@ -172,9 +153,7 @@ public function createTemplate(string $which): Template $template->setParameters(['base_dir' => __DIR__ . '/templates']); - $template->addFilter('routeMaskStyles', function ($mask): string { - return str_replace(['<', '>'], ['<', '>'], $mask); - }); + $template->addFilter('routeMaskStyles', fn ($mask): string => str_replace(['<', '>'], ['<', '>'], $mask)); $template->addFilter('apiDocuResponseCode', function ($code): string { if ($code >= 200 && $code <= 202) { @@ -197,13 +176,13 @@ public function createTemplate(string $which): Template /******************************************************************************** - * INTERNAL * + * INTERNAL * ********************************************************************************/ /** - * @return array + * @return array|string, array|ApiRoute> */ - private function splitRoutesIntoSections(IRouter $router): array + private function splitRoutesIntoSections(Router $router): array { $routes = []; $sections = []; @@ -211,17 +190,14 @@ private function splitRoutesIntoSections(IRouter $router): array if ($router instanceof ApiRoute) { $routes = [$router]; - } elseif ($router instanceof \IteratorAggregate) { + } elseif ($router instanceof RouteList) { $routes = $this->getApiRoutesFromIterator($router); } foreach ($routes as $route) { - if ($route->getSection()) { - if ($sections !== [] && $sections[$route->getSection()] === []) { - $sections[$route->getSection()] = []; - } - - $sections[$route->getSection()][$fileName] = $route; + if ($route->getSection() !== null) { + $sections[$route->getSection()] ??= []; + $sections[$route->getSection()][$fileName] = $route; // @phpstan-ignore-line } else { $sections[$fileName] = $route; } @@ -232,21 +208,20 @@ private function splitRoutesIntoSections(IRouter $router): array return $sections; } - /** * Recursively flatten \IteratorAggregate (probably Nette\Application\Routers\RouteList) - * @return array + * + * @return array */ - private function getApiRoutesFromIterator(\IteratorAggregate $i): array + private function getApiRoutesFromIterator(RouteList $routeList): array { $return = []; + $routers = $routeList->getRouters(); - /** @var RouteList $i */ - $routers = $i->getRouters(); foreach ($routers as $router) { if ($router instanceof ApiRoute) { $return[] = $router; - } elseif ($router instanceof \IteratorAggregate) { + } elseif ($router instanceof RouteList) { $routes = $this->getApiRoutesFromIterator($router); foreach ($routes as $route) { @@ -258,7 +233,6 @@ private function getApiRoutesFromIterator(\IteratorAggregate $i): array return $return; } - private function getHttpAuthSnippet(): string { $user = (string) $this->httpAuth['user']; @@ -277,4 +251,5 @@ private function getHttpAuthSnippet(): string . " die('Invalid authentication');" . '} ?>'; } + } diff --git a/src/Starter.php b/src/Starter.php index ca3a938..f62b1f8 100644 --- a/src/Starter.php +++ b/src/Starter.php @@ -1,12 +1,10 @@ -generator = $generator; $this->router = $router; @@ -42,9 +29,9 @@ public function __construct( } } - /** * Event thatis firex when particular ApiRoute is matched + * * @param array $parameters */ public function routeMatched(ApiRoute $route, array $parameters): void @@ -66,7 +53,6 @@ public function routeMatched(ApiRoute $route, array $parameters): void } } - /** * Find ApiRoutes and add listener to each ApiRoute::onMatch event */ @@ -78,4 +64,5 @@ protected function attachEvents(RouteList $routeList): void } } } + } diff --git a/src/TemplateFilters.php b/src/TemplateFilters.php index 1c7c69f..35b79fe 100644 --- a/src/TemplateFilters.php +++ b/src/TemplateFilters.php @@ -1,21 +1,20 @@ -
(.*?)<\/json>/s', function ($item): string { @@ -45,4 +44,5 @@ public static function description(array $text): string return (string) $text; } + } diff --git a/tests/Cases/DI/ApiDocuExtension.phpt b/tests/Cases/DI/ApiDocuExtension.phpt index 2e3cb03..b483a06 100644 --- a/tests/Cases/DI/ApiDocuExtension.phpt +++ b/tests/Cases/DI/ApiDocuExtension.phpt @@ -5,6 +5,7 @@ use Contributte\ApiDocu\Generator; use Contributte\ApiRouter\DI\ApiRouterExtension; use Contributte\Tester\Environment; use Contributte\Tester\Toolkit; +use Contributte\Tester\Utils\Neonkit; use Nette\Bridges\ApplicationDI\LatteExtension; use Nette\Bridges\HttpDI\HttpExtension; use Nette\DI\Compiler; @@ -29,7 +30,7 @@ Toolkit::test(function (): void { 'debugMode' => false, ], ]); - $compiler->addConfig(\Contributte\Tester\Utils\Neonkit::load(<<<'NEON' + $compiler->addConfig(Neonkit::load(<<<'NEON' services: router: Nette\Routing\SimpleRouter NEON