|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bref\SymfonyBridge; |
| 4 | + |
| 5 | +use Bref\Runtime\FileHandlerLocator; |
| 6 | +use Bref\SymfonyBridge\Http\KernelAdapter; |
| 7 | +use Bref\SymfonyBridge\Runtime\BrefRuntime; |
| 8 | +use Exception; |
| 9 | +use Psr\Container\ContainerInterface; |
| 10 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 11 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * This class resolves handlers. |
| 15 | + * |
| 16 | + * For example, if we configure `handler: xyz` in serverless.yml, then Bref |
| 17 | + * will call this class to resolve `xyz` into the real Lambda handler. |
| 18 | + */ |
| 19 | +class HandlerResolver implements ContainerInterface |
| 20 | +{ |
| 21 | + private ?ContainerInterface $symfonyContainer; |
| 22 | + private FileHandlerLocator $fileLocator; |
| 23 | + |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + // Bref's default handler resolver |
| 27 | + $this->fileLocator = new FileHandlerLocator; |
| 28 | + $this->symfonyContainer = null; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + public function get($id) |
| 35 | + { |
| 36 | + $isComposed = strpos($id, ':') !== false; |
| 37 | + |
| 38 | + // By default we check if the handler is a file name (classic Bref behavior) |
| 39 | + if (! $isComposed && $this->fileLocator->has($id)) { |
| 40 | + return $this->fileLocator->get($id); |
| 41 | + } |
| 42 | + |
| 43 | + $service = $id; |
| 44 | + |
| 45 | + $bootstrapFile = null; |
| 46 | + if ($isComposed) { |
| 47 | + [$bootstrapFile, $service] = explode(':', $id, 2); |
| 48 | + } |
| 49 | + |
| 50 | + // If not, we try to get the handler from the Symfony container |
| 51 | + $handler = $this->symfonyContainer($bootstrapFile)->get($service); |
| 52 | + |
| 53 | + // If the kernel was configured as a handler, then we wrap it to make it a valid HTTP handler for Lambda |
| 54 | + if ($handler instanceof HttpKernelInterface) { |
| 55 | + $handler = new KernelAdapter($handler); |
| 56 | + } |
| 57 | + |
| 58 | + return $handler; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritDoc} |
| 63 | + */ |
| 64 | + public function has($id): bool |
| 65 | + { |
| 66 | + $isComposed = strpos($id, ':') !== false; |
| 67 | + |
| 68 | + // By default we check if the handler is a file name (classic Bref behavior) |
| 69 | + if (! $isComposed && $this->fileLocator->has($id)) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + $service = $id; |
| 74 | + |
| 75 | + $bootstrapFile = null; |
| 76 | + if ($isComposed) { |
| 77 | + [$bootstrapFile, $service] = explode(':', $id, 2); |
| 78 | + } |
| 79 | + |
| 80 | + // If not, we try to get the handler from the Symfony container |
| 81 | + return $this->symfonyContainer($bootstrapFile)->has($service); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Create and return the Symfony container. |
| 86 | + */ |
| 87 | + private function symfonyContainer(?string $bootstrapFile = null): ContainerInterface |
| 88 | + { |
| 89 | + // Only create it once |
| 90 | + if (! $this->symfonyContainer) { |
| 91 | + $bootstrapFile = $bootstrapFile ?: 'public/index.php'; |
| 92 | + |
| 93 | + if (! file_exists($bootstrapFile)) { |
| 94 | + throw new Exception( |
| 95 | + "Cannot find file '$bootstrapFile': the Bref-Symfony bridge tried to require that file to get the Symfony kernel. If your application does not have that file, follow the Bref-Symfony documentation to create and configure a file that returns the Symfony Kernel." |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + $app = require $bootstrapFile; |
| 100 | + |
| 101 | + if (! is_object($app)) { |
| 102 | + throw new Exception(sprintf( |
| 103 | + "The '%s' file must return an anonymous function (that returns the Symfony Kernel). Instead it returned '%s'. Either edit the file to return an anonymous function, or create a separate file (follow the online documentation to do so).", |
| 104 | + $bootstrapFile, |
| 105 | + // @phpstan-ignore-next-line |
| 106 | + is_object($app) ? get_class($app) : gettype($app), |
| 107 | + )); |
| 108 | + } |
| 109 | + |
| 110 | + $projectDir = getenv('LAMBDA_TASK_ROOT') ?: null; |
| 111 | + |
| 112 | + // Use the Symfony Runtime component to resolve the closure and get the PSR-11 container |
| 113 | + $options = $_SERVER['APP_RUNTIME_OPTIONS'] ?? []; |
| 114 | + if ($projectDir) { |
| 115 | + $options['project_dir'] = $projectDir; |
| 116 | + } |
| 117 | + $runtime = new BrefRuntime($options); |
| 118 | + |
| 119 | + [$app, $args] = $runtime |
| 120 | + ->getResolver($app) |
| 121 | + ->resolve(); |
| 122 | + |
| 123 | + $container = $app(...$args); |
| 124 | + |
| 125 | + if ($container instanceof KernelInterface) { |
| 126 | + $container->boot(); |
| 127 | + $container = $container->getContainer(); |
| 128 | + } |
| 129 | + |
| 130 | + if (! $container instanceof ContainerInterface) { |
| 131 | + throw new Exception(sprintf( |
| 132 | + "The closure returned by '%s' must return either a Symfony Kernel or a PSR-11 container. Instead it returned '%s'", |
| 133 | + $bootstrapFile, |
| 134 | + is_object($container) ? get_class($container) : gettype($container), |
| 135 | + )); |
| 136 | + } |
| 137 | + |
| 138 | + $this->symfonyContainer = $container; |
| 139 | + } |
| 140 | + |
| 141 | + return $this->symfonyContainer; |
| 142 | + } |
| 143 | +} |
0 commit comments