Skip to content

Commit 854f72a

Browse files
author
liutao
committed
feat: swagger
1 parent a26b687 commit 854f72a

File tree

7 files changed

+141
-10
lines changed

7 files changed

+141
-10
lines changed

src/Application.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ public static function boot(): Application
4040
}
4141

4242
/**
43+
* @param OutputInterface $output
44+
* @return Application
45+
* @throws ContainerExceptionInterface
46+
* @throws NotFoundExceptionInterface
4347
* @throws ReflectionException
4448
*/
4549
public function run(OutputInterface $output): Application
4650
{
4751
$app = new Application();
48-
$config = $app->init();
49-
$app->listen($config, $output);
52+
$app->init();
53+
$app->listen($output);
5054

5155
return $app;
5256
}
@@ -73,21 +77,24 @@ public static function getClass(string $class)
7377
/**
7478
* @throws ReflectionException
7579
*/
76-
private function init(): array
80+
private function init(): void
7781
{
7882
Env::load();
7983
$this->initContainer();
8084
$config = $this->getConfig();
8185
$this->initDatabase($config['database']);
8286
$this->scanAttributes($config['app']['scanner']);
83-
84-
return $config;
87+
$router = $this->getRouter($config['routes']);
88+
Application::getContainer()->add(Router::class, $router);
8589
}
8690

87-
private function listen(array $config, OutputInterface $output): void
91+
/**
92+
* @throws ContainerExceptionInterface
93+
* @throws NotFoundExceptionInterface
94+
*/
95+
private function listen(OutputInterface $output): void
8896
{
89-
$router = $this->getRouter($config['routes']);
90-
Application::getContainer()->add(Router::class, $router);
97+
$router = Application::getContainer()->get(Router::class);
9198
ServerFactory::newServer()->run($router, $output);
9299
}
93100

src/Commands/StartCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ private function roadrunnerStart(OutputInterface $output, ServerConfig $serverCo
7070
}
7171

7272
/**
73+
* @param OutputInterface $output
74+
* @return int
7375
* @throws ContainerExceptionInterface
74-
* @throws ReflectionException
7576
* @throws NotFoundExceptionInterface
77+
* @throws ReflectionException
7678
*/
7779
private function workermanStart(OutputInterface $output): int
7880
{
@@ -82,9 +84,11 @@ private function workermanStart(OutputInterface $output): int
8284
}
8385

8486
/**
87+
* @param OutputInterface $output
88+
* @return int
8589
* @throws ContainerExceptionInterface
86-
* @throws ReflectionException
8790
* @throws NotFoundExceptionInterface
91+
* @throws ReflectionException
8892
*/
8993
private function swooleStart(OutputInterface $output): int
9094
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MicroPHP\Framework\Commands;
5+
6+
use MicroPHP\Framework\Attribute\Attributes\CMD;
7+
use MicroPHP\Swagger\Swagger;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
#[CMD]
13+
class SwaggerGenerateCommand extends Command
14+
{
15+
protected function execute(InputInterface $input, OutputInterface $output): int
16+
{
17+
Swagger::gen();
18+
$output->writeln('<info>swagger was generated</info>');
19+
return Command::SUCCESS;
20+
}
21+
22+
protected function configure(): void
23+
{
24+
$this->setName('swagger:gen')->setDescription('Generate swagger file');
25+
}
26+
}

src/ConfigProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use MicroPHP\Framework\Commands\PublishConfigCommand;
88
use MicroPHP\Framework\Commands\StartCommand;
9+
use MicroPHP\Framework\Commands\SwaggerGenerateCommand;
910
use MicroPHP\Framework\Config\ConfigProviderInterface;
1011

1112
class ConfigProvider implements ConfigProviderInterface
@@ -16,6 +17,7 @@ public function config(): array
1617
'commands' => [
1718
StartCommand::class,
1819
PublishConfigCommand::class,
20+
SwaggerGenerateCommand::class,
1921
],
2022
];
2123
}

src/Router/Route.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MicroPHP\Framework\Router;
5+
6+
class Route extends \League\Route\Route
7+
{
8+
public function getHandler(): array|string|callable|object
9+
{
10+
return $this->handler;
11+
}
12+
}

src/Router/Router.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MicroPHP\Framework\Router;
66

77
use League\Route\RouteGroup;
8+
use League\Route\Strategy\OptionsHandlerInterface;
89

910
class Router extends \League\Route\Router
1011
{
@@ -15,4 +16,51 @@ public function getOrPost(string $path, $handler): RouteGroup
1516
$group->post($path, $handler);
1617
});
1718
}
19+
20+
/**
21+
* @return Route[]
22+
*/
23+
public function getRoutes(): array
24+
{
25+
return $this->routes;
26+
}
27+
28+
public function map(string $method, string $path, $handler): Route
29+
{
30+
$path = sprintf('/%s', ltrim($path, '/'));
31+
$route = new Route($method, $path, $handler);
32+
33+
$this->routes[] = $route;
34+
35+
return $route;
36+
}
37+
38+
protected function buildOptionsRoutes(array $options): void
39+
{
40+
if (!($this->getStrategy() instanceof OptionsHandlerInterface)) {
41+
return;
42+
}
43+
44+
/** @var OptionsHandlerInterface $strategy */
45+
$strategy = $this->getStrategy();
46+
47+
foreach ($options as $identifier => $methods) {
48+
[$scheme, $host, $port, $path] = explode(static::IDENTIFIER_SEPARATOR, $identifier);
49+
$route = new Route('OPTIONS', $path, $strategy->getOptionsCallable($methods));
50+
51+
if (!empty($scheme)) {
52+
$route->setScheme($scheme);
53+
}
54+
55+
if (!empty($host)) {
56+
$route->setHost($host);
57+
}
58+
59+
if (!empty($port)) {
60+
$route->setPort((int)$port);
61+
}
62+
63+
$this->routeCollector->addRoute($route->getMethod(), $this->parseRoutePath($route->getPath()), $route);
64+
}
65+
}
1866
}

src/Swagger/PathGetter.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MicroPHP\Framework\Swagger;
5+
6+
use MicroPHP\Framework\Application;
7+
use MicroPHP\Framework\Router\Router;
8+
use MicroPHP\Swagger\Contract\PathGetterInterface;
9+
use MicroPHP\Swagger\Enum\MethodEnum;
10+
use Psr\Container\ContainerExceptionInterface;
11+
use Psr\Container\NotFoundExceptionInterface;
12+
13+
class PathGetter implements PathGetterInterface
14+
{
15+
/**
16+
* @throws ContainerExceptionInterface
17+
* @throws NotFoundExceptionInterface
18+
*/
19+
public static function getPath(string $controller, string $function, MethodEnum $requestMethod = null):?string {
20+
/** @var Router $route */
21+
$route = Application::getContainer()->get(Router::class);
22+
foreach ($route->getRoutes() as $route) {
23+
$handler = $route->getHandler();
24+
if (is_array($handler)) {
25+
if ($controller === $handler[0] && $function === $handler[1]) {
26+
return $route->getPath();
27+
}
28+
}
29+
}
30+
return null;
31+
}
32+
}

0 commit comments

Comments
 (0)