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
### Regular-expression patterns
`class` and `method` accept either wildcard strings or explicit
`Okapi\Wildcards\Regex` objects. Each argument can use a different pattern type:
This matches controller methods whose names start with a letter, excluding
constructors and other magic methods. Regex objects can also be used for `class`.
The same arguments are supported by `Before`, `Around`, and `After`.
Pass a complete PHP regular expression, including delimiters and any modifiers.
Regex patterns are used as written: add `^` and `$` when you want to match the
whole name. Plain strings always retain wildcard semantics, even if they look
like `/regex/`. Invalid explicit regex patterns throw `InvalidArgumentException`
when the advice attribute is instantiated, identifying the affected argument.
### 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 properties through $invocation->properties(),
with an explicit declaring class when names overlap in an inheritance hierarchy
-
Access protected properties and private/protected methods of the subject
(Direct access may 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
- 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 ### Regular-expression patterns `class` and `method` accept either wildcard strings or explicit `Okapi\Wildcards\Regex` objects. Each argument can use a different pattern type: This matches controller methods whose names start with a letter, excluding constructors and other magic methods. Regex objects can also be used for `class`. The same arguments are supported by `Before`, `Around`, and `After`. Pass a complete PHP regular expression, including delimiters and any modifiers. Regex patterns are used as written: add `^` and `$` when you want to match the whole name. Plain strings always retain wildcard semantics, even if they look like `/regex/`. Invalid explicit regex patterns throw `InvalidArgumentException` when the advice attribute is instantiated, identifying the affected argument. ### 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 properties through $invocation->properties(),
with an explicit declaring class when names overlap in an inheritance hierarchy
Access protected properties and private/protected methods of the subject (Direct access may 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
TransformerManagerservice stores the list of aspects and their configuration -
The
CacheStateManagerservice manages the cache state -
The
StreamFilterservice registers a PHP Stream Filter which allows to modify the source code before it is loaded by PHP - The
AutoloadInterceptorservice overloads the Composer autoloader, which handles the loading of classes
PHP-AOP builds on CodeTransformer's autoloader, stream filters, and cache. After kernel initialization, eligible classes follow this simplified flow:
Internal classes and excluded paths bypass this flow. Debug mode disables cache reuse; development checks freshness, while production trusts existing cache state.
For weaving, the original implementation is renamed to MyClass__AopProxied, and
the generated MyClass extends that renamed implementation. Its intercepted
methods delegate to the advice machinery; callers keep using the original name.
See How PHP-AOP works for the initialization, class-loading, and method-invocation diagrams, with links to the implementation.
Testing
Development quality checks: see Mago setup and commands.
- Run
composer run-script test
or - Run
composer run-script test-coverage
For performance tests and the JIT configuration used by CI, see Performance testing.
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
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