Skip to content

Commit

Permalink
feat: use PHP config instead of YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
COil committed Dec 4, 2024
1 parent cee23fa commit ad1b87c
Show file tree
Hide file tree
Showing 30 changed files with 467 additions and 233 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,27 @@ These branches will be rebased after each release so they are always up to date.

## Notes 📒

### Symfony-UX

Turbo forms are disabled in [assets/app.js](https://github.com/strangebuzz/MicroSymfony/blob/main/assets/app.js).
To enable the feature for a given form, add the `data-turbo="true"` attribute to it.
Or change the parameter `Turbo.setFormMode` to `on` to activate the feature globally.
In both cases, your controller code has to be [modified accordingly](https://symfony.com/bundles/ux-turbo/current/index.html#3-form-response-code-changes).

### PHP configuration files

If you install a new Symfony library, the flex recipes can add YAML files to your
project.
Theses YAML files are loaded but you can convert them to PHP like the other configuration
files.
For exemple, to convert the `messenger` configuration, run:

vendor/bin/config-transformer convert config/packages/messenger.yaml


## Other good practices 👌

* Using PHP configuration files instead of YAML ([source](https://github.com/strangebuzz/MicroSymfony/blob/main/config/services.php))
* Using strict types in all PHP files ([source](https://github.com/strangebuzz/MicroSymfony/blob/main/src/Controller/SlugifyAction.php#L3))
* Using the ADR pattern in an action controller ([source](https://github.com/strangebuzz/MicroSymfony/blob/main/src/Controller/SlugifyAction.php)) ([doc](https://symfony.com/doc/current/controller/service.html#invokable-controllers))
* The [composer.json](https://github.com/strangebuzz/MicroSymfony/blob/main/composer.json)
Expand All @@ -215,6 +228,7 @@ In both cases, your controller code has to be [modified accordingly](https://sym

## References 📚

* [How to Switch from YAML Configs to PHP Today with Symplify](https://tomasvotruba.com/blog/2020/07/27/how-to-switch-from-yaml-xml-configs-to-php-today-with-migrify/) (tomasvotruba.com)
* [PHPStan 2.0 Released With Level 10 and Elephpants!](https://phpstan.org/blog/phpstan-2-0-released-level-10-elephpants) (phpstan.org)
* [A better ADR pattern for your Symfony controllers](https://www.strangebuzz.com/en/blog/a-better-adr-pattern-for-your-symfony-controllers) (strangebuzz.com)
* [My Taskfile configuration for Symfony](https://jmsche.fr/en/blog/my-taskfile-configuration-for-symfony) (jmsche.fr)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"symfony/maker-bundle": "^1.61",
"symfony/requirements-checker": "^2.0",
"symfony/stopwatch": "~7.2.0",
"symfony/web-profiler-bundle": "~7.2.0"
"symfony/web-profiler-bundle": "~7.2.0",
"symplify/config-transformer": "^12.3"
},
"replace": {
"symfony/polyfill-ctype": "*",
Expand Down
44 changes: 43 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions config/packages/asset_mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('framework', [
'asset_mapper' => [
// The paths to make available to the asset mapper.
'paths' => [
'assets/',
],
],
]);
};
5 changes: 0 additions & 5 deletions config/packages/asset_mapper.yaml

This file was deleted.

29 changes: 29 additions & 0 deletions config/packages/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('framework', [
// Unique name of your app: used to compute stable namespaces for cache keys.
// 'prefix_seed' => 'your_vendor_name/app_name',

// The "app" cache stores to the filesystem by default.
// The data in this cache should persist between deploys.
// Other options include:

// Redis
// 'app' => 'cache.adapter.redis',
// 'default_redis_provider' => 'redis://localhost',

// APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
// 'app' => 'cache.adapter.apcu',

// Namespaced pools use the above "app" backend by default
// 'pools' => [
// 'my.dedicated.cache' => null
// ],
'cache' => null,
]);
};
19 changes: 0 additions & 19 deletions config/packages/cache.yaml

This file was deleted.

23 changes: 23 additions & 0 deletions config/packages/csrf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// Enable stateless CSRF protection for forms and logins/logouts
$containerConfigurator->extension('framework', [
'form' => [
'csrf_protection' => [
'token_id' => 'submit',
],
],
'csrf_protection' => [
'stateless_token_ids' => [
'submit',
'authenticate',
'logout',
],
],
]);
};
11 changes: 0 additions & 11 deletions config/packages/csrf.yaml

This file was deleted.

39 changes: 39 additions & 0 deletions config/packages/framework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// see https://symfony.com/doc/current/reference/configuration/framework.html
$containerConfigurator->extension('framework', [
'secret' => '%env(APP_SECRET)%',
// 'csrf_protection' => true,
'http_method_override' => false,
'handle_all_throwables' => true,

// Enables session support. Note that the session will ONLY be started if you read or write from it.
// Remove or comment this section to explicitly disable session support.
'session' => [
'handler_id' => null,
'cookie_secure' => 'auto',
'cookie_samesite' => 'lax',
'storage_factory_id' => 'session.storage.factory.native',
],

// 'esi' => true
// 'fragments' => true
'php_errors' => [
'log' => true,
],
]);

if ($containerConfigurator->env() === 'test') {
$containerConfigurator->extension('framework', [
'test' => true,
'session' => [
'storage_factory_id' => 'session.storage.factory.mock_file',
],
]);
}
};
25 changes: 0 additions & 25 deletions config/packages/framework.yaml

This file was deleted.

110 changes: 110 additions & 0 deletions config/packages/monolog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('monolog', [
'channels' => [
'deprecation',
],
]);

if ($containerConfigurator->env() === 'dev') {
$containerConfigurator->extension('monolog', [
'handlers' => [
'main' => [
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
'channels' => [
'!event',
],
],
// uncomment to get logging in your browser
// you may have to allow bigger header sizes in your Web server configuration
// 'firephp' => [
// 'type' => 'firephp',
// 'level' => 'info',
// ],
// 'chromephp' => [
// 'type' => 'chromephp',
// 'level' => 'info',
// ],

'console' => [
'type' => 'console',
'process_psr_3_messages' => false,
'channels' => [
'!event',
'!doctrine',
'!console',
],
],
],
]);
}

if ($containerConfigurator->env() === 'test') {
$containerConfigurator->extension('monolog', [
'handlers' => [
'main' => [
'type' => 'fingers_crossed',
'action_level' => 'error',
'handler' => 'nested',
'excluded_http_codes' => [
404,
405,
],
'channels' => [
'!event',
],
],
'nested' => [
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
],
],
]);
}

if ($containerConfigurator->env() === 'prod') {
$containerConfigurator->extension('monolog', [
'handlers' => [
'main' => [
'type' => 'fingers_crossed',
'action_level' => 'error',
'handler' => 'nested',
'excluded_http_codes' => [
404,
405,
],
'buffer_size' => 50,
],
'nested' => [
'type' => 'stream',
'path' => 'php://stderr',
'level' => 'debug',
'formatter' => 'monolog.formatter.json',
],
'console' => [
'type' => 'console',
'process_psr_3_messages' => false,
'channels' => [
'!event',
'!doctrine',
],
],
'deprecation' => [
'type' => 'stream',
'channels' => [
'deprecation',
],
'path' => 'php://stderr',
],
],
]);
}
};
Loading

0 comments on commit ad1b87c

Please sign in to comment.