forked from lanfix/goaop-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluentInterfaceAspect.php
More file actions
40 lines (35 loc) · 1.17 KB
/
FluentInterfaceAspect.php
File metadata and controls
40 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Demo\Aspect;
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Attribute\Around;
/**
* Fluent interface aspect provides an easy way to reuse "chain" interface for classes
*
* Basically, it uses around method to intercept all public setters in the class that implements
* special marker interface FluentInterface. Then it checks the return value for setter, if it's null,
* then advice replaces it with reference to the object "$this".
*
* @see http://go.aopphp.com/blog/2013/03/19/implementing-fluent-interface-pattern-in-php/
*/
class FluentInterfaceAspect implements Aspect
{
/**
* Fluent interface advice
*/
#[Around('within(Demo\Aspect\FluentInterface+) && execution(public **->set*(*))')]
protected function aroundMethodExecution(MethodInvocation $invocation): mixed
{
$result = $invocation->proceed();
return $result ?? $invocation->getThis();
}
}