Download the PHP package okapi/aop without Composer
On this page you can find all versions of the php package okapi/aop. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package aop
Short Description PHP AOP is a PHP library that provides a powerful Aspect Oriented Programming (AOP) implementation for PHP.
License MIT
Homepage https://github.com/okapi-web/php-aop
Informations about the package aop
PHP AOP
PHP AOP is a PHP library that provides a powerful Aspect Oriented Programming
(AOP) implementation for PHP.
Installation
Usage
📖 List of contents
- Terminology
- Implicit Aspects
- Create a Kernel
- Create an Aspect
- Target Classes
- Initialize the Kernel
- Result
- Class-Level Explicit Aspects
- Create an Aspect
- Target Classes
- Initialize the Kernel
- Result
- Method-Level Explicit Aspects
- Create an Aspect
- Target Classes
- Initialize the Kernel
- Result
- Features
- Limitations
- How it works
- Testing
- Contributing
Terminology
-
AOP: Aspect Oriented Programming - A programming paradigm that aims to
increase modularity by allowing the separation of cross-cutting concerns.
-
Aspect: A class that implements the logic that you want to apply to your
target classes. Aspects must be annotated with the #[Aspect]
attribute.
-
Advice: The logic that you want to apply to your target classes. Advice
methods must be annotated with the #[Before]
, #[Around]
or #[After]
attributes.
-
Join Point: A point in the execution of your target classes where you can
apply your advice. Join points are defined by the #[Before]
, #[Around]
or
#[After]
attributes.
-
Pointcut: A set of join points where you can apply your advice. Pointcuts
are defined by the #[Pointcut]
attribute.
-
Weaving: The process of applying your advice to your target classes.
-
Implicit Aspects: The aspects are applied without any modification to the
target classes. The aspect itself specifies the classes or methods it applies
to.
-
Class-Level Explicit Aspects: The aspects are applied by modifying the
target classes, typically by adding the aspect as an attribute to the target
class.
- Method-Level Explicit Aspects: The aspects are applied by modifying the
target classes, typically by adding the aspect as an attribute to the target
method.
Implicit Aspects
Click to expand
### Create a Kernel
### Create an Aspect
### Target Classes
### Initialize the Kernel
### Result
Class-Level Explicit Aspects
Click to expand
Adding the custom Aspect to the Kernel is not required for class-level explicit
aspects as they are registered automatically at runtime.
### Create an Aspect
### Target Classes
### Initialize the Kernel
### Result
Method-Level Explicit Aspects
Click to expand
Adding the custom Aspect to the Kernel is not required for method-level explicit
aspects as they are registered automatically at runtime.
### Create an Aspect
### Target Classes
### Initialize the Kernel
### Result
Features
-
Advice types: "Before", "Around" and "After"
-
Intercept "private" and "protected" methods
(Will show errors in IDEs)
-
Access "private" and "protected" properties and methods of the subject
(Will show errors in IDEs)
-
Intercept "final" methods and classes
- Use Transformers from the "Okapi/Code-Transformer" package in your Kernel
to modify and transform the source code of a loaded PHP class
(See "Okapi/Code-Transformer" package for more information)
Limitations
- Internal "private" and "protected" methods cannot be intercepted
How it works
-
This package extends the "Okapi/Code-Transformer" package with Dependency
Injection and AOP features
-
The AopKernel
registers multiple services
-
The TransformerManager
service stores the list of aspects and their
configuration
-
The CacheStateManager
service manages the cache state
-
The StreamFilter
service registers a
PHP Stream Filter
which allows to modify the source code before it is loaded by PHP
- The
AutoloadInterceptor
service overloads the Composer autoloader,
which handles the loading of classes
General workflow when a class is loaded
-
The AutoloadInterceptor
service intercepts the loading of a class
-
The AspectMatcher
matches the class and method names with the list of
aspects and their configuration
-
If the class and method names match an aspect, query the cache state to see
if the source code is already cached
- Check if the cache is valid:
- Modification time of the caching process is less than the modification
time of the source file or the aspect file
-
Check if the cache file, the source file and the aspect file exist
- If the cache is valid, load the proxied class from the cache
- If not, return a stream filter path to the
AutoloadInterceptor
service
- The
StreamFilter
modifies the source code by applying the aspects
- Convert the original source code to a proxied class
(MyClass -> MyClass__AopProxied)
- The proxied class should have the same amount of lines as the original
class (because the debugger will point to the original class)
- The proxied class extends a woven class which contains the logic of applying
the aspects
- The woven class will be included at the bottom of the proxied class
- The woven class will also be cached
Testing
- Run
composer run-script test
or
- Run
composer run-script test-coverage
Contributing
- To contribute to this project, fire up an aspect in any application
that works or has 100% working tests, and match every class and method with
'*' with any advice type.
- If the application throws an error, then it's a bug.
- Example:
Show your support
- Create a Kernel
- Create an Aspect
- Target Classes
- Initialize the Kernel
- Result
- Create an Aspect
- Target Classes
- Initialize the Kernel
- Result
- Create an Aspect
- Target Classes
- Initialize the Kernel
- Result
AOP: Aspect Oriented Programming - A programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.
Aspect: A class that implements the logic that you want to apply to your
target classes. Aspects must be annotated with the #[Aspect]
attribute.
Advice: The logic that you want to apply to your target classes. Advice
methods must be annotated with the #[Before]
, #[Around]
or #[After]
attributes.
Join Point: A point in the execution of your target classes where you can
apply your advice. Join points are defined by the #[Before]
, #[Around]
or
#[After]
attributes.
Pointcut: A set of join points where you can apply your advice. Pointcuts
are defined by the #[Pointcut]
attribute.
Weaving: The process of applying your advice to your target classes.
Implicit Aspects: The aspects are applied without any modification to the target classes. The aspect itself specifies the classes or methods it applies to.
Class-Level Explicit Aspects: The aspects are applied by modifying the target classes, typically by adding the aspect as an attribute to the target class.
Click to expand
### Create a Kernel ### Create an Aspect ### Target Classes ### Initialize the Kernel ### ResultClick to expand
Adding the custom Aspect to the Kernel is not required for class-level explicit aspects as they are registered automatically at runtime. ### Create an Aspect ### Target Classes ### Initialize the Kernel ### ResultClick to expand
Adding the custom Aspect to the Kernel is not required for method-level explicit aspects as they are registered automatically at runtime. ### Create an Aspect ### Target Classes ### Initialize the Kernel ### ResultAdvice types: "Before", "Around" and "After"
Intercept "private" and "protected" methods (Will show errors in IDEs)
Access "private" and "protected" properties and methods of the subject (Will show errors in IDEs)
Intercept "final" methods and classes
This package extends the "Okapi/Code-Transformer" package with Dependency Injection and AOP features
The AopKernel
registers multiple services
-
The
TransformerManager
service stores the list of aspects and their configuration -
The
CacheStateManager
service manages the cache state -
The
StreamFilter
service registers a PHP Stream Filter which allows to modify the source code before it is loaded by PHP - The
AutoloadInterceptor
service overloads the Composer autoloader, which handles the loading of classes
The AutoloadInterceptor
service intercepts the loading of a class
The AspectMatcher
matches the class and method names with the list of
aspects and their configuration
If the class and method names match an aspect, query the cache state to see if the source code is already cached
- Check if the cache is valid:
- Modification time of the caching process is less than the modification time of the source file or the aspect file
-
Check if the cache file, the source file and the aspect file exist
- If the cache is valid, load the proxied class from the cache
- If not, return a stream filter path to the
AutoloadInterceptor
service
StreamFilter
modifies the source code by applying the aspects
- Convert the original source code to a proxied class (MyClass -> MyClass__AopProxied)
- The proxied class should have the same amount of lines as the original class (because the debugger will point to the original class)
- The proxied class extends a woven class which contains the logic of applying the aspects
- The woven class will be included at the bottom of the proxied class
- The woven class will also be cached
composer run-script test
or
composer run-script test-coverage
Give a ⭐ if this project helped you!
🙏 Thanks
- Big thanks to lisachenko for their pioneering work on the Go! Aspect-Oriented Framework for PHP. This project drew inspiration from their innovative approach and served as a foundation for this project.
📝 License
Copyright © 2023 Valentin Wotschel.
This project is MIT licensed.
All versions of aop with dependencies
nette/php-generator Version ^4.0
okapi/code-transformer Version 1.3.7
okapi/wildcards Version ^1.0
okapi/singleton Version ^1.0
php-di/php-di Version ^7.0