Skip to content

Commit

Permalink
feat: use PHP config instead of YAML (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
COil authored Dec 5, 2024
1 parent cee23fa commit 3c45a7b
Show file tree
Hide file tree
Showing 30 changed files with 472 additions and 239 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
MicroSymfony is a [Symfony 7.2](https://symfony.com/blog/symfony-7-2-curated-new-features)
application skeleton on steroids, ready to use.

I have made a long blog post explaining all it contains; it will be the reference
for documentation.
I'll update it when needed:
I have made a long blog post explaining the philosophy behind and how to use it:

* [Introducing the MicroSymfony application template](https://www.strangebuzz.com/en/blog/introducing-the-microsymfony-application-template)

Expand Down Expand Up @@ -171,7 +169,7 @@ are not merged in the main branch but are used to test the integration of a new
vendor library or make a [POC](https://en.wikipedia.org/wiki/Proof_of_concept).
For example, have you ever dreamed of testing [Eloquent](https://laravel.com/docs/11.x/eloquent#introduction),
the Laravel ORM, on a Symfony project?
Then clone the `eloquent` branch and run `composer install && make load-fixtures`.
Then clone the `eloquent` branch, and run `composer install && make load-fixtures`.

### Infrastructure

Expand All @@ -180,7 +178,7 @@ Then clone the `eloquent` branch and run `composer install && make load-fixtures

### Database 💽

These « database » branches aim to display a list of records coming from a [SQLite](https://www.sqlite.org/)
These « database » branches aim to display a list of records from a [SQLite](https://www.sqlite.org/)
database.

* Doctrine DBAL ([PR](https://github.com/strangebuzz/MicroSymfony/pull/72), [branch](https://github.com/strangebuzz/MicroSymfony/tree/doctrine-dbal), rebased on 2024-11-17)
Expand All @@ -192,19 +190,33 @@ database.
* TwigStan ([PR](https://github.com/strangebuzz/MicroSymfony/pull/95), [branch](https://github.com/strangebuzz/MicroSymfony/tree/twigstan), rebased on 2024-11-17)
* Twig-CS-Fixer ([PR](https://github.com/strangebuzz/MicroSymfony/pull/118), [branch](https://github.com/strangebuzz/MicroSymfony/tree/feat/use-twig-cs-fixer), rebased on 2024-11-21)

These branches will be rebased after each release so they are always up to date.
One will rebase those branches regularly 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.
These YAML files are loaded, but you can convert them to PHP like the other configuration
files.
For example, to convert the `messenger` YAML configuration to PHP with [Simplify](https://github.com/symplify/config-transformer),
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 +227,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.

Loading

0 comments on commit 3c45a7b

Please sign in to comment.