子集 / aop-feature-bundle
Symfony 2 的 AOP 功能
v1.0.0-beta.1
2013-10-07 08:09 UTC
Requires
- php: >=5.3.3
- jms/aop-bundle: >=1.0.0
Requires (Dev)
- doctrine/doctrine-bundle: >=1.2.0
- doctrine/orm: >=2.2,<2.5-dev
Suggests
- doctrine/doctrine-bundle: The Doctrine Symfony bundle.
- doctrine/orm: The Doctrine ORM integration is optional in the bundle.
This package is not auto-updated.
Last update: 2024-09-23 15:25:48 UTC
README
这是 Symfony 框架的扩展,用于面向方面编程。它增加了为方法指定方面的功能,从而减少了应用程序中的横切代码。
方面
该扩展提供了一系列预定义的方面。
日志记录
用于方法调用的日志记录方面。
<?php use SibSet\Bundle\AopFeatureBundle\Annotation as Aspect; /** * @Aspect\Logging("log.message.writer.user.create") */ public function createUser(User $user) { // ... }
在注解中需要指定依赖容器中的服务标识符。该服务必须是 SibSet\Bundle\AopFeatureBundle\Aspect\Logging\AbstractWriter
类的子类。
事务
为方法使用事务
<?php use SibSet\Bundle\AopFeatureBundle\Annotation as Aspect; /** * @Aspect\Transactional */ public function createUser(User $user) { $this->getManager()->persist($user); $this->getManager()->flush($user); }
方法调用将被包裹在 try...catch 块中,在 try 中调用 commit(),在 catch 中调用 rollback()。
抑制异常
在方法中抑制异常,与日志记录结合使用很有用
<?php use SibSet\Bundle\AopFeatureBundle\Annotation as Aspect; /** * @Aspect\SuppressException */ public function createUser(User $user) { throw new \Exception("All is dust!"); }
可以指定要捕获的具体异常类型
/**
* @Aspect\SuppressException("\RuntimeException")
*/
public function createUser(User $user)
安装
Composer
将依赖项添加到项目文件 composer.json 中
"require": {
# ..
"sibset/aop-feature-bundle": ">=1.0.0"
# ..
}
AppKernel.php
<?php public function registerBundles() { $bundles = array( // ... new JMS\AopBundle\JMSAopBundle(), new SibSet\Bundle\SibSetAopBundle(), ); // ... return $bundles; }