|
| 1 | +# How PHP-AOP works |
| 2 | + |
| 3 | +PHP-AOP extends CodeTransformer's class-loading and source-transformation pipeline. |
| 4 | +CodeTransformer supplies the kernel lifecycle, dependency injection, transformer |
| 5 | +management, stream filters, and cache infrastructure. PHP-AOP adds aspect matching, |
| 6 | +class weaving, and advice execution. |
| 7 | + |
| 8 | +These diagrams describe the current implementation. They use Mermaid, which GitHub |
| 9 | +renders directly inside Markdown. This page separates initialization, class loading, |
| 10 | +and method execution because they happen at different times. |
| 11 | + |
| 12 | +## 1. Initialize the kernel |
| 13 | + |
| 14 | +Call your kernel's `init()` before loading the classes you want to intercept. |
| 15 | + |
| 16 | +```mermaid |
| 17 | +flowchart TD |
| 18 | + Start["Application calls MyKernel::init()"] --> Exists{"Kernel instance exists?"} |
| 19 | + Exists -->|Yes| Kernel["Use the kernel instance"] |
| 20 | + Exists -->|No| DI["CodeTransformer: register dependency injection"] |
| 21 | + DI --> Decorate["AOP: provide specialized loader, processor, managers and cache services"] |
| 22 | + Decorate --> Create["Create the kernel through dependency injection"] |
| 23 | + Create --> Kernel |
| 24 | + Kernel --> Initialized{"Already initialized?"} |
| 25 | + Initialized -->|Yes| Repeat["Return, or throw if double initialization is forbidden"] |
| 26 | + Initialized -->|No| Configure["Collect aspects, transformers and options; run configureOptions()"] |
| 27 | + Configure --> Aspects["AOP: instantiate aspects and register their advice"] |
| 28 | + Aspects --> Services["CodeTransformer: register options, transformers and cache state"] |
| 29 | + Services --> Filters["CodeTransformer: register transformation and cached-source filters"] |
| 30 | + Filters --> Loader["Install the AOP-aware Composer class loader"] |
| 31 | + Loader --> Ready["Mark the kernel initialized"] |
| 32 | +``` |
| 33 | + |
| 34 | +The specialization uses service decoration: AOP's class loader extends |
| 35 | +CodeTransformer's loader, and `AspectProcessor` extends `TransformerProcessor`. |
| 36 | +The aspect and transformer managers can use the application's custom dependency |
| 37 | +injection callback to construct components. |
| 38 | + |
| 39 | +Implementation: [AopKernel](../src/AopKernel.php), plus the inherited |
| 40 | +`CodeTransformerKernel` lifecycle from the `okapi/code-transformer` dependency. |
| 41 | + |
| 42 | +## 2. Load a class |
| 43 | + |
| 44 | +The AOP loader first asks Composer for the source file. Internal framework classes |
| 45 | +and excluded paths bypass matching and transformation. |
| 46 | + |
| 47 | +```mermaid |
| 48 | +flowchart TD |
| 49 | + Request["PHP requests a class"] --> Locate["Composer locates its source file"] |
| 50 | + Locate --> Found{"File found?"} |
| 51 | + Found -->|No| Missing["Return false to the autoload chain"] |
| 52 | + Found -->|Yes| Bypass{"Internal class or excluded path?"} |
| 53 | + Bypass -->|Yes| Original["Load original source"] |
| 54 | + Bypass -->|No| Cache{"Cache state reusable?"} |
| 55 | + Cache -->|Yes| CachedFile{"Cached source path available?"} |
| 56 | + CachedFile -->|No| Original |
| 57 | + CachedFile -->|Yes| Cached["Load cached source through the cached-source filter"] |
| 58 | + Cache -->|No| Match["Match AOP advice and CodeTransformer transformers"] |
| 59 | + Match --> Any{"Anything matched?"} |
| 60 | + Any -->|No| Original |
| 61 | + Any -->|Yes| Filter["Load through CodeTransformer's transformation stream filter"] |
| 62 | + Filter --> Transform["Run matched transformers first"] |
| 63 | + Transform --> Advice{"Advice matched and class eligible for weaving?"} |
| 64 | + Advice -->|Yes| Weave["AOP: rename implementation and generate woven class"] |
| 65 | + Advice -->|No| Save |
| 66 | + Weave --> Save["Save changed source and any woven file; update cache state"] |
| 67 | + Save --> Load["PHP loads the resulting source"] |
| 68 | +``` |
| 69 | + |
| 70 | +Cache reuse depends on the configured mode: |
| 71 | + |
| 72 | +| Mode | Behavior | |
| 73 | +| --- | --- | |
| 74 | +| Debug enabled | Bypass cache reuse and match again. | |
| 75 | +| Development | Reuse existing cache state only when `isFresh()` succeeds. | |
| 76 | +| Production | Trust existing cache state without checking source freshness. | |
| 77 | + |
| 78 | +For a reusable state, the loader either returns the original source path or loads |
| 79 | +the recorded cache path through a stream filter. When rebuilding, processing can |
| 80 | +record woven, transformed-only, or no-transformation cache state. A class with no |
| 81 | +matching advice or transformers returns its original path immediately. |
| 82 | + |
| 83 | +When a class is woven, the generated relationship is: |
| 84 | + |
| 85 | +```php |
| 86 | +// Renamed implementation, retaining its original parent if it had one: |
| 87 | +class MyClass__AopProxied extends OriginalParent { /* Original implementation */ } |
| 88 | + |
| 89 | +// Generated class under the name application code already uses: |
| 90 | +class MyClass extends MyClass__AopProxied { /* Intercepted method wrappers */ } |
| 91 | +``` |
| 92 | + |
| 93 | +For a class without an original parent, the first declaration has no `extends` |
| 94 | +clause. The woven class extends the renamed implementation. Application code keeps |
| 95 | +using `MyClass`; it does not switch to a separate wrapper object. Source rewriting |
| 96 | +and the stream filters preserve source-file locations for debugging. |
| 97 | + |
| 98 | +Implementation: [ClassLoader](../src/Core/AutoloadInterceptor/ClassLoader.php), |
| 99 | +[AspectProcessor](../src/Core/Processor/AspectProcessor.php), |
| 100 | +[ProxiedClassModifier](../src/Core/Transform/ProxiedClassModifier.php), and |
| 101 | +[WovenClassBuilder](../src/Core/Transform/WovenClassBuilder.php). |
| 102 | + |
| 103 | +## 3. Invoke an intercepted method |
| 104 | + |
| 105 | +Class loading prepares the wrappers. Each subsequent call to an intercepted method |
| 106 | +uses the registered advice; it does not repeat source matching and weaving. |
| 107 | + |
| 108 | +```mermaid |
| 109 | +flowchart TD |
| 110 | + Call["Application calls an intercepted method"] --> Wrapper["Generated method delegates to Interceptor"] |
| 111 | + Wrapper --> Before["Run Before advice; carry forward argument changes"] |
| 112 | + Before --> HasAround{"Around advice registered?"} |
| 113 | + HasAround -->|No| Direct["Invoke the renamed implementation's method"] |
| 114 | + HasAround -->|Yes| Around |
| 115 | + subgraph Chain["Inside the Around advice chain"] |
| 116 | + Around["Enter Around advice"] --> Needed{"Chain reaches original without an established result?"} |
| 117 | + Needed -->|Yes| Original["Invoke original inside the chain, possibly through proceed()"] |
| 118 | + Original --> Resume["Resume Around advice and resolve the final chain result"] |
| 119 | + Needed -->|No| Resume |
| 120 | + end |
| 121 | + Direct --> After["Run After advice with the current result"] |
| 122 | + Resume --> After |
| 123 | + After --> Result["Return the final result to the caller"] |
| 124 | +``` |
| 125 | + |
| 126 | +This is the normal successful path. An exception interrupts it; `After` is not a |
| 127 | +`finally` handler. Missing advice groups are skipped. The configured advice order |
| 128 | +is applied within the Before, Around, and After groups. |
| 129 | + |
| 130 | +An Around invocation's `proceed()` advances the chain. The original method can run |
| 131 | +inside that call; the Around advice then resumes and can modify the result. The |
| 132 | +chain also continues |
| 133 | +after an advice returns; merely omitting `proceed()` is not sufficient to suppress |
| 134 | +the original call. Supplying a result can avoid the original call. The chain |
| 135 | +normally reuses an established result; `proceed(true)` permits repeated original |
| 136 | +calls, including from After advice. An explicit `setResult(null)` distinguishes a |
| 137 | +replacement null result from an advice that returns no replacement value. |
| 138 | + |
| 139 | +Implementation: [Interceptor](../src/Core/Intercept/Interceptor.php), |
| 140 | +[AdviceChain](../src/Core/Invocation/AdviceChain.php), and |
| 141 | +[MethodInvocation](../src/Invocation/MethodInvocation.php). |
0 commit comments