Skip to content

Commit 39f0d9c

Browse files
committed
Updated contents for the latest Symfony Flex recipes
1 parent 7dd9744 commit 39f0d9c

File tree

10 files changed

+64
-39
lines changed

10 files changed

+64
-39
lines changed

bin/console

+16-13
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,38 @@
33

44
use App\Kernel;
55
use Symfony\Bundle\FrameworkBundle\Console\Application;
6+
use Symfony\Component\Console\Input\ArgvInput;
67
use Symfony\Component\Debug\Debug;
7-
use Symfony\Component\Dotenv\Dotenv;
88

99
set_time_limit(0);
1010

11-
require __DIR__.'/../vendor/autoload.php';
11+
require dirname(__DIR__).'/vendor/autoload.php';
1212

1313
if (!class_exists(Application::class)) {
14-
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
14+
throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
1515
}
1616

17-
if (!isset($_SERVER['APP_ENV'])) {
18-
if (!class_exists(Dotenv::class)) {
19-
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
20-
}
21-
(new Dotenv())->load(__DIR__.'/../.env');
17+
$input = new ArgvInput();
18+
if (null !== $_ENV['APP_ENV'] = $input->getParameterOption(['--env', '-e'], null, true)) {
19+
putenv('APP_ENV='.$_ENV['APP_ENV']);
20+
// force loading .env files when --env is defined
21+
$_SERVER['APP_ENV'] = null;
22+
}
23+
24+
if ($input->hasParameterOption('--no-debug', true)) {
25+
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
2226
}
2327

24-
$env = $_SERVER['APP_ENV'] ?? 'dev';
25-
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));
28+
require dirname(__DIR__).'/config/bootstrap.php';
2629

27-
if ($debug) {
30+
if ($_SERVER['APP_DEBUG']) {
2831
umask(0000);
2932

3033
if (class_exists(Debug::class)) {
3134
Debug::enable();
3235
}
3336
}
3437

35-
$kernel = new Kernel($env, $debug);
38+
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
3639
$application = new Application($kernel);
37-
$application->run();
40+
$application->run($input);

config/bootstrap.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Symfony\Component\Dotenv\Dotenv;
4+
5+
require dirname(__DIR__).'/vendor/autoload.php';
6+
7+
if (!array_key_exists('APP_ENV', $_SERVER)) {
8+
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] ?? null;
9+
}
10+
11+
if ('prod' !== $_SERVER['APP_ENV']) {
12+
if (!class_exists(Dotenv::class)) {
13+
throw new RuntimeException('The "APP_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
14+
}
15+
16+
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
17+
}
18+
19+
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?: $_ENV['APP_ENV'] ?: 'dev';
20+
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];
21+
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';

config/packages/framework.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
framework:
22
secret: '%env(APP_SECRET)%'
3-
csrf_protection: { enabled: true }
3+
csrf_protection: true
44
http_method_override: true
55
trusted_hosts: ~
66
session:
77
# With this config, PHP's native session handling is used
88
handler_id: ~
9+
cookie_secure: auto
10+
cookie_samesite: lax
911
# When using the HTTP Cache, ESI allows to render page fragments separately
1012
# and with different cache configurations for each fragment
1113
# https://symfony.com/doc/current/book/http_cache.html#edge-side-includes
12-
esi: { enabled: true }
13-
fragments: { enabled: true }
14+
esi: true
15+
fragments: true
1416
php_errors:
1517
log: true
1618
assets:

config/packages/prod/monolog.yaml

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ monolog:
77
excluded_http_codes: [404]
88
nested:
99
type: stream
10-
path: '%kernel.logs_dir%/%kernel.environment%.log'
10+
path: "%kernel.logs_dir%/%kernel.environment%.log"
1111
level: debug
1212
console:
13-
type: console
13+
type: console
1414
process_psr_3_messages: false
15-
channels: ['!event', '!doctrine']
15+
channels: ["!event", "!doctrine"]
16+
deprecation:
17+
type: stream
18+
path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
19+
deprecation_filter:
20+
type: filter
21+
handler: deprecation
22+
max_level: info
23+
channels: ["php"]

config/packages/routing.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
framework:
22
router:
33
strict_requirements: ~
4+
utf8: true

config/packages/test/framework.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
framework:
2-
test: ~
2+
test: true
33
session:
44
storage_id: session.storage.mock_file

config/packages/translation.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
framework:
22
default_locale: '%locale%'
33
translator:
4+
default_path: '%kernel.project_dir%/translations'
45
fallbacks:
56
- '%locale%'

config/packages/twig.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
twig:
22
debug: '%kernel.debug%'
3+
default_path: '%kernel.project_dir%/templates'
34
strict_variables: '%kernel.debug%'
45
form_themes:
56
- 'form/layout.html.twig'

public/index.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,25 @@
22

33
use App\Kernel;
44
use Symfony\Component\Debug\Debug;
5-
use Symfony\Component\Dotenv\Dotenv;
65
use Symfony\Component\HttpFoundation\Request;
76

8-
require __DIR__.'/../vendor/autoload.php';
7+
require dirname(__DIR__).'/config/bootstrap.php';
98

10-
// The check is to ensure we don't use .env in production
11-
if (!isset($_SERVER['APP_ENV'])) {
12-
if (!class_exists(Dotenv::class)) {
13-
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
14-
}
15-
(new Dotenv())->load(__DIR__.'/../.env');
16-
}
17-
18-
$env = $_SERVER['APP_ENV'] ?? 'dev';
19-
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));
20-
21-
if ($debug) {
9+
if ($_SERVER['APP_DEBUG']) {
2210
umask(0000);
2311

2412
Debug::enable();
2513
}
2614

27-
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
15+
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
2816
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
2917
}
3018

31-
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
32-
Request::setTrustedHosts(explode(',', $trustedHosts));
19+
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
20+
Request::setTrustedHosts([$trustedHosts]);
3321
}
3422

35-
$kernel = new Kernel($env, $debug);
23+
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
3624
$request = Request::createFromGlobals();
3725
$response = $kernel->handle($request);
3826
$response->send();

src/Kernel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function registerBundles()
3838
{
3939
$contents = require $this->getProjectDir().'/config/bundles.php';
4040
foreach ($contents as $class => $envs) {
41-
if (isset($envs['all']) || isset($envs[$this->environment])) {
41+
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
4242
yield new $class();
4343
}
4444
}

0 commit comments

Comments
 (0)