定制 anh/doctrine-extensions-resource 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

anh/doctrine-extensions-resource

Composer 安装命令:

composer require anh/doctrine-extensions-resource

包简介

Doctrine extension for managing resources

README 文档

README

Build Status Scrutinizer Code Quality SensioLabsInsight

Extension provides simplified and unified interface for working with resources.

Installation

$ php composer.phar require 'anh/doctrine-extensions-resource:0.4.*'

Symfony integration

There is a bundle or package for that.

Usage

Defining resources

Resource definition is an array with required and optional keys.

$resources = [
    'article' => [ // resource name
        'model' => 'Some\Name\Space\Entity\Article', // model (required)
        'repository' => 'Some\Other\Name\Space\Repository', // you can override resource repository here (optional)
        'interface' => 'Another\Lib\Interface', // for Doctrine ResolveTargetEntityListener (optional, can be array, not implemented yet)
        'rules' => [ // rules for this resource (optional)
            'isPublished' => [
                'isDraft' => false,
                'r.publishedSince <= current_timestamp()',
            ],
        ],
    ],

    'category' => [ // another resource
        /* ... */
    ],
];

Initialization

You should configure object manager to use ResourceRepositoryFactory as repository factory in order to inject paginator and rule resolver services into repositories. Extension can use any paginator compatible with ResourcePaginatorInterface.

Create entity manager, event dispatcher, add event subscriber and create ResourceManagerFactory.

<?php

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Anh\DoctrineResource\ORM\ResourceRepositoryFactory;
use Anh\DoctrineResource\ORM\EventListener\LoadMetadataSubscriber;
use Anh\Paginator\Paginator;

$repositoryFactory = new ResourceRepositoryFactory($resources, new Paginator());

// create config for object manager
$config = new Configuration();
/* set up orm config here */

$config->setRepositoryFactory($repositoryFactory);

// create entity manager
$entityManager = EntityManager::create([/* connection params */], $config);

// create event dispatcher
$eventDispatcher = new EventDispatcher();
/* set up event dispatcher here */

// add doctrine event subscriber
$entityManager->getEventManager()->addEventSubscriber(new LoadMetadataSubscriber($resources));

// create factory for resource manager
$resourceManagerFactory = new ResourceManagerFactory($resources, $eventDispatcher);

CRUD operations with ResourceManager.

Each resource type has it's own resource manager, to instantiate it you should use ResourceManagerFactory::create().

$articleManager = $resourceManagerFactory->create('article', $entityManager);

// basic CRUD operations
// create resource
$article = $articleManager->createResource();
$article->setTitle('This is so test title');
$articleManager->create($article);

// update resource
$article->setTitle('This is test title');
$articleManager->update($article);

// delete resource
$articleManager->delete($article);

Events

Code above will generate this events:

  • anh_resource.article.pre_create
  • anh_resource.article.post_create
  • anh_resource.article.pre_update
  • anh_resource.article.post_update
  • anh_resource.article.pre_delete
  • anh_resource.article.post_delete

Fetching resources with ResourceRepository.

ResourceRepository has two methods for fetching resources: paginate() and fetch().

// resource repository usage
$articleRepository = $articleManager->getRepository();

// paginate published articles
$publishedArticles = $articleRepository->paginate(1, 20, ['[isPublished]']);

// fetch articles with complex criteria
$ratedArticles = $articleRepository->fetch(
    [ // criteria
        '%rating' => [ '>' => 10 ],
        '[isPublished]',
    ],
    [ // sorting
        'rating' => 'desc'
    ],
    5 // limit
);

See orm-example.php.

Advanced criteria format

You can use advanced criteria format for filtering. It's not limited to equal comparison only. For example:

$criteria = [
    'section' => 'articles', // old school
    '%rating' => [ '>' => 10 ],
    '#or' => [
        '%title-1' => [ 'like' => '%word.' ],
        '%title-2' => [ 'like' => 'Some%' ],
        '#and' => [
            'role' => [ 'moderator', 'editor' ],
            '#or' => [
                'status' => 'fixed',
                'isDraft' => true,
            ],
        ],
    ],
];

Advanced comparison

If the field name starts with % then advanced format for field should follow. It consists of the array with a single element, where key is the comparison operator and value is parameter for operator. Common operators are: >, <, >=, <=, <> and others, for full list refer to QueryBuilderAdapter of each driver.

Comparison types

Symbol # with following and or or is used for changing comparison type. Comparison types can be nested.

Multiple criteria on the same field

If you need a few criteria for the same field, simply add a dash followed by a number. Apply the same for the comparison types.

Rules

Rule is predefined group of criteria for resource. You can define it in resource definition (key rules). When rule is defined you can use its name in square brackets as criterion for ResourceRepository::paginate() and ResourceRepository::fetch() methods.

$repository = $manager->getRepository();
$publishedArticlesInSection = $repository->fetch([
    '[isPublished]',
    'section' => $section
]);

Auto joins

Query builder adds inner join automatically... (to be written)

$criteria = [
    'user.email' => $email,
    '%user.role' => [ 'in' => ['guest', 'anonymous'] ],
];

$sorting = [
    'user.createdAt' => 'desc',
];

This advanced criteria format is valid for all available drivers (ORM, PHPCR-ODM, MongoDB-ODM).

Note

Not all operators are available for each driver.

Credits

Inspired by SyliusResourceBundle from Sylius.

Versioning

Library uses semantic versioning.

License

MIT

anh/doctrine-extensions-resource 适用场景与选型建议

anh/doctrine-extensions-resource 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.97k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2014 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「manager」 「repository」 「doctrine」 「extension」 「object」 「resource」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 anh/doctrine-extensions-resource 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 anh/doctrine-extensions-resource 我们能提供哪些服务?
定制开发 / 二次开发

基于 anh/doctrine-extensions-resource 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 5
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-06-05