Skip to content

Commit 030c9fd

Browse files
authored
Merge pull request #97 from WalterWoshid/docs/architecture-diagrams
Document AOP architecture with Mermaid diagrams
2 parents 58061c9 + 8875615 commit 030c9fd

2 files changed

Lines changed: 161 additions & 24 deletions

File tree

‎README.md‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -755,33 +755,29 @@ $firstLog = $logs[0];
755755

756756
## General workflow when a class is loaded
757757

758-
- The `AutoloadInterceptor` service intercepts the loading of a class
759-
760-
- The `AspectMatcher` matches the class and method names with the list of
761-
aspects and their configuration
762-
763-
- If the class and method names match an aspect, query the cache state to see
764-
if the source code is already cached
765-
766-
- Check if the cache is valid:
767-
- Modification time of the caching process is less than the modification
768-
time of the source file or the aspect file
769-
- Check if the cache file, the source file and the aspect file exist
770-
771-
- If the cache is valid, load the proxied class from the cache
772-
- If not, return a stream filter path to the `AutoloadInterceptor` service
758+
PHP-AOP builds on CodeTransformer's autoloader, stream filters, and cache. After
759+
kernel initialization, eligible classes follow this simplified flow:
760+
761+
```mermaid
762+
flowchart TD
763+
Request["Class requested"] --> Cache{"Reusable cache state?"}
764+
Cache -->|Yes| Cached["Load the source recorded by the cache state"]
765+
Cache -->|No| Match["Match aspects and transformers"]
766+
Match --> Any{"Anything matched?"}
767+
Any -->|No| Original["Load original source"]
768+
Any -->|Yes| Transform["Apply transformers, then weave matched advice"]
769+
Transform --> Load["Update cache state and load resulting source"]
770+
```
773771

774-
- The `StreamFilter` modifies the source code by applying the aspects
775-
- Convert the original source code to a proxied class
776-
(MyClass -> MyClass__AopProxied)
777-
- The proxied class should have the same amount of lines as the original
778-
class (because the debugger will point to the original class)
779-
- The proxied class extends a woven class which contains the logic of applying
780-
the aspects
781-
- The woven class will be included at the bottom of the proxied class
782-
- The woven class will also be cached
772+
Internal classes and excluded paths bypass this flow. Debug mode disables cache
773+
reuse; development checks freshness, while production trusts existing cache state.
783774

775+
For weaving, the original implementation is renamed to `MyClass__AopProxied`, and
776+
the generated `MyClass` **extends that renamed implementation**. Its intercepted
777+
methods delegate to the advice machinery; callers keep using the original name.
784778

779+
See [How PHP-AOP works](docs/architecture.md) for the initialization, class-loading,
780+
and method-invocation diagrams, with links to the implementation.
785781

786782
## Testing
787783

‎docs/architecture.md‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)