pugx/aop

PHP的面向切面编程日志功能。

维护者

详细信息

github.com/pugx/aop

源代码

问题

0.0.1 2013-05-31 20:40 UTC

This package is not auto-updated.

Last update: 2024-09-10 03:44:43 UTC


README

Build Status

这个库旨在为PHP框架和库提供非常简单轻量级的面向切面编程(AOP)支持。

它基本上提供了一个层,用于编译在依赖注入容器中作为服务暴露的类中的方面。

开发

AOP目前处于开发阶段,因此不建议在生产环境中使用。

在计划中,我们有以下想法

  • 评估如何与JMSAopBundle中提供的代码/概念集成
  • 支持容器(例如,在部署之前)的静态编译
  • 缓存
  • 用越来越多的单元测试覆盖库
  • 为更多的DIC编写编译器

这个库是实验性的:在进一步推进之前,我们需要了解以这种方式实现的AOP的概念。无论如何,开发是活跃的。

支持的容器

以下是一个支持的DIC列表

可用方面

以下是一个可用方面的列表

安装

安装应通过composer完成。

这里是Packagist上的包链接

示例

以下是简要概述如何在您的代码中集成AOP - 您也可以查看examples目录中提供的脚本。

<?php

namespace Example;

// import the required namespaces
use Doctrine\Common\Annotations\AnnotationReader;
use PUGX\AOP\Aspect\LoggableGenerator;
use PUGX\AOP\DependencyInjection\Compiler\Symfony2;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use PUGX\AOP\Aspect\Loggable;
use Doctrine\Common\Annotations\AnnotationRegistry;

// integrate autoloading with composer and annotations mapping
$loader = require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

// instantiate the Symfony2 DIC
$container  = new ContainerBuilder();
$loader     = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load(__DIR__ . DIRECTORY_SEPARATOR . 'container.yml');

// define a directory where the proxy classes - containing the aspects - will be generated
$proxyDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR . 'proxy/';

require 'MyClassExample.php';

$symfony2Compiler = new Symfony2(new AnnotationReader(), $proxyDir, '\PUGX\AOP\Aspect\BaseAnnotation', array());
$container->addCompilerPass($symfony2Compiler);
$container->compile();

就是这样,现在您已成功编译了包含AOP的DIC。

正如您可能已经注意到的,我们传递了一个表示方面列表的空数组。继续阅读以了解如何在容器服务中实际注入方面。

可记录

以下是启用Loggable方面的方法

<?php

// Enable the Loggable aspect for all classes in the container that has @Log annotation
$symfony2Compiler = new Symfony2(new AnnotationReader(), $proxyDir, '\PUGX\AOP\Aspect\BaseAnnotation', array('loggable'));
$container->addCompilerPass($symfony2Compiler);
$container->compile();

现在,让我们看看DIC的配置

services:
  loggable:
    class: "PUGX\\AOP\\Aspect\\Loggable\\Loggable"
    arguments: ["@service_container"]
  monolog.logger_standard:
    class: "Monolog\\Logger"
    arguments:
      name: logger
  my_example_service:
    class: "Example\\MyExampleClass"
    arguments:
      a: 1
      b: 2

在此阶段,您只需将注释添加到服务my_example_service中,以启用Loggable

<?php

namespace Example;

// import the Loggable aspect as Log
use PUGX\AOP\Aspect\Loggable\Annotation as Log;

class MyExampleClass
{
    protected $a;
    protected $b;

    /**
     * @Log(what="$a", when="start", with="monolog.logger_standard", as="Hey, Im getting %s as first argument")
     */
    public function __construct($a, $b)
    {
        $this->a = $a;
        $this->b = $b;
    }

    /**
     * @Log(what="$c", when="start", with="monolog.logger_standard", as="argument $c is %s")
     * @Log(what="$this->b", when="start", with="monolog.logger_standard", as="Hey, value of MyExampleClass::b is %s")
     * @Log(what="$this->b", when="end", with="monolog.logger_standard", as="HOLY COW! Now MyExampleClass::b is %s")
     * @\PUGX\AOP\Stub\MyAnnotation
     */
    public function doSomething($c)
    {
        $this->b = $this->b * 10 + (int) $c;
    }
}

最后,只需运行代码即可看到Monolog实际上是通过Loggable方面进行记录的

<?php

$myExampleService = $container->get('my_example_service');
$myExampleService->doSomething(5);