juliangut/doctrine-manager-builder

Doctrine2 管理器构建器

1.8.1 2018-10-30 14:47 UTC

This package is auto-updated.

Last update: 2024-08-31 00:27:54 UTC


README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Doctrine2 管理器构建器

让您摆脱配置 Doctrine 管理器、ORM 实体管理器、MongoDB 文档管理器和 CouchDB 文档管理器的繁琐工作。

安装

Composer

composer require juliangut/doctrine-manager-builder

如果使用 PHP >= 7.0 的 MongoDB

composer require alcaeus/mongo-php-adapter --ignore-platform-reqs

用法

关系型数据库实体管理器

use Jgut\Doctrine\ManagerBuilder\ManagerBuilder;
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;

$rdbmsBuilder = new RelationalBuilder([
    'annotation_autoloaders' => ['class_exists'],
    'connection' => [
        'driver' => 'pdo_sqlite',
        'memory' => true,
    ],
    'metadata_mapping' => [
        [
            'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 
            'path' => 'path/to/entities',
        ],
    ],
]);

MongoDB 文档管理器

use Jgut\Doctrine\ManagerBuilder\ManagerBuilder;
use Jgut\Doctrine\ManagerBuilder\MongoDBBuilder;

$mongoDBBuilder = new MongoDBBuilder([
    'annotation_autoloaders' => ['class_exists'],
    'connection' => [
        'server' => 'mongodb://localhost:27017',
        'options' => ['connect' => false],
    ],
    'metadata_mapping' => [
        [
            'driver' => new \Doctrine\ORM\Mapping\Driver\YamlDriver(
                'path/to/document/yaml/mapping/files',
                '.yml'
            ),
        ],
        [
            'type' => ManagerBuilder::METADATA_MAPPING_PHP,
            'path' => 'path/to/document/php/mapping/files',
            'namespace' => 'Project\Document\Namespace',
        ],
    ],
]);
$documentManager = $mongoDBBuilder->getManager();

CouchDB 文档管理器

use Jgut\Doctrine\ManagerBuilder\CouchDBBuilder;
use Jgut\Doctrine\ManagerBuilder\MongoDBBuilder;

$couchDBBuilder = new CouchDBBuilder([
    'annotation_autoloaders' => ['class_exists'],
    'connection' => [
        'host' => 'localhost',
        'dbname' => 'doctrine',
    ],
    'metadata_mapping' => [
        [
            'type' => ManagerBuilder::METADATA_MAPPING_XML,
            'path' => 'path/to/document/xml/mapping/files',
            'extension' => '.xml',
        ],
    ],
]);
$documentManager = $couchDBBuilder->getManager();

请注意,Doctrine CouchDB ODM 的支持不如 Doctrine ORM 或 Doctrine MongoDB ODM 好或广泛。

配置

通用

  • metadata_mapping 必需 创建它们的元数据映射驱动程序或配置的数组,见下文注释
  • annotation_files Doctrine 注释文件的数组
  • annotation_namespaces Doctrine 注释命名空间的数组
  • annotation_autoloaders Doctrine 注释自动加载调用的数组
  • proxies_path Doctrine 创建其代理类的路径,默认为 /tmp
  • proxies_namespace 代理命名空间字符串
  • proxies_auto_generation 表示代理自动生成行为的整数
  • metadata_cache_driver \Doctrine\Common\Cache\CacheProvider 元数据缓存驱动程序
  • metadata_cache_namespace 元数据缓存命名空间字符串(每种类型的 manager 都不同)
  • event_manager 配置的 Doctrine\Common\EventManager
  • event_subscribers 自定义 Doctrine\Common\EventSubscriber 的数组

关系型 ORM 实体管理器

  • connection 必需 PDO 配置的数组或 \Doctrine\DBAL\Connection。参见 支持的驱动程序
  • query_cache_driver \Doctrine\Common\Cache\CacheProvider 查询缓存驱动程序,默认为 metadata_cache_driver
  • query_cache_namespace 查询缓存命名空间字符串,默认为 'DoctrineRDBMSORMQueryCache'
  • result_cache_driver \Doctrine\Common\Cache\CacheProvider 结果缓存驱动程序,默认为 metadata_cache_driver
  • result_cache_namespace 结果缓存命名空间字符串,默认为 'DoctrineRDBMSORMResultCache'
  • hydrator_cache_driver \Doctrine\Common\Cache\CacheProvider 润滑器缓存驱动程序,默认为 metadata_cache_driver
  • hydrator_cache_namespace 润滑器缓存命名空间字符串,默认为 'DoctrineRDBMSORMHydratorCache'
  • repository_factory \Doctrine\ORM\Repository\RepositoryFactory
  • default_repository_class \Doctrine\ORM\EntityRepository
  • naming_strategy 一个 \Doctrine\ORM\Mapping\NamingStrategy,默认为 UnderscoreNamingStrategy
  • quote_strategy 一个 \Doctrine\ORM\Mapping\QuoteStrategy,默认为 DefaultQuoteStrategy
  • second_level_cache_configuration 一个 \Doctrine\ORM\Cache\CacheConfiguration
  • sql_logger 一个 \Doctrine\DBAL\Logging\SQLLogger
  • custom_types 一个数组,其中 'type_name' => '\Doctrine\DBAL\Types\Type'
  • custom_mapping_types 一个数组,其中 'type_name' => 'Doctrine type: a constant on \Doctrine\DBAL\Types\Type'。与 custom_types 一起使用
  • custom_filters 一个数组,其中 'filter_name' => '\Doctrine\ORM\Query\Filter\SQLFilter'
  • string_functions 一个数组,其中 'function_name' => '\Doctrine\ORM\Query\AST\Functions\FunctionNode' 用于字符串 DQL 函数
  • numeric_functions 一个数组,其中 'function_name' => '\Doctrine\ORM\Query\AST\Functions\FunctionNode' 用于数字 DQL 函数
  • datetime_functions 一个数组,其中 'function_name' => '\Doctrine\ORM\Query\AST\Functions\FunctionNode' 用于日期时间 DQL 函数

MongoDB ODM 文档管理器

  • connection 必需 \MongoClient 配置的数组(服务器和选项)或 \Doctrine\MongoDB\Connection
  • default_database 必需 在未指定的情况下要使用的默认数据库
  • hydrators_path Doctrine 创建其 hydrator 类的路径,默认为 /tmp
  • hydrators_namespace hydrator 命名空间字符串,默认为 'DoctrineMongoDBODMHydrator'
  • hydrators_auto_generation 指示 hydrator 自动生成行为的整数
  • persistent_collections_path Doctrine 创建其持久化集合类的路径,默认为 /tmp
  • persistent_collections_namespace 持久化集合命名空间的字符串,默认为 'DoctrineMongoDBODMPersistentCollection'
  • persistent_collections_auto_generation 持久化集合自动生成行为
  • repository_factory \Doctrine\ODM\MongoDB\Repository\RepositoryFactory
  • default_repository_class \Doctrine\ODM\MongoDB\DocumentRepository
  • logger_callable 有效的可调用对象
  • custom_types 包含 'type_name' => '\Doctrine\ODM\MongoDB\Types\Type' 的数组
  • custom_filters 包含自定义的 'filter_name' => '\Doctrine\ODM\MongoDB\Query\Filter\BsonFilter' 的数组

CouchDB ODM 文档管理器

  • connection 必需 包含 \Doctrine\CouchDB\CouchDBClient 配置的数组或一个 \Doctrine\CouchDB\CouchDBClient
  • repository_factory \Jgut\Doctrine\ManagerBuilder\CouchDB\Repository\RepositoryFactory
  • default_repository_class \Doctrine\ODM\CouchDB\DocumentRepository
  • lucene_handler_name Apache Lucene 处理器名称

注意事项

  • 确保始终提供一个 annotation_autoloader 可调用对象以在加载注解时回退,通常它是 'class_exists'。如果在创建多个管理器,则应将其添加到最后一个生成的管理器中。
  • metadata_mapping 必须是包含创建 MappingDriver 对象配置的数组的数组
    • type 是 \Jgut\Doctrine\ManagerBuilder\ManagerBuilder 常量之一: METADATA_MAPPING_ANNOTATIONMETADATA_MAPPING_XMLMETADATA_MAPPING_YAMLMETADATA_MAPPING_PHP,如果没有驱动程序则 必需
    • path 指示映射文件的字符串路径或路径数组,如果没有驱动程序则 必需
    • extension 覆盖默认映射文件扩展名:XML 文件为 '.dcm.xml',YAML 文件为 '.dcm.yml'
    • namespace 映射类所在的命名空间,只有当定义了多个映射驱动程序时才 必需
    • driver 一个已经创建的 \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver 对象,如果没有类型和路径则 必需
  • metadata_cache_driver,如果没有提供,将根据可用性自动生成以下顺序: ApcuCacheXcacheCacheMemcacheCacheRedisCache,最后回退到始终可用的 ArrayCache。任何未提供的其他缓存驱动程序都将回退到使用元数据缓存驱动程序的克隆。
  • proxies_auto_generationhydrators_auto_generationpersistent_collections_auto_generation 配置值是 Doctrine\Common\Proxy\AbstractProxyFactory 常量,在所有情况下默认为 AUTOGENERATE_NEVER

管理器正在配置 为生产环境准备,这意味着代理、hydrator 和持久化集合将不会自动生成,并且在没有提供缓存驱动程序的情况下,将自动生成一个。建议您始终提供您的缓存提供程序。对于开发,您应使用 VoidCache

扩展管理器

通过添加额外功能来扩展默认管理器非常简单。让我们看看两个与知名库相关的示例。

添加新类型

使用 ramsey/uuid-doctrine

composer require ramsey/uuid-doctrine
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;
use Ramsey\Uuid\Doctrine\UuidType;

require __DIR__ . '/vendor/autoload.php';

$rdbmsBuilder = new RelationalBuilder([
    'annotation_autoloaders' => ['class_exists'],
    'connection' => [
        'driver' => 'pdo_sqlite',
        'memory' => true,
    ],
    'metadata_mapping' => [
        [
            'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 
            'path' => 'path/to/entities',
        ],
    ],
    // Register UUID as custom type
    'custom_types' => ['uuid' => UuidType::class],
]);
$entityManager = $rdbmsBuilder->getManager();

添加新行为

使用 gedmo/doctrine-extensions

composer require gedmo/doctrine-extensions
use Gedmo\DoctrineExtensions;
use Gedmo\Sluggable\SluggableListener;
use Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
use Gedmo\Timestampable\TimestampableListener;
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;

require __DIR__ . '/vendor/autoload.php';

$rdbmsBuilder = new RelationalBuilder([
    'annotation_autoloaders' => ['class_exists'],
    'connection' => [
        'driver' => 'pdo_sqlite',
        'memory' => true,
    ],
    'metadata_mapping' => [
        [
            'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 
            'path' => 'path/to/entities',
        ],
    ],
    // Register new doctrine behaviours
    'event_subscribers' => [
        new SluggableListener,
        new TimestampableListener,
        new SoftDeleteableListener,
    ],
    // Register custom filters
    'custom_filters' => [
        'soft-deleteable' => SoftDeleteableFilter::class,
    ],
]);

// Register mapping driver into DoctrineExtensions
DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($rdbmsBuilder->getMetadataMappingDriver());

// Get entity manager as usual
$entityManager = $rdbmsBuilder->getManager();

控制台集成

尽管 Doctrine ORM 提供了出色的 CLI 工具,但此库旨在在没有 ORM 的情况下使用,因此已创建了一个新的工具,而不是强制要求使用 Doctrine ORM。

这个新的 CLI 工具 (doctrine-manager) 更强大,因为它通过提供 \Jgut\Doctrine\ManagerBuilder\ConsoleBuilder 的命名构建器来为同一类别的不同数据库(管理器)运行相同的命令。

“doctrine-manager”工具的配置与ORM自带的配置相似,因此您必须有一个“cli-config.php”或“config/cli-config.php”文件。

唯一的区别是,这里您必须返回一个Symfony\Component\Console\Application的实例,而不是返回一个Symfony\Component\Console\Helper\HelperSet的实例。

use Jgut\Doctrine\ManagerBuilder\ConsoleBuilder;
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;

require __DIR__ . '/vendor/autoload.php';

$settings = require 'managers-configurations.php';

$consoleBuilder = new ConsoleBuilder;
$consoleBuilder->addBuilder(new RelationalBuilder($settings['main'], 'one'));
$consoleBuilder->addBuilder(new RelationalBuilder($settings['secondary'], 'two'));

return $consoleBuilder->getApplication();

如果您运行./vendor/bin/doctrine-manager list,您会发现命令前缀是构建器的名称,因此命令是使用不同的管理器运行的。

Available commands:
...
 dbal
  dbal:one:import               Import SQL file(s) directly to Database.
  dbal:one:run-sql              Executes arbitrary SQL directly from the command line.
  dbal:two:import               Import SQL file(s) directly to Database.
  dbal:two:run-sql              Executes arbitrary SQL directly from the command line.
  ...
 orm
  orm:one:clear-cache:metadata  Clear all metadata cache of the various cache drivers.
  orm:one:clear-cache:query     Clear all query cache of the various cache drivers.
  orm:two:clear-cache:metadata  Clear all metadata cache of the various cache drivers.
  orm:two:clear-cache:query     Clear all query cache of the various cache drivers.
  ...

“doctrine-manager”只允许命名管理器构建器。

贡献

发现了一个错误或有一个功能请求?请新建一个issue。在新建之前,请先查看现有的issue。

请参阅CONTRIBUTING.md文件。

许可证

请参阅源代码中包含的LICENSE文件,以获取许可证条款的副本。