kendoctor/knd-rad-bundle

Kendoctor Rad Edition 套件

v1.4 2015-11-09 13:06 UTC

This package is not auto-updated.

Last update: 2024-10-02 17:57:16 UTC


README

#KndRadBundle#

受 KnpRadBundle 启发,为 Symfony2 设计的快速应用开发套件

##特性###

  1. 自动依赖注入
  2. 实体模型管理器
  3. 模型安全投票者

##安装##

composer require knd/rad-bundle

安装后,将套件添加到内核中。

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new \Knd\Bundle\RadBundle\KndRadBundle(),
            ...
        );

    }
    ...
}

通过 Knd\Bundle\RadBundle\Bundle\Bundle 扩展你的套件

namespace AppBundle;
use Knd\Bundle\RadBundle\Bundle\Bundle;

class AppBundle extends Bundle
{
}

##自动依赖注入##

###常用类注入为服务###

如果你想将普通类注入为服务,例如

namespace AppBundle\Generator;

class SomeGenerator
{
}

app/config.yml 中添加 KndRadBundle 配置

knd_rad:
    auto_inject:
        common:
            #auto inject classes in the directories, default ``Form``
            dirs: [ Form, Generator, SomeDirectory/SubDirectory, ...]
            #auto inject specified classes, default None
            classes:
                - AppBundle\SomeDirectory\SomeClass
            #exclude classes to prevent being injected if the classes exist in dirs, default None
            exclude_classes:
                - AppBundle\OtherDirectory\OtherClass

对于类 AppBundle\Generator\SomeGenerator

  1. 类注入的容器参数全名为 app.class.generator.some

  2. 类注入到容器中为

    services: app.generator.some class: %app.class.generator.some% arguments: []

如果类后缀与根目录(相对套件目录)相同,则将被删除。

其他可能性

    Acme\Bundle\SomeBundle\Builder\SubDir\SomeBuilder 
        => acme_some.class.generator.sub_dir_some #class container parameter
        => acme_some.generator.sub_dir_some #service id

注意

如果你想自动注入新添加的类,则需要清除缓存

    php app/console cache:clear

如果类的构造函数有参数,例如

namespace AppBundle\Builder;
use Knd\Bundle\RadBundle\DependencyInjection\AutoInjectInterface;

class SomeBuilder implements AutoInjectInterface 
{
    private $em;
    private $class;
    
    public function __construct($class, $em)
    {
        $this->class = $class;
        $this->em = $em;
    }
    
    public static function getConstructorParameters()
    {
        return array(
            '%app.class.entity.product%',
            '@doctrine.orm.entity_manager'
        );
    }
}

如你所见

  1. %app.class.entity.product% - 将是容器参数
  2. @service_id - 将是服务

其他可能性

表单类型服务,类应实现 Symfony\Component\Form\FormInterface

namespace AppBundle\Form\UserType
class UserType extends AbstractType
    => app.class.form.user_type
    => app.form.type.user

实体存储库服务,类应扩展 Doctrine\ORM\EntityRepository

AppBundle\Repository\UserRepository 
    => app.class.repository.user #class container parameter
    => app.repository.user #service id

默认存储库类目录是 Repository,如果你想将存储库类与实体放在同一个目录中

//config.yml
knd_rad:
    auto_inject:
        entity:
            repository:
                dir: Entity

你也不需要指定 ORM 映射中实体类的存储库

AppBundle\Entity\User
    type: entity
    repositoryClass: xxx # you can ignore this setting

如果实体没有存储库类,将自动使用 Knd\Bundle\RadBundle\Repository\EntityRepository

你可以通过配置忽略自动功能

//config.yml
knd_rad:
    auto_inject:
        entity:
            repository:
                auto: false

实体类管理器服务,管理器类应扩展 Knd\Bundle\RadBundle\Manager\Manager

默认管理器类目录是 Manager

AppBundle\Manager\UserManager
    => app.class.manager.user #class container parameter
    => app.manager.user #service id 

如果实体没有管理器类,将自动使用 Knd\Bundle\RadBundle\Manager\Manager

你可以通过配置忽略自动功能

//config.yml
knd_rad:
    auto_inject:
        entity:
            manager:
                auto: false

模型类投票者服务,默认目录是 Security/Voter,投票者类应扩展 Knd\Bundl\RadBundle\Security\Voter\AbstractVoter

AppBundle\Security\Voter\UserVoter
    => app.class.security.voter_user_voter
    => app.security.voter.user # the service is private, you can not access it from container

##配置##

knd_rad:
    auto_inject:
        entity:
            dirs: [Entity]
            classes: []
            exclude_classes: []
            repository:
                auto: true
                dir: Repository
            manager:
                auto: true
                dir: Manager
            voter:
                auto: true
                dir: Security/Voter
        common:
            dirs: [Form]
            classes: []
            exclude_classes: []