Completed
Pull Request — master (#465)
by Marco
23:33
created

MyProxiedClass::sayHello()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace StaticAnalysis\LazyLoadingValueHolder;
4
5
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
6
use ProxyManager\Proxy\LazyLoadingInterface;
7
8
require_once __DIR__ . '/../../vendor/autoload.php';
9
10
class MyProxiedClass
11
{
12
    public function sayHello() : string
13
    {
14
        return 'Hello!';
15
    }
16
}
17
18
echo (new LazyLoadingValueHolderFactory())
19
    ->createProxy(
20
        MyProxiedClass::class,
21
        static function (
22
            ?object & $instance,
23
            LazyLoadingInterface $proxy,
24
            string $method,
25
            array $parameters,
26
            ?\Closure & $initializer
27
        ) : bool {
28
            $instance    = new MyProxiedClass();
29
            $initializer = null; // disable initialization
30
31
            return true;
32
        }
33
    )
34
    ->sayHello();
35
36
$valueHolder = (new LazyLoadingValueHolderFactory())
37
    ->createProxy(MyProxiedClass::class, static function (
38
        ?object & $wrappedObject,
39
        LazyLoadingInterface $proxy,
40
        string $method,
41
        array $parameters,
42
        ?\Closure & $initializer
43
    ) : bool {
44
        $initializer   = null; // disable initialization
45
        $wrappedObject = new MyProxiedClass();
46
47
        return true;
48
    });
49
50
$valueHolder->initializeProxy();
51
52
$wrappedValue = $valueHolder->getWrappedValueHolderValue();
53
54
assert(null !== $wrappedValue);
55
56
echo $wrappedValue->sayHello();
57
58
$valueHolder->setProxyInitializer(static function (
59
    ?object & $instance,
60
    LazyLoadingInterface $proxy,
61
    string $method,
62
    array $parameters,
63
    ?\Closure & $initializer
64
) : bool {
65
    $instance    = new MyProxiedClass();
66
    $initializer = null; // disable initialization
67
68
    return true;
69
});
70