roave / psr-container-doctrine
PSR-11 容器使用的 Doctrine 工厂
Requires
- php: ~8.2.0 || ~8.3.0
- doctrine/common: ^3.4.4
- doctrine/dbal: ^4.0.4
- doctrine/event-manager: ^2.0.1
- doctrine/migrations: ^3.8.0
- doctrine/orm: ^3.2.1
- doctrine/persistence: ^3.3.3
- psr/cache: ^2.0.0 || ^3.0.0
- psr/container: ^1.1.2 || ^2.0.2
Requires (Dev)
- doctrine/coding-standard: ^12.0.0
- phpunit/phpunit: ^10.5.26
- psalm/plugin-phpunit: ^0.19.0
- symfony/yaml: ^7.1.1
- vimeo/psalm: ^5.25.0
- 5.3.x-dev
- 5.2.x-dev
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.x-dev
- 5.1.0
- 5.0.x-dev
- 5.0.0
- 4.2.x-dev
- 4.1.x-dev
- 4.1.0
- 4.0.x-dev
- 4.0.0
- 3.11.x-dev
- 3.10.x-dev
- 3.10.0
- 3.9.x-dev
- 3.9.0
- 3.8.x-dev
- 3.8.0
- 3.7.x-dev
- 3.7.0
- 3.6.x-dev
- 3.6.0
- 3.5.x-dev
- 3.5.0
- 3.4.x-dev
- 3.4.1
- 3.4.0
- 3.3.x-dev
- 3.3.0
- 3.2.x-dev
- 3.2.0
- 3.1.x-dev
- 3.1.1
- 3.1.0
- 3.0.x-dev
- 3.0.0
- 2.2.x-dev
- 2.2.0
- 2.1.x-dev
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/lock-file-maintenance
- dev-renovate/all-minor-patch
- dev-5.2.x-merge-up-into-5.3.x_mm3oYLTi
This package is auto-updated.
Last update: 2024-09-22 04:50:34 UTC
README
此包提供了一组工厂,用于与使用 PSR-11 标准的容器配合使用,以便在项目中轻松集成 Doctrine。此项目最初由 @DASPRiD 编写,但维护工作已由 Roave 接管。
安装
安装此包的最简单方法是通过 composer
$ composer require roave/psr-container-doctrine
配置
在一般情况下,如果您只使用单个连接,只需定义实体管理器工厂即可
return [ 'dependencies' => [ 'factories' => [ 'doctrine.entity_manager.orm_default' => \Roave\PsrContainerDoctrine\EntityManagerFactory::class, ], ], ];
如果您想添加第二个连接或使用除 "orm_default" 之外的其他名称,可以通过使用工厂的静态变体来实现
return [ 'dependencies' => [ 'factories' => [ 'doctrine.entity_manager.orm_other' => static fn (ContainerInterface $container) => (new EntityManagerFactory('orm_custom_key'))($container), ], ], ];
您还可以使用 ::class
功能定义别名以检索实体管理器实例
return [ 'aliases' => [ 'doctrine.entity_manager.orm_default' => Doctrine\ORM\EntityManagerInterface::class, ], ];
此包提供的每个工厂默认都会在容器中查找已注册的工厂。如果找不到,它将自动从动态创建的工厂中获取其依赖项。这可以为您节省在容器中注册可能根本不需要的工厂的麻烦。当然,您可以在需要时始终注册这些工厂。以下是一些额外的工厂
\Roave\PsrContainerDoctrine\CacheFactory
(doctrine.cache.*)\Roave\PsrContainerDoctrine\ConnectionFactory
(doctrine.connection.*)\Roave\PsrContainerDoctrine\ConfigurationFactory
(doctrine.configuration.*)\Roave\PsrContainerDoctrine\DriverFactory
(doctrine.driver.*)\Roave\PsrContainerDoctrine\EventManagerFactory
(doctrine.event_manager.*)
这些工厂中的每一个都支持与实体管理器工厂相同的静态行为。对于特定于容器的配置,示例目录中提供了一些示例
示例配置
完整的示例配置可以在 example/full-config.php 中找到。请注意,其中的值是默认值,如果您没有更改它们,则无需提供。请尽量保持您自己的配置尽可能简单。最小配置可以在 example/minimal-config.php 中找到
迁移
如果您想公开迁移命令,您必须将命令名称映射到 CommandFactory
。此工厂需要迁移配置设置。以下 ExecuteCommand
示例
return [ 'dependencies' => [ 'factories' => [ \Doctrine\Migrations\Tools\Console\Command\ExecuteCommand::class => \Roave\PsrContainerDoctrine\Migrations\CommandFactory::class, // Optionally, you could make your container aware of additional factories as of migrations release v3.0: \Doctrine\Migrations\Configuration\Migration\ConfigurationLoader::class => \Roave\PsrContainerDoctrine\Migrations\ConfigurationLoaderFactory::class, \Doctrine\Migrations\DependencyFactory::class => \Roave\PsrContainerDoctrine\Migrations\DependencyFactoryFactory::class, ], ], ];
您可以在 example/full-config.php 中找到可用命令的完整列表。
使用 Doctrine CLI
为了能够使用 Doctrine 的 CLI 工具,您需要在项目目录中创建一个 bin/doctrine
文件。这将设置命令行应用程序并允许您添加自定义命令。它与 设置控制台 中描述的文件几乎相同,但它从您的容器中获取 EntityManagerInterface
。
#!/usr/bin/env php <?php declare(strict_types=1); use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Tools\Console\ConsoleRunner; use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider; use Psr\Container\ContainerInterface; require 'vendor/autoload.php'; /** @var ContainerInterface $container */ $container = require 'config/container.php'; /** @var EntityManagerInterface $entityManager */ $entityManager = $container->get('doctrine.entity_manager.orm_default'); $commands = [ // If you want to add your own custom console commands, // you can do so here. ]; ConsoleRunner::run( new SingleManagerProvider($entityManager), $commands );
之后,调用 php bin/doctrine list
来查看可用的命令。
多个连接
当你有多个实体管理器时,这会变得有点复杂。Doctrine 本身没有处理这种情况的方法,所以一种可能的方法是复制上述命令两次,分别根据它们所工作的管理器命名,并从容器中拉取不同的实体管理器 - 例如 bin/doctrine-default
和 bin/doctrine-customer
。
以下代码可用于多个连接,但有一个缺点:你将不会在每个命令的帮助部分看到 --em=...
选项。
#!/usr/bin/env php <?php declare(strict_types=1); use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Tools\Console\ConsoleRunner; use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider; use InvalidArgumentException; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Component\Console\Input\ArgvInput; require 'vendor/autoload.php'; /** @var ContainerInterface $container */ $container = require 'config/container.php'; $input = new ArgvInput(); /** @var string $em */ $em = $input->getParameterOption('--em', 'orm_default'); // hack to remove the --em option, cause it's not supported by the original ConsoleRunner. foreach ($_SERVER['argv'] as $i => $arg) { if (0 === strpos($arg, '--em=')) { unset($_SERVER['argv'][$i]); } } try { $entityManager = $this->container->get('doctrine.entity_manager.'.$em); } catch (NotFoundExceptionInterface $serviceNotFoundException) { throw new InvalidArgumentException(sprintf('Missing entity manager with name "%s"', $em)); } $commands = [ // If you want to add your own custom console commands, // you can do so here. ]; ConsoleRunner::run( new SingleManagerProvider($entityManager), $commands );