|
12 | 12 | use Reconmap\Services\Logging\LoggingConfigurator;
|
13 | 13 | use Reconmap\Services\SearchListener;
|
14 | 14 | use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
|
| 15 | +use Symfony\Component\Config\FileLocator; |
15 | 16 | use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
|
16 | 17 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
| 18 | +use Symfony\Component\HttpFoundation\Request; |
17 | 19 | use Symfony\Component\HttpFoundation\Response;
|
| 20 | +use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
| 21 | +use Symfony\Component\Routing\Loader\AttributeClassLoader; |
| 22 | +use Symfony\Component\Routing\Loader\AttributeDirectoryLoader; |
| 23 | +use Symfony\Component\Routing\RequestContext; |
| 24 | +use Symfony\Component\Routing\Route; |
| 25 | +use Symfony\Component\Routing\Router; |
18 | 26 |
|
| 27 | +// Load the configuration |
19 | 28 | $configFilePath = $applicationDir . '/config.json';
|
20 | 29 | if (!file_exists($configFilePath) || !is_readable($configFilePath)) {
|
21 | 30 | $errorMessage = 'Missing or unreadable API configuration file (config.json)';
|
|
31 | 40 | $loggingConfigurator = new LoggingConfigurator($logger, $config);
|
32 | 41 | $loggingConfigurator->configure();
|
33 | 42 |
|
34 |
| -$file = $config->getAppDir() . '/data/attachments/container.php'; |
| 43 | +$file = $config->getAppDir() . '/data/cache/container.php'; |
35 | 44 | if (file_exists($file)) {
|
36 | 45 | require $file;
|
37 | 46 | $container = new CachedApplicationContainer();
|
|
44 | 53 | }
|
45 | 54 | ApplicationContainer::initialise($container, $config, $logger);
|
46 | 55 |
|
47 |
| -$request = GuzzleHttp\Psr7\ServerRequest::fromGlobals(); |
48 |
| -$container->set(Psr\Http\Message\ServerRequestInterface::class, $request); |
| 56 | +$request = Request::createFromGlobals(); |
| 57 | +$requestContext = new RequestContext(); |
| 58 | +$requestContext->fromRequest($request); |
49 | 59 |
|
50 |
| -$router = new ApiRouter(); |
51 |
| -$router->mapRoutes($container, $config); |
| 60 | +$controllersDir = $applicationDir . '/src/Controllers'; |
52 | 61 |
|
53 |
| -/** |
54 |
| - * @var EventDispatcher $eventDispatcher |
55 |
| - */ |
56 |
| -$eventDispatcher = $container->get(EventDispatcher::class); |
57 |
| -$eventDispatcher->addListener(SearchEvent::class, $container->get(SearchListener::class)); |
| 62 | +$loader = new AttributeDirectoryLoader( |
| 63 | + new FileLocator([$controllersDir]), |
| 64 | + new class extends AttributeClassLoader { |
| 65 | + protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void |
| 66 | + { |
| 67 | + $route->setDefault('_controller', $class->name . '::' . $method->name); |
| 68 | + } |
| 69 | + } |
| 70 | +); |
58 | 71 |
|
59 |
| -$response = $router->dispatch($request); |
| 72 | +$routes = $loader->load($controllersDir); |
| 73 | +$router = new Router( |
| 74 | + $loader, |
| 75 | + $controllersDir, |
| 76 | + ['cache_dir' => $applicationDir . '/data/cache', 'debug' => false], |
| 77 | + $requestContext |
| 78 | +); |
60 | 79 |
|
61 |
| -$httpFoundationFactory = new HttpFoundationFactory(); |
62 |
| -$symfonyResponse = $httpFoundationFactory->createResponse($response); |
| 80 | +try { |
| 81 | + // Try routing the request with Symfony's Router |
| 82 | + $parameters = $router->match($request->getPathInfo()); |
| 83 | + $controller = $parameters['_controller']; |
| 84 | + unset($parameters['_controller'], $parameters['_route']); |
63 | 85 |
|
64 |
| -$symfonyResponse->send(); |
| 86 | + // Call the matched controller |
| 87 | + [$class, $method] = explode('::', $controller, 2); |
| 88 | + $ooo = $container->get($class); |
| 89 | + $response = call_user_func_array([$ooo, $method], $parameters); |
| 90 | +} catch (ResourceNotFoundException $e) { |
| 91 | + // Fall back to the custom API router |
| 92 | + $guzzleRequest = GuzzleHttp\Psr7\ServerRequest::fromGlobals(); |
| 93 | + $container->set(Psr\Http\Message\ServerRequestInterface::class, $guzzleRequest); |
65 | 94 |
|
| 95 | + $apiRouter = new ApiRouter(); |
| 96 | + $apiRouter->mapRoutes($container, $config); |
| 97 | + |
| 98 | + /** |
| 99 | + * @var EventDispatcher $eventDispatcher |
| 100 | + */ |
| 101 | + $eventDispatcher = $container->get(EventDispatcher::class); |
| 102 | + $eventDispatcher->addListener(SearchEvent::class, $container->get(SearchListener::class)); |
| 103 | + |
| 104 | + $apiResponse = $apiRouter->dispatch($guzzleRequest); |
| 105 | + |
| 106 | + $httpFoundationFactory = new HttpFoundationFactory(); |
| 107 | + $response = $httpFoundationFactory->createResponse($apiResponse); |
| 108 | +} |
| 109 | + |
| 110 | +$response->send(); |
0 commit comments