8000 [DI][FrameworkBundle] (Idea) PHP-DSL base class for simple shortcuts for DI · Issue #27073 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI][FrameworkBundle] (Idea) PHP-DSL base class for simple shortcuts for DI #27073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aurimasniekis opened this issue Apr 27, 2018 · 6 comments
Closed

Comments

@aurimasniekis
Copy link
Contributor
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 4.1

Hi,

Thanks for merging my PR #27065 my whole point of that change was to ability use inline classes with __invoke method and some shortcuts like addCommand, addService, addControllers, addRepository.

So I thought about something a bit wider scope for e.g. Make a base class inside FrameworkBundle which would have all shortcuts for main features and would allow extending default properties of it and etc.

Something like this:

<?php

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

abstract class ContainerConfig
{
    /**
     * @var ContainerConfigurator
     */
    private $containerConfig;

    public function addController(string $class, bool $autoWire = true)
    {
        // ...
    }

    public function addDoctrineRepository(string $entityClass)
    {
        // ...
    }

    public function addService(string $class, bool $autoWire = true, bool $public = false)
    {
        // ...
    }
    
    public function addEventListener(string $class, string $eventName)
    {
        // ...
    }

    public function __invoke(ContainerConfigurator $containerConfigurator)
    {
        $this->containerConfig = $containerConfigurator;
        
        $this->configure($containerConfigurator);
    }

    abstract public function configure(ContainerConfigurator $container);
}

return new class extends ContainerConfig {
    public function configure(ContainerConfigurator $container)
    {
        $this->addController(UserController::class);
        $this->addDoctrineRepository(User::class);
        $this->addService(UserCreationService::class);

        $this->addEventListener(UserCreationListener::class, 'user.new');
    }
};

Either way, I am going to make it either an external library or try to integrate inside FrameworkBundle. But I just don't know all shortcuts I should add and if someone could suggest more these kind of shortcuts I would not mind spending time implementing this util class.

@stof
Copy link
Member
stof commented Apr 27, 2018

We already have autoconfiguration being able to identifier event subscribers and doctrine repositories (and controllers when they extend the base class). So what would be the benefit of these shortcuts when the minimal code needed in all of them is to register the service ?

@aurimasniekis
Copy link
Contributor Author

Hmm, haven't heard about identifying doctrine repositories, cause all the time you need to define repo as a service via factory if you want to inject it... The key point of this idea is to minimize repetitive code when working on big projects, so for e.g. Instead of writing 5 lines of code every time you need to define repository as a service.

P.S. I think autoconfiguration identifier doesn't work with out of src folder code for e.g. If the application is designed to be modular.

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Apr 27, 2018

As @stof, I'm not sure this would provide many benefits over the current way to express the same. It would also be for sure much more restrictive.

BUT, that could instead be a great start to implement per bundle PHP-DSL-style configuration, instead of the current ->extension('foo', array(........)):

return new class extends FrameworkBundleConfigurator {
    public function configure()
    {
        $this->...; // to be created
    }
};

autoconfiguration identifier doesn't work with out of src folder code

Yes it does: autoconfiguration works where you configure it. Can be anywhere.

@stof
Copy link
Member
stof commented Apr 27, 2018

Hmm, haven't heard about identifying doctrine repositories, cause all the time you need to define repo as a service via factory if you want to inject it...

Look at the new features of DoctrineBundle 1.8 about that: doctrine/DoctrineBundle#727

Registering the Doctrine repository as a service without this feature is a very bad idea, as it breaks the entity manager resetting (your service keeps using the old entity manager). And with this feature, it can be autoconfigured.

@fabpot
Copy link
Member
fabpot commented Aug 6, 2019

Closing this issue as it didn't get traction.

@fabpot fabpot closed this as completed Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0