2017 © Pedro Peláez
 

library aop

An aspect oriented framework

image

ray/aop

An aspect oriented framework

  • Monday, July 30, 2018
  • by koriym
  • Repository
  • 10 Watchers
  • 56 Stars
  • 79,610 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 13 Forks
  • 0 Open issues
  • 48 Versions
  • 8 % Grown

The README.md

Ray.Aop

Aspect Oriented Framework

Scrutinizer Code Quality codecov Type Coverage Continuous Integration Total Downloads, (*1)

ray-di logo, (*2)

[Japanese], (*3)

Ray.Aop package provides method interception. This feature enables you to write code that is executed each time a matching method is invoked. It's suited for cross cutting concerns ("aspects"), such as transactions, security and logging. Because interceptors divide a problem into aspects rather than objects, their use is called Aspect Oriented Programming (AOP)., (*4)

A Matcher is a simple interface that either accepts or rejects a value. For Ray.AOP, you need two matchers: one that defines which classes participate, and another for the methods of those classes. To make this easy, there's factory class to satisfy the common scenarios., (*5)

MethodInterceptors are executed whenever a matching method is invoked. They have the opportunity to inspect the call: the method, its arguments, and the receiving instance. They can perform their cross-cutting logic and then delegate to the underlying method. Finally, they may inspect the return value or exception and return. Since interceptors may be applied to many methods and will receive many calls, their implementation should be efficient and unintrusive., (*6)

Example: Forbidding method calls on weekends

To illustrate how method interceptors work with Ray.Aop, we'll forbid calls to our pizza billing system on weekends. The delivery guys only work Monday thru Friday so we'll prevent pizza from being ordered when it can't be delivered! This example is structurally similar to use of AOP for authorization., (*7)

To mark select methods as weekdays-only, we define an attribute., (*8)

<?php
#[Attribute(Attribute::TARGET_METHOD)]
final class NotOnWeekends
{
}

...and apply it to the methods that need to be intercepted:, (*9)

<?php
class RealBillingService
{
    #[NotOnWeekends] 
    public function chargeOrder(PizzaOrder $order, CreditCard $creditCard)
    {

Next, we define the interceptor by implementing the org.aopalliance.intercept.MethodInterceptor interface. When we need to call through to the underlying method, we do so by calling $invocation->proceed():, (*10)

<?php
class WeekendBlocker implements MethodInterceptor
{
    public function invoke(MethodInvocation $invocation)
    {
        $today = getdate();
        if ($today['weekday'][0] === 'S') {
            throw new \RuntimeException(
                $invocation->getMethod()->getName() . " not allowed on weekends!"
            );
        }
        return $invocation->proceed();
    }
}

Finally, we configure everything using the Aspect class:, (*11)

use Ray\Aop\Aspect;
use Ray\Aop\Matcher;

$aspect = new Aspect();
$aspect->bind(
    (new Matcher())->any(),
    (new Matcher())->annotatedWith(NotOnWeekends::class),
    [new WeekendBlocker()]
);

$billing = $aspect->newInstance(RealBillingService::class);
try {
    echo $billing->chargeOrder(); // Interceptors applied
} catch (\RuntimeException $e) {
    echo $e->getMessage() . "\n";
    exit(1);
}

Putting it all together, (and waiting until Saturday), we see the method is intercepted and our order is rejected:, (*12)

chargeOrder not allowed on weekends!

PECL Extension

Ray.Aop also supports a PECL extension. When the extension is installed, you can use the weave method to apply aspects to all classes in a directory:, (*13)

$aspect = new Aspect();
$aspect->bind(
    (new Matcher())->any(),
    (new Matcher())->annotatedWith(NotOnWeekends::class),
    [new WeekendBlocker()]
);
// Weave aspects into all matching classes in the source directory
$aspect->weave('/path/to/src');

// Or weave into specific target directory
$aspect->weave('/path/to/target');

$billing = new RealBillingService();
echo $billing->chargeOrder(); // Interceptors applied

With the PECL extension: - You can create new instances anywhere in your code using the normal new keyword. - Interception works even with final classes and methods., (*14)

To use these features, simply install the PECL extension and Ray.Aop will automatically utilize it when available. PHP 8.1+ is required for the PECL extension., (*15)

Installing the PECL extension

PHP 8.1 or higher is required to use the PECL extension. For more information, see ext-rayaop., (*16)

Configuration Options

When creating an Aspect instance, you can optionally specify a temporary directory:, (*17)

$aspect = new Aspect('/path/to/tmp/dir');

If not specified, the system's default temporary directory will be used., (*18)

This concludes the basic usage of Ray.Aop. For more detailed information and advanced usage, please refer to the full documentation., (*19)

Own matcher

You can have your own matcher. To create contains matcher, You need to provide a class which have two method. One is matchesClass for class match. The other one is matchesMethod method match. Both return the boolean result of matched., (*20)

use Ray\Aop\AbstractMatcher;
use Ray\Aop\Matcher;

class IsContainsMatcher extends AbstractMatcher
{
    /**
     * {@inheritdoc}
     */
    public function matchesClass(\ReflectionClass $class, array $arguments) : bool
    {
        [$contains] = $arguments;

        return (strpos($class->name, $contains) !== false);
    }

    /**
     * {@inheritdoc}
     */
    public function matchesMethod(\ReflectionMethod $method, array $arguments) : bool
    {
        [$contains] = $arguments;

        return (strpos($method->name, $contains) !== false);
    }
}

Interceptor Details

In an interceptor, a MethodInvocation object is passed to the invoke method:, (*21)

class MyInterceptor implements MethodInterceptor
{
    public function invoke(MethodInvocation $invocation)
    {
        // Before method invocation
        $result = $invocation->proceed();
        // After method invocation
        return $result;
    }
}

$invocation->proceed() invokes the next interceptor in the chain. If no more interceptors are present, it calls the target method. This chaining allows multiple interceptors for a single method, executing in the order bound., (*22)

Example execution flow for interceptors A, B, and C:, (*23)

  1. Interceptor A (before)
  2. Interceptor B (before)
  3. Interceptor C (before)
  4. Target method
  5. Interceptor C (after)
  6. Interceptor B (after)
  7. Interceptor A (after)

This chaining mechanism allows you to combine multiple cross-cutting concerns (like logging, security, and performance monitoring) for a single method., (*24)

With the MethodInvocation object, you can:, (*25)

/** @var $method \Ray\Aop\ReflectionMethod */
$method = $invocation->getMethod();
/** @var $class \Ray\Aop\ReflectionClass */
$class = $invocation->getMethod()->getDeclaringClass();
  • $method->getAnnotations() - Get method attributes/annotations
  • $method->getAnnotation($name) - Get method attribute/annotation
  • $class->->getAnnotations() - Get class attributes/annotations
  • $class->->getAnnotation($name) - Get class attributes/annotation

Annotation/Attribute

Ray.Aop can be used either with doctrine/annotation in PHP 7/8 or with an Attributes in PHP8., (*26)

AOP Alliance

The method interceptor API implemented by Ray.Aop is a part of a public specification called AOP Alliance., (*27)

Installation

The recommended way to install Ray.Aop is through Composer., (*28)

# Add Ray.Aop as a dependency
$ composer require ray/aop ^2.0
SevericeLocator::setReader(new AttributeReader);`

Integrated DI framework

  • See also the DI framework Ray.Di which integrates DI and AOP.

Stability

Ray.Aop follows semantic versioning and ensures backward compatibility. Released in 2015, version 2.0 and its successors have maintained compatibility while evolving with PHP, and we remain committed to this stability., (*29)


  • Note: This documentation of the part is taken from Guice/AOP. z1

The Versions

30/07 2018

2.x-dev

2.9999999.9999999.9999999-dev

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

15/07 2018

2.7.6

2.7.6.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

16/06 2018

2.7.5

2.7.5.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

16/06 2018

2.7.4

2.7.4.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

25/05 2018

2.7.3

2.7.3.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

25/05 2018

dev-multi

dev-multi

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

08/05 2018

2.7.2

2.7.2.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

27/04 2018

2.7.1

2.7.1.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

25/04 2018

2.7.0

2.7.0.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

02/03 2018

2.6.0

2.6.0.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

02/03 2018

dev-annotation-loader

dev-annotation-loader

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

02/03 2018

2.5.2

2.5.2.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

01/03 2018

dev-revert

dev-revert

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

01/03 2018

2.5.1

2.5.1.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

20/08 2017

2.5.0

2.5.0.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

12/07 2017

2.4.2

2.4.2.0

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

The Development Requires

aop

28/08 2016

2.4.1

2.4.1.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

10/08 2016

2.4.0

2.4.0.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

01/07 2016

2.3.3

2.3.3.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

23/05 2016

2.3.2

2.3.2.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

01/05 2016

2.3.1

2.3.1.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

17/02 2016

2.3.0

2.3.0.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

16/02 2016

2.2.0

2.2.0.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

MIT

The Requires

 

annotations aop aop alliance code-gen

27/05 2015

2.1.1

2.1.1.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop aop alliance code-gen

26/04 2015

2.1.0

2.1.0.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop aop alliance code-gen

18/04 2015

2.0.1

2.0.1.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop aop alliance code-gen

28/02 2015

2.0.0

2.0.0.0 https://github.com/koriym/Ray.Aop

An aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop aop alliance code-gen

21/02 2015

2.0.0-beta.3

2.0.0.0-beta3 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop method invocation code gen aop alliance

16/02 2015

2.0.0-beta.2

2.0.0.0-beta2 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop method invocation code gen aop alliance

11/02 2015

2.0.0-beta

2.0.0.0-beta https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop method invocation code gen aop alliance

16/01 2015

2.0.0-alpha.3

2.0.0.0-alpha3 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop method invocation code gen aop alliance

13/01 2015

2.0.0-alpha.2

2.0.0.0-alpha2 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop method invocation code gen aop alliance

22/12 2014

2.0.0-alpha

2.0.0.0-alpha https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotations aop method invocation code gen aop alliance

21/10 2014

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

29/09 2014

1.3.1

1.3.1.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

19/08 2014

1.3.0

1.3.0.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

14/08 2014

1.2.7

1.2.7.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

16/07 2014

1.2.6

1.2.6.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

27/05 2014

1.2.5

1.2.5.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

19/05 2014

1.2.4

1.2.4.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

26/02 2014

1.2.3

1.2.3.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

18/01 2014

1.2.2

1.2.2.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

08/12 2013

1.2.1

1.2.1.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

07/12 2013

1.2.0

1.2.0.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

22/09 2013

1.1.2

1.1.2.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

16/09 2013

1.1.1

1.1.1.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

15/09 2013

1.1.0

1.1.0.0 https://github.com/koriym/Ray.Aop

Aspect oriented framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect

31/03 2013

1.0.0

1.0.0.0 https://github.com/koriym/Ray.Aop

Aspect oriented programing framework

  Sources   Download

BSD-3-Clause

The Requires

 

annotation aop aspect method interception