From 7152d55203fc4cdb4ea0c7d05bf049dc77de5f0a Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Thu, 2 May 2024 16:42:54 +0200 Subject: [PATCH] Add bundle helper docs --- blog/2024-05-02-new-docs.mdx | 12 ++++ docs/php/symfony/bundle-helpers/index.mdx | 83 ++++++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 blog/2024-05-02-new-docs.mdx diff --git a/blog/2024-05-02-new-docs.mdx b/blog/2024-05-02-new-docs.mdx new file mode 100644 index 0000000..ea4b8f3 --- /dev/null +++ b/blog/2024-05-02-new-docs.mdx @@ -0,0 +1,12 @@ +--- +title: New Docs +authors: [jannik] +tags: [bundle-helpers] +--- + +A bunch of new docs were written: + +- [Bundle Helpers] is now documented. + + +[Bundle Helpers]: /docs/php/symfony/bundle-helpers diff --git a/docs/php/symfony/bundle-helpers/index.mdx b/docs/php/symfony/bundle-helpers/index.mdx index 8fdd0c8..9888413 100644 --- a/docs/php/symfony/bundle-helpers/index.mdx +++ b/docs/php/symfony/bundle-helpers/index.mdx @@ -7,6 +7,85 @@ import {LinkList} from "../../../../src/components/Link/LinkList"; packagist="https://packagist.org/packages/21torr/bundle-helpers" /> -:::caution -The docs still need to be written. +The bundle helpers bundle contains several wrapper classes, that eases the integration of your bundle into Symfony. + +Instead of needing to manually writing an extension class, you can inline it in your bundle. + + +## Installation + +```shell +composer require 21torr/bundle-helpers +``` + + +## Usage + +There are two helpers for your bundle: + +- `BundleExtension` for when your bundle doesn't have configuration +- `ConfigurableBundleExtension` when it does + + +You it in your `Bundle::getContainerExtension()` method: + +```php +use Symfony\Component\HttpKernel\Bundle\Bundle; +use Torr\BundleHelpers\Bundle\BundleExtension; + +class MyBundle extends Bundle +{ + /** + * @inheritDoc + */ + public function getContainerExtension () : ExtensionInterface + { + return new BundleExtension($this); + } +} +``` + +The extension automatically + +- loads `config/services.yaml` in your bundle, if it exists +- deducts the bundle alias from your class name (without "Bundle" suffix and without the namespace before). + + +:::tip +You can customize the auto-generated bundle alias by explicitly passing the `$alias` parameter to the constructor. ::: + + +### Configurable Extension + +When your bundle needs configuration, you first create the regular configuration class for your bundle, [like in any other Symfony bundle]. + +You can then use that configuration directly by passing a closure: + +```php +use Symfony\Component\HttpKernel\Bundle\Bundle; +use Torr\BundleHelpers\Bundle\ConfigurableBundleExtension; + +class MyBundle extends Bundle +{ + /** + * @inheritDoc + */ + public function getContainerExtension () : ExtensionInterface + { + return new ConfigurableBundleExtension( + $this, + new MyBundleConfiguration(), // this is your custom configuration class + static function (array $config, ContainerBuilder $container) : void + { + // $config is your already parsed config and you can + // map it to container classes. + }, + } + ); + } +} +``` + + +[like in any other Symfony bundle]: https://symfony.com/doc/current/bundles/configuration.html