PHP code example of yceruto / micro-symfony

1. Go to this page and download the library: Download yceruto/micro-symfony library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yceruto / micro-symfony example snippets


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);
        }
    }
}

// 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()
    ;
};

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);
        }
    }
}

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

// 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;
};
bash
$ php index.php cache:clear