phpsymfonyno-framework

Symfony Components without the full stack framework (Episode 2)


I'm only using Symfony DI, Http, Kernel, Routing components in my project following Create your own PHP Framework (https://symfony.com/doc/current/create_framework/index.html). Project, on which I specify, that @cerad was of great help. Thanks again. It's ok with Service and Controller dependency injection.

Cf. Symfony Components without the full stack framework (Episode 1) And https://github.com/Monnomcjo/symfony-simplex

But now I encounter a new difficulty with instantiate repository in dependency injection.

I added symfony/orm-pack to use ServiceEntityRepository.

UPDATED :

As specified in the comments my concern is a problem of approach.

My question is poorly put and the context is not sufficiently clear. Still in a learning environment, I try to implement the concepts of Clean Architecture and Domain Driven Design.

In this idea of ​​partitioning and independence, I also try to better master my Framework by starting from almost nothing, to add as I go, only what I need.

Still following the comments I no longer use symfony / orm-pack but doctrine / dbal.

There is still a lot to do but it works.

#container.php

// Doctrine
$containerBuilder->register(\ExampleApp\Domain\Client\Entity\ClientRepository::class, \Infrastructure\Persistence\Doctrine\Client\DoctrineClientRepository::class)
    ->setArguments([new Reference('service_container')]);

$containerBuilder->register(\ExampleApp\Domain\Client\UseCase\GetClient\GetClient::class, \ExampleApp\Domain\Client\UseCase\GetClient\GetClient::class)
    ->setArguments([new Reference(\ExampleApp\Domain\Client\Entity\ClientRepository::class)]);

$containerBuilder->register(\ExampleApp\Presentation\Client\GetClientHtmlPresenter::class, \ExampleApp\Presentation\Client\GetClientHtmlPresenter::class);

$containerBuilder->register(\Infrastructure\View\GetClientView::class, \Infrastructure\View\GetClientView::class);

$containerBuilder->register(\Infrastructure\Controller\ClientController::class, \Infrastructure\Controller\ClientController::class)
    ->setArguments([
        new Reference(\ExampleApp\Domain\Client\UseCase\GetClient\GetClient::class),
        new Reference(\ExampleApp\Presentation\Client\GetClientHtmlPresenter::class),
        new Reference(\Infrastructure\View\GetClientView::class)
]);

Returns are ok now.

https://github.com/Monnomcjo/symfony-simplex is updated too.

Next step, add .env, and conf files type services.yaml


Solution

  • Since your problem right now is with the missing doctrine.orm.entity_manager I think the best way to see how Symfony does it, is to look at the DoctrineBundle. Specifically at DependencyInjection/DoctrineExtension.php and the Configuration from which the config values are received. You might also want to take a look at the service definitions.

    The Extension class takes the configuration (usually in your config/packages/doctrine.yaml) that is validated through the Configuration and then updates the service definitions under Resources/config. This approach is rather complex, but it will help you see how things are connected in Symfony.

    Alternatively you can look at the Doctrine Docs to see how to get an EntityManager and then convert this into Symfony Service Container registration:

    $isDevMode = true;
    $proxyDir = null;
    $cache = null;
    $useSimpleAnnotationReader = false;
    $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode, $proxyDir, $cache, $useSimpleAnnotationReader);
    // or if you prefer yaml or XML
    //$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
    //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
    
    // database configuration parameters
    $conn = array(
        'driver' => 'pdo_sqlite',
        'path' => __DIR__ . '/db.sqlite',
    );
    
    // obtaining the entity manager
    $entityManager = EntityManager::create($conn, $config);
    

    You can see the EntityManager::create in the service definition link above. How you do this in your application largely depends on how close you want to build it to Symfony and what and how you want to configure around Doctrine.