All Projects → ray-di → Ray.aop

ray-di / Ray.aop

Licence: mit
An aspect-oriented framework for PHP

Projects that are alternatives of or similar to Ray.aop

Agentframework
An elegant & efficient TypeScript metaprogramming API to build software agents
Stars: ✭ 97 (+44.78%)
Mutual labels:  interceptor, aop
Cauldron
C# Toolkit
Stars: ✭ 68 (+1.49%)
Mutual labels:  interceptor, aop
Cauldron
C# Toolkit
Stars: ✭ 27 (-59.7%)
Mutual labels:  interceptor, aop
Aspect4Delphi
Concepts of aspect-oriented programming (AOP) in Delphi.
Stars: ✭ 28 (-58.21%)
Mutual labels:  interceptor, aop
Framework
💎 Go! AOP PHP - modern aspect-oriented framework for the new level of software development
Stars: ✭ 1,559 (+2226.87%)
Mutual labels:  interceptor, aop
Blockhook
Hook Objective-C blocks. A powerful AOP tool.
Stars: ✭ 742 (+1007.46%)
Mutual labels:  interceptor, aop
Stinger
Stinger is a high-efficiency library with great compatibility, for aop in Objective-C, using libffi instead of Objective-C message forwarding. It is 20+ times faster than the Aspects, from message-sending to Aspect-oriented code ends.
Stars: ✭ 845 (+1161.19%)
Mutual labels:  aop
Axios Module
Secure and easy axios integration with Nuxt.js
Stars: ✭ 998 (+1389.55%)
Mutual labels:  interceptor
Frankie
A frankenstein framework - middleware and annotation based
Stars: ✭ 19 (-71.64%)
Mutual labels:  aop
Aspekt
A lightweight (Aspect Oriented Programming) AOP foundation
Stars: ✭ 18 (-73.13%)
Mutual labels:  aop
Spring Examples
SpringBoot Examples
Stars: ✭ 67 (+0%)
Mutual labels:  aop
Eshop Soa
EShop基于Dubbo实现SOA服务化拆分,并基于RocketMQ解决了分布式事务(新版SpringBootSOASkeleton)
Stars: ✭ 65 (-2.99%)
Mutual labels:  aop
Go Grpc Prometheus
Prometheus monitoring for your gRPC Go servers.
Stars: ✭ 965 (+1340.3%)
Mutual labels:  interceptor
Conskit
A Clojure Application Toolkit
Stars: ✭ 9 (-86.57%)
Mutual labels:  interceptor
Easycaching
💥 EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
Stars: ✭ 1,047 (+1462.69%)
Mutual labels:  interceptor
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (-65.67%)
Mutual labels:  interceptor
Androidlearn
Android Custom Views
Stars: ✭ 66 (-1.49%)
Mutual labels:  aop
Spring Boot
spring-boot 项目实践总结
Stars: ✭ 989 (+1376.12%)
Mutual labels:  aop
Goaop Symfony Bundle
[Outdated!] Integration bridge for Go! AOP framework and Symfony
Stars: ✭ 59 (-11.94%)
Mutual labels:  aop
Broxy
An HTTP/HTTPS intercept proxy written in Go.
Stars: ✭ 912 (+1261.19%)
Mutual labels:  interceptor

Ray.Aop

Aspect Oriented Framework

Scrutinizer Code Quality Code Coverage Build Status Total Downloads

[Japanese]

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).

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.

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.

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.

To mark select methods as weekdays-only, we define an annotation. (Ray.Aop uses Doctrine Annotations)

<?php
/**
 * NotOnWeekends
 *
 * @Annotation
 * @Target("METHOD")
 */
final class NotOnWeekends
{
}

...and apply it to the methods that need to be intercepted:

<?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():

<?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. In this case we match any class, but only the methods with our @NotOnWeekends annotation:

<?php

use Ray\Aop\Sample\Annotation\NotOnWeekends;
use Ray\Aop\Sample\Annotation\RealBillingService;

$pointcut = new Pointcut(
    (new Matcher)->any(),
    (new Matcher)->annotatedWith(NotOnWeekends::class),
    [new WeekendBlocker]
);
$bind = (new Bind)->bind(RealBillingService::class, [$pointcut]);
$billing = (new Weaver($bind, $tmpDir))->newInstance(RealBillingService::class, [], $bind);

try {
    echo $billing->chargeOrder();
} 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:

chargeOrder not allowed on weekends!

Explicit method name match

<?php
    $bind = (new Bind)->bindInterceptors('chargeOrder', [new WeekendBlocker]);
    $compiler = new Weaver($bind, $tmpDir);
    $billing = $compiler->newInstance('RealBillingService', [], $bind);
    try {
        echo $billing->chargeOrder();
    } catch (\RuntimeException $e) {
        echo $e->getMessage() . "\n";
        exit(1);
    }

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.

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

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

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

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

        return (strpos($method->name, $contains) !== false);
    }
}
$pointcut = new Pointcut(
    (new Matcher)->any(),
    new IsContainsMatcher('charge'),
    [new WeekendBlocker]
);
$bind = (new Bind)->bind(RealBillingService::class, [$pointcut]);
$billing = (new Weaver($bind, $tmpDir))->newInstance(RealBillingService::class, [$arg1, $arg2]);

Performance boost

Cached Weaver object can save the compiling, binding, annotation reading costs.

$weaver = unserialize(file_get_contentes('./serializedWever'));
$billing = (new Weaver($bind, $tmpDir))->newInstance(RealBillingService::class, [$arg1, $arg2]);

Priority

The order of interceptor invocation are determined by following rules.

  • Basically, it will be invoked in bind order.
  • PriorityPointcut has most priority.
  • Annotation method match is followed by PriorityPointcut. Invoked in annotation order as follows.
/**
 * @Auth    // 1st
 * @Cache   // 2nd
 * @Log     // 3rd
 */

Limitations

Behind the scenes, method interception is implemented by generating code at runtime. Ray.Aop dynamically creates a subclass that applies interceptors by overriding methods.

This approach imposes limits on what classes and methods can be intercepted:

  • Classes must be non-final
  • Methods must be public

Interceptor

In an interceptor a MethodInvocation object gets passed to the invoke method. We can the decorate the targetted instances so that you run computations before or after any methods on the target are invoked.

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

With the MethodInvocation object, you can access the target method's invocation object, method's and parameters.

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

AOP Alliance

The method interceptor API implemented by Ray.Aop is a part of a public specification called AOP Alliance.

Installation

The recommended way to install Ray.Aop is through Composer.

# Add Ray.Aop as a dependency
$ composer require ray/aop ^2.0

Testing Ray.Aop

Here's how to install Ray.Aop from source and run the unit tests and demos.

git clone https://github.com/ray-di/Ray.Aop.git
cd Ray.Aop
composer install
vendor/bin/phpunit
php demo/run.php

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

  • This documentation for the most part is taken from Guice/AOP.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].