没有完整堆栈框架的 Symfony 组件(第 2 集)

Symfony Components without the full stack framework (Episode 2)

我只在我的项目中使用 Symfony DI、Http、内核、路由组件,然后创建您自己的 PHP 框架 (https://symfony.com/doc/current/create_framework/index.html)。 我指定的项目,@cerad 提供了很大的帮助。再次感谢。 Service和Controller依赖注入没问题

比照。 (第 1 集) 和 https://github.com/Monnomcjo/symfony-simplex

但是现在我在依赖注入中实例化存储库遇到了新的困难。

我添加了 symfony/orm-pack 以使用 ServiceEntityRepository。

更新:

如评论中所述,我担心的是方法问题。

我的问题提得不好,上下文不够清楚。仍然在学习环境中,我尝试实现 Clean Architecture 和 Domain Driven Design 的概念。

在这种分区和独立的想法中,我也试图通过几乎从无到有,只添加我需要的来更好地掌握我的框架。

仍然关注评论,我不再使用 symfony / orm-pack,而是使用 doctrine / dbal。

还有很多事情要做,但它有效。

#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现在好了。

https://github.com/Monnomcjo/symfony-simplex也更新了。

下一步,添加 .env 和 conf 文件类型 services.yaml

因为你现在的问题是缺少 doctrine.orm.entity_manager 我认为了解 Symfony 如何做到这一点的最好方法是查看 DoctrineBundle。具体在 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.

扩展 class 采用通过配置验证的配置(通常在您的 config/packages/doctrine.yaml 中),然后更新 Resources/config 下的服务定义。这种方法相当复杂,但它会帮助您了解事物在 Symfony 中是如何连接的。

或者您可以查看 Doctrine Docs 以了解如何获取 EntityManager 然后将其转换为 Symfony 服务容器注册:

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

您可以在上面的服务定义link中看到EntityManager::create。你如何在你的应用程序中做到这一点在很大程度上取决于你想要将它构建成与 Symfony 的接近程度以及你想要围绕 Doctrine 配置什么以及如何配置。