yceruto/micro-symfony

Micro-Symfony Tools

v4.0.3 2024-06-28 19:22 UTC

README

Class helpers for Symfony applications.

ci

Installation

composer require yceruto/micro-symfony

AbstractBundle

Bundles are a very important piece of code in your Symfony applications, and most of the time they require special configuration and DI extensions to achieve their goal.

In that sense, this AbstractBundle class will help you to create a concise and small bundle, fastly, focusing on what you only need to define and import by providing useful shortcuts and configurators:

namespace Acme\FooBundle;

use MicroSymfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use MicroSymfony\Component\HttpKernel\Bundle\AbstractBundle;
// ...

class AcmeFooBundle extends AbstractBundle
{
    protected string $extensionAlias = ''; // set here the custom extension alias, e.g. 'foo' (default 'acme_foo')

    public function configuration(DefinitionConfigurator $definition): void
    {
        // loads config definition from a file
        $definition->import('../config/definition.php');

        // loads config definition from multiple files (when it's too long you can split it)
        $definition->import('../config/definition/*.php');

        // defines config directly when it's short
        $definition->rootNode()
            ->children()
                ->scalarNode('foo')->defaultValue('bar')->end()
            ->end()
        ;
    }

    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        // prepend config from plain array
        $builder->prependExtensionConfig('framework', [
            'cache' => ['prefix_seed' => 'foo/bar'],
        ]);

        // prepend config from a config file
        $container->import('../config/packages/cache.yaml');
    }

    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        $container->parameters()
            ->set('foo', $config['foo']);

        $container->import('../config/services.php');

        if ('bar' === $config['foo']) {
            $container->services()
                ->set(Foobar::class);
        }
    }
}

With this class you don't have to create a separate class for Extension or Configuration. Further, all methods contain configurators that allow you to import a definition or config file in any supported format (yaml, xml, php, etc.)

This is how the configuration definition should look when you are importing it from a file:

// acme/foo_bundle/config/definition.php

use MicroSymfony\Component\Config\Definition\Configurator\DefinitionConfigurator;

return static function (DefinitionConfigurator $definition) {
    $definition->rootNode()
        ->children()
            ->scalarNode('foo')->defaultValue('bar')->end()
        ->end()
    ;
};

AbstractExtension

In some cases, mainly for bundle-less approach, you might want to add a DI extension to your application without a bundle class. This AbstractExtension class will help you to simplify your extension definition by providing the same useful shortcuts and configurators:

namespace App\FooModule\Infrastructure\Symfony\DependecyInjection;

use MicroSymfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use MicroSymfony\Component\DependencyInjection\Extension\AbstractExtension;
// ...

class FooExtension extends AbstractExtension
{
    public function configuration(DefinitionConfigurator $definition): void
    {
        // loads config definition from a file
        $definition->import('../../config/definition.php');

        // loads config definition from multiple files (when it's too long you can split it)
        $definition->import('../../config/definition/*.php');

        // defines config directly when it's short
        $definition->rootNode()
            ->children()
                ->scalarNode('foo')->defaultValue('bar')->end()
            ->end()
        ;
    }

    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        // prepend config from plain array
        $builder->prependExtensionConfig('framework', [
            'cache' => ['prefix_seed' => 'foo/bar'],
        ]);

        // prepend config from a config file
        $container->import('../../config/packages/cache.yaml');
    }

    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        $container->parameters()
            ->set('foo', $config['foo']);

        $container->import('../../config/services.php');

        if ('bar' === $config['foo']) {
            $container->services()
                ->set(Foobar::class);
        }
    }
}

You can register your extension directly into the Kernel this way:

class Kernel extends BaseKernel
{
    protected function build(ContainerBuilder $container): void
    {
        $container->registerExtension(new FooExtension());
    }
}

MicroKernelTrait

This class is an implementation of the base Kernel + MicroKernelTrait that allows you to create a single "one-file" application for your cloud worker, microservice, or any other small application.

// index.php

use MicroSymfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\Kernel;

class StripeWebhookEventSubscriber extends Kernel
{
    use MicroKernelTrait;

    #[Route('/', methods: 'GET')]
    public function __invoke(Request $request, NotifierInterface $notifier): Response
    {
        // parse the webhook event and notify the user...
    
        return new Response('OK');
    }
}

return static function (array $context) {
    $kernel = new StripeWebhookEventSubscriber($context['APP_ENV'], (bool) $context['APP_DEBUG']);

    return \PHP_SAPI === 'cli' ? new Application($kernel) : $kernel;
};

You can use the same index.php as console application to perform the common cache clear operations or any other command you need to run.

$ php index.php cache:clear

Symfony 6.1 Support

This feature is fully implemented since Symfony 6.1, so you can remove this package from your dependencies after upgrading accordingly.

Symfony 7.1 Support

The $container->import() method support in prependExtension was implemented in Symfony 7.1, so you can remove this package from your dependencies after upgrading accordingly.

Symfony 7.2 Support

The MicroKernelTrait optional capabilities were implemented in Symfony 7.2, so you can remove this package from your dependencies after upgrading accordingly.

Upgrade Notes

All classes included in this package are registered under the MicroSymfony namespace, however, they follow the same organization that Symfony. Thus, to upgrade just remove the Micro prefix from all imported classes and everything should keep working as before.

-use MicroSymfony\Component\DependencyInjection\Extension\AbstractExtension;
+use Symfony\Component\DependencyInjection\Extension\AbstractExtension;

License

This software is published under the MIT License