Skip to content

Commit d4625b0

Browse files
committed
Symfony: introduce micro2
1 parent ad5f2bc commit d4625b0

File tree

7 files changed

+2067
-10
lines changed

7 files changed

+2067
-10
lines changed

php-symfony-micro1/.gitignore

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
2-
###> symfony/framework-bundle ###
3-
/.env.local
4-
/.env.local.php
5-
/.env.*.local
6-
/public/bundles/
7-
/var/
8-
/vendor/
9-
###< symfony/framework-bundle ###
10-
111
# Vercel
122
.vercel
3+
4+
# PHP
5+
/vendor
6+
/var

php-symfony-micro2/.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Vercel
2+
.vercel
3+
4+
# PHP
5+
/vendor
6+
/var

php-symfony-micro2/.vercelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

php-symfony-micro2/api/index.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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);

php-symfony-micro2/composer.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"symfony/config": "^5.1",
4+
"symfony/http-kernel": "^5.1",
5+
"symfony/http-foundation": "^5.1",
6+
"symfony/routing": "^5.1",
7+
"symfony/dependency-injection": "^5.1",
8+
"symfony/framework-bundle": "^5.1"
9+
}
10+
}

0 commit comments

Comments
 (0)