Download the PHP package ray/aop without Composer
On this page you can find all versions of the php package ray/aop. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package aop
Ray.Aop
Aspect Oriented Framework
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 attribute.
...and apply it to the methods that need to be intercepted:
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()
:
Finally, we configure everything. In this case we match any class, but only the methods with our @NotOnWeekends
annotation:
Putting it all together, (and waiting until Saturday), we see the method is intercepted and our order is rejected:
Explicit method name match
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.
The matcher supports reading doctrine annotations or PHP8 attributes.
Performance boost
Cached Weaver
object can save the compiling, binding, annotation reading costs.
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.
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.
With the MethodInvocation
object, you can access the target method's invocation object, method's and parameters.
$invocation->proceed()
- Invoke method$invocation->getMethod()
- Get method reflection$invocation->getThis()
- Get object$invocation->getArguments()
- Get parameters-
$invocation->getNamedArguments()
- Get named parameters An extendedClassRefletion
andMethodReflection
holds methos to get annotation(s). $method->getAnnotations()
- Get method annotations$method->getAnnotation($name)
- Get method annotation$class->->getAnnotations()
- Get class annotations$class->->getAnnotation($name)
- Get class annotation
Annotation/Attribute
Ray.Aop can be used either with doctrine/annotation in PHP 7/8 or with an Attributes in PHP8. See the annotation code examples in the older README(v2.9).
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.
Performance
Compilation of the AOP class allows Ray.Aop to run faster. Annotations are only loaded at first compile time, so they do not affect runtime performance. During the development phase and even at first runtime, PHP files are cached using the file timestamps, so normally you do not need to worry about the cost of annotation generation, but by setting up an annotation reader in the application bootstrap, first-time compile time performance. This setting is especially useful for large applications.
APCu
PHP8 attributes only (recommended)
Integrated DI framework
- See also the DI framework Ray.Di which integrates DI and AOP.
- Note: This documentation of the most part is taken from Guice/AOP.
All versions of aop with dependencies
ext-hash Version *
ext-tokenizer Version *
doctrine/annotations Version ^1.12 || ^2.0
koriym/attributes Version ^1.0.3