GithubHelp home page GithubHelp logo

aop's Introduction

PUGX/AOP

Build Status

This library aims to provide a very simple and lightweight support for Aspect-Oriented Programming in PHP frameworks and libraries.

It basically provides a layer to compile aspects in classes exposed as services in a dependency injection container.

Development

AOP is currently under development, so it is not recommended for production environments.

In the pipeline, we have the following ideas:

  • evaluate how to integrate with the code/concepts provided in JMSAopBundle
  • support static compilation of the container (for example, before deployments)
  • caching
  • covering the library with more and more unit tests
  • writing compilers for more DICs

This library is experimental: we need to get our heads around the idea of AOP implemented in this way before going further with it. Development, in any case, is active.

Supported containers

Here is a list of the supported DICs:

Available aspects

Here is a list of available aspects:

Installation

Installation should be done via composer.

Here's the link of the package on Packagist.

Examples

Here is a brief recap of how to integrate AOP in your own code - you can also have a look at the scripts provided in the examples directory.

<?php

namespace Example;

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

// integrate autoloading (composer is recommended) and annotations mapping
$loader = require '/path/to/my/autoload/script.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/';

// instantiate the AOP manager with an array of aspects
$manager = new Manager(array(), $proxyDir);

// tell the manager to inject AOP in the container
$manager->enableAop($container);

That's it, now you have successfully compiled your DIC with AOP.

As you probably noticed, we passed an empty array representing the list of aspects. Read on to understand how to actually inject aspects in the container's services.

Loggable

Here is how you enable the Loggable aspect:

<?php

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

// instantiate the AOP manager, adding the Loggable aspect
$manager = new Manager(array(
    new Loggable($proxyDir)
), $proxyDir);

Now, let's see how the DIC is configured:

services:
  monolog.logger_standard:
    class: "Monolog\\Logger"
    arguments:
      name: logger
  my_example_service:
    class: "Example\\MyExampleClass"
    arguments:
      a: 1
      b: 2

At this point you just need to add the annotations to enable Loggable in the service my_example_service:

<?php

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="$this->b", when="start", with="monolog.logger_standard", as="Hey, value of MyExampleClass::b is %s")
     * @Log(what="$this->b", with="monolog.logger_standard", as="Log with context!", context="$this->b")
     * @Log(what="$this->b", when="end", with="monolog.logger_standard", as="HOLY COW! Now MyExampleClass::b is %s")
     */
    public function doSomething()
    {
        $this ->b = $this->b * 10;
    }
}

At the end, you just need to run the code to see that Monolog is actually logging thanks to the Loggable aspect:

<?php

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

aop's People

Contributors

cirpo avatar kpacha avatar odino avatar odracci avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.