68publishers/doctrine-bridge

连接Doctrine ORM集成与'68publishers bundles'的桥梁。

v1.1.0 2023-08-21 03:22 UTC

This package is auto-updated.

Last update: 2024-09-21 05:45:31 UTC


README

在CompilerExtensions中直接注册自定义DBAL类型、实体映射、目标实体和迁移目录!

Checks Coverage Status Total Downloads Latest Version PHP Version

安装

安装68publishers/doctrine-bridge的最佳方式是使用Composer。

$ composer require 68publishers/doctrine-bridge

配置

extensions:
    68publishers.doctrine_bridge: SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\DoctrineBridgeExtension

# The default configuration:
68publishers.doctrine_bridge:
    database_types_enabled: yes # Enables/disables registration of acustom DBAL types
    entity_mappings_enabled: yes # Enables/disables registration of entity mappings
    target_entities_enabled: yes # Enables/disables resolving of target entities
    migration_directories_enabled: yes # Enables/disables registration of migrations for doctrine/migrations

    # Dependent services. Allowed are classname strings (for autowired services) or references e.g. @myService
    services:
        dbal_connection: Doctrine\DBAL\Connection
        drivers:
            # For drivers, you can use `false`. In this case, mappings for the drive will be omitted
            chain: Doctrine\Persistence\Mapping\Driver\MappingDriverChain
            annotation: Doctrine\ORM\Mapping\Driver\AnnotationDriver
            xml: Doctrine\ORM\Mapping\Driver\XmlDriver
            simplified_xml: Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver
            attribute: Doctrine\ORM\Mapping\Driver\AttributeDriver
        migrations_configuration: Doctrine\Migrations\Configuration\Configuration

该包已与nettrine/ormnettrine/migrations完全测试过,但可以使用services选项将其集成到Nette Framework的几乎所有Doctrine集成中。

用法

数据库类型提供者

use Doctrine\DBAL\Types\Types;
use Nette\DI\CompilerExtension;
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\DatabaseType;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\DatabaseTypeProviderInterface;

class MyExtension extends CompilerExtension implements DatabaseTypeProviderInterface
{
    public function getDatabaseTypes() : array
    {
        return [
            new DatabaseType('uuid_binary_ordered_time', Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType::class, Types::BINARY),
            new DatabaseType('my_custom_type', MyCustomType::class),
        ];
    }
}

Doctrine类型中的服务

默认情况下,Doctrine DBAL类型无法访问服务。使用此扩展,您可以在创建连接时,在自定义类型中接收DI容器。

use Nette\DI\Container;
use Doctrine\DBAL\Types\StringType;
use SixtyEightPublishers\DoctrineBridge\Type\ContainerAwareTypeInterface;

final class MyExtension extends StringType implements ContainerAwareTypeInterface
{
    private MyService $service;

    public function setContainer(Container $container, array $context = []) : void
    {
        $this->service = $container->getByType(MyService::class);
    }
}

通过捆绑的DatabaseTypeProviderExtension注册Doctrine类型

要注册自定义类型,无需创建自定义扩展,但可以使用DatabaseTypeProviderExtension类。

extensions:
    68publishers.doctrine_bridge.database_type_provider: SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\DatabaseTypeProviderExtension

68publishers.doctrine_bridge.database_type_provider:
    # inline notation:
    my_type_1: App\DbalType\MyType1

    # structured notation:
    my_type_2:
        class: App\DbalType\MyType2
        mapping_type: text
        context: []

实体映射提供者

use Nette\DI\CompilerExtension;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\EntityMapping;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\EntityMappingProviderInterface;

class MyExtension extends CompilerExtension implements EntityMappingProviderInterface
{
    public function getEntityMappings() : array
    {
        return [
            new EntityMapping(EntityMapping::DRIVER_ANNOTATION, 'App\\Entity', __DIR__ . '/../Entity'),
            new EntityMapping(EntityMapping::DRIVER_ATTRIBUTE, 'App\\Entity', __DIR__ . '/../Entity'),
            new EntityMapping(EntityMapping::DRIVER_XML, 'App\\Entity', __DIR__ . '/../Mapping/xml'),
            # or
            new EntityMapping(EntityMapping::DRIVER_SIMPLIFIED_XML, 'App\\Entity', __DIR__ . '/../Mapping/xml'),
        ];
    }
}

目标实体提供者

use Nette\DI\CompilerExtension;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\TargetEntity;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\TargetEntityProviderInterface;

class MyExtension extends CompilerExtension implements TargetEntityProviderInterface
{
    public function getTargetEntities() : array
    {
        return [
            new TargetEntity(ProductInterface::class, ProductEntity::class),
        ];
    }
}

迁移目录

use Nette\DI\CompilerExtension;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\TargetEntity;
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\MigrationsDirectoriesProviderInterface;

class MyExtension extends CompilerExtension implements MigrationsDirectoriesProviderInterface
{
    public function getMigrationsDirectories() : array
    {
        return [
            new MigrationsDirectory('App\\Bundle\\MyBundle\\Migrations', __DIR__ . '/../Migrations'),
        ];
    }
}

贡献

在提交拉取请求之前,请使用以下命令检查您的更改:

$ make init # to pull and start all docker images

$ make cs.check
$ make stan
$ make tests.all