|
| 1 | +<?php |
| 2 | + |
| 3 | +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
| 4 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 5 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 6 | +use Symfony\Component\HttpFoundation\Request; |
| 7 | +use Symfony\Component\HttpKernel\Kernel as BaseKernel; |
| 8 | +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; |
| 9 | + |
| 10 | +require __DIR__ . '/../vendor/autoload.php'; |
| 11 | + |
| 12 | +class Kernel extends BaseKernel |
| 13 | +{ |
| 14 | + use MicroKernelTrait; |
| 15 | + |
| 16 | + public function getCacheDir() |
| 17 | + { |
| 18 | + if(isset($_ENV['NOW_REGION'])){ |
| 19 | + return '/tmp/symfony/cache/'.$this->environment; |
| 20 | + } |
| 21 | + |
| 22 | + return $this->getProjectDir().'/var/cache/'.$this->environment; |
| 23 | + } |
| 24 | + |
| 25 | + public function getLogDir() |
| 26 | + { |
| 27 | + if(isset($_ENV['NOW_REGION'])){ |
| 28 | + return '/tmp/symfony/log'; |
| 29 | + } |
| 30 | + |
| 31 | + return $this->getProjectDir().'/var/log'; |
| 32 | + } |
| 33 | + |
| 34 | + public function registerBundles(): array |
| 35 | + { |
| 36 | + return [ |
| 37 | + new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + protected function configureContainer(ContainerConfigurator $c): void |
| 42 | + { |
| 43 | + // PHP equivalent of config/packages/framework.yaml |
| 44 | + $c->extension('framework', [ |
| 45 | + 'secret' => 'S0ME_SECRET' |
| 46 | + ]); |
| 47 | + } |
| 48 | + |
| 49 | + protected function configureRoutes(RoutingConfigurator $routes): void |
| 50 | + { |
| 51 | + $routes->add('index', '/')->controller([$this, 'handlerIndex']); |
| 52 | + $routes->add('random_number', '/random/{limit}')->controller([$this, 'handlerRandomNumber']); |
| 53 | + } |
| 54 | + |
| 55 | + public function handlerIndex(): JsonResponse |
| 56 | + { |
| 57 | + return new JsonResponse([ |
| 58 | + 'hello' => 'world', |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + public function handlerRandomNumber(int $limit): JsonResponse |
| 63 | + { |
| 64 | + return new JsonResponse([ |
| 65 | + 'number' => random_int(0, $limit), |
| 66 | + ]); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +$kernel = new Kernel('dev', true); |
| 71 | +$request = Request::createFromGlobals(); |
| 72 | +$response = $kernel->handle($request); |
| 73 | +$response->send(); |
| 74 | +$kernel->terminate($request, $response); |
0 commit comments