c33s/propel-di-behavior-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

c33s/propel-di-behavior-bundle

最新稳定版本:v0.1.1

Composer 安装命令:

composer require c33s/propel-di-behavior-bundle

包简介

Clean Symfony2 dependency injection for your Propel models

README 文档

README

Clean Symfony2 dependency injection for your Propel models

Installation

This bundle depends on GlorpenPropelBundle. Install that one first.

Require c33s/propel-di-behavior-bundle in your composer.json file:

{
    "require": {
        "c33s/propel-di-behavior-bundle": "@stable",
    }
}

Add propel behaviors to your propel config:

# app/config/config.yml

propel:
    # ...
    
    behaviors:
        c33s_di:        vendor.c33s.propel-di-behavior-bundle.src.C33sPropelDependencyCollectorBehavior
        
        # Optional: add another "instance" for global usage that does not interfere with model-specific behavior instances
        c33s_di_global: vendor.c33s.propel-di-behavior-bundle.src.C33sPropelDependencyCollectorBehavior

Register the bundle in app/AppKernel.php:

    // app/AppKernel.php

    public function registerBundles()
    {
        return array(
            // ...

            new C33s\PropelDIBehaviorBundle\C33sPropelDIBehaviorBundle(),
        );
    }

Usage

Add behavior to your propel models - either globally (use the c33s_di_global name for that) or to a specific model. You may inject Symfony2 services or parameters into any Model or Query class. Each definition consists of the service or parameter name to inject (enclose parameters in %-chars) followed by optional getter methods and type hints for this methods, separated by colons:

  • logger:getLogger:\Psr\Log\LoggerInterface injects the Symfony2 logger service, making it accessible by $model->getLogger() (or $query->getLogger()) and providing \Psr\Log\LoggerInterface as a type hint for that getter
  • logger injects the Symfony2 logger service without an explicit getter. Use $model->getInjectedDependency('logger') to access it.
  • %locale%:getLocale:string injects the locale parameter, providing a getLocale() method for accessing it.

Example schema

<!-- my/Bundle/Resources/config/schema.xml -->

    <!-- this will inject the logger into ALL Propel model and query instances -->
    <behavior name="c33s_di_global">
        <parameter name="model" value="logger:getLogger:\Psr\Log\LoggerInterface" />
        <parameter name="query" value="logger:getLogger:\Psr\Log\LoggerInterface" />
    </behavior>

    <table name="book">
        <!-- this will inject the mailer and session into the Book instances and request_stack into BookQuery instances -->
        <behavior name="c33s_di">
            <parameter name="model" value="
                mailer:getMailer:\Swift_Mailer,
                session:getSession:\Symfony\Component\HttpFoundation\Session\Session,
            " />
            <parameter name="query" value="request_stack:getRequestStack:\Symfony\Component\HttpFoundation\RequestStack" />
        </behavior>
        
        <...>
    </table>

How it works

C33sPropelDIBehaviorBundle registers event listeners for model.create and query.create that are processed by GlorpenPropelBundle. Upon each model or query creation anonymous callbacks for each service will be injected into the classes, making sure that only the services that were specified in the schema can be accessed. The callback furthermore ensures that no services will be instantiated without actually being used.

So far this is the cleanest way I have found to inject specific Symfony2 dependencies into Propel models without messing around with the full DI container.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固