slickframework /
web-stack
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of php-scaffold |
||
| 5 | * |
||
| 6 | * For the full copyright and license information, please view the LICENSE |
||
| 7 | * file that was distributed with this source code. |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace Infrastructure\Http\Routing; |
||
| 11 | |||
| 12 | use Psr\Http\Message\ServerRequestInterface; |
||
| 13 | use Slick\Di\Container; |
||
| 14 | use Slick\Di\Definition\ObjectDefinition; |
||
| 15 | use Slick\WebStack\Infrastructure\Http\Routing\RequestContext; |
||
| 16 | use Slick\WebStack\Infrastructure\Http\Routing\RoutesAttributeClassLoader; |
||
| 17 | use Symfony\Component\Config\FileLocator; |
||
| 18 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
| 19 | use Symfony\Component\Routing\Loader\AttributeDirectoryLoader; |
||
| 20 | use Symfony\Component\Routing\Matcher\UrlMatcherInterface; |
||
| 21 | use Symfony\Component\Routing\Router; |
||
| 22 | use Symfony\Component\Routing\RouterInterface; |
||
| 23 | |||
| 24 | $services = []; |
||
| 25 | |||
| 26 | $cacheDirectory = sys_get_temp_dir() . '/cache/routes'; |
||
| 27 | $path = APP_ROOT . '/src/UserInterface'; |
||
| 28 | $userInterfaceDirectory = $path; |
||
| 29 | if (!is_dir($cacheDirectory)) { |
||
| 30 | mkdir($cacheDirectory, 0777, true); |
||
| 31 | } |
||
| 32 | $services['routingBasePath'] = $_ENV['ROUTING_BASE_PATH'] ?? ''; |
||
| 33 | $services['request.context'] = function (Container $container) { |
||
| 34 | $context = new RequestContext(); |
||
| 35 | $request = $container->get('http.request'); |
||
| 36 | $requestContext = $context->fromPsrRequest($request); |
||
| 37 | $baseUrl = $container->get('routingBasePath'); |
||
| 38 | if (!empty($baseUrl)) { |
||
| 39 | $requestContext->setBaseUrl($baseUrl); |
||
| 40 | } |
||
| 41 | return $requestContext; |
||
| 42 | }; |
||
| 43 | |||
| 44 | $services['routes.attribute.loader'] = function () use ($userInterfaceDirectory) { |
||
| 45 | return new AttributeDirectoryLoader( |
||
| 46 | new FileLocator($userInterfaceDirectory), |
||
| 47 | new RoutesAttributeClassLoader() |
||
| 48 | ); |
||
| 49 | }; |
||
| 50 | |||
| 51 | $services[RouterInterface::class] = '@router'; |
||
| 52 | $services['router'] = function (Container $container) use ($userInterfaceDirectory, $cacheDirectory) { |
||
|
0 ignored issues
–
show
|
|||
| 53 | $router = new Router( |
||
| 54 | loader: $container->get('routes.attribute.loader'), |
||
| 55 | resource: $userInterfaceDirectory, |
||
| 56 | context: $container->get('request.context') |
||
| 57 | ); |
||
| 58 | |||
| 59 | //$router->setOptions(['cache_dir' => $cacheDirectory]); |
||
| 60 | return $router; |
||
| 61 | }; |
||
| 62 | |||
| 63 | $services[UrlMatcherInterface::class] = '@url.matcher'; |
||
| 64 | $services['url.matcher'] = function (Container $container) { |
||
| 65 | /** @var Router $router */ |
||
| 66 | $router = $container->get('router'); |
||
| 67 | return $router->getMatcher(); |
||
| 68 | }; |
||
| 69 | |||
| 70 | $services[UrlGeneratorInterface::class] = '@url.generator'; |
||
| 71 | $services['url.generator'] = function (Container $container) { |
||
| 72 | /** @var Router $router */ |
||
| 73 | $router = $container->get('router'); |
||
| 74 | return $router->getGenerator(); |
||
| 75 | }; |
||
| 76 | |||
| 77 | return $services; |
||
| 78 |
This check looks for imports that have been defined, but are not used in the scope.