forked from antimax/PHP-DependencyInjectionContainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream.php
71 lines (54 loc) · 1.55 KB
/
Stream.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
require_once 'DiContainer/Container.php';
use DiContainer\Interception;
interface IStream
{
function Read();
function Write(&$stream);
}
class Stream implements IStream
{
private $buffer = null;
public function Read()
{
return $this->buffer;
}
public function Write(&$stream)
{
$this->buffer = $stream;
$stream = 'xxxx';
}
}
class StreamInterceptor implements Interception\IInterceptor
{
public function Intercept(Interception\IInvocation $invocation)
{
if ($invocation->GetMethod()->getName() == 'Write') {
/** @var $instance IStream */
$instance = $invocation->GetDecoratedInstance();
$params = &$invocation->GetMethodParameters();
if ($instance->Read() == $params['stream']) {
throw new Exception('You can not set the value whict is already set');
}
$params['stream'] = 'yyy';
}
$invocation->Proceed();
}
}
class StreamInterceptorSelector implements Interception\IInerceptorSelector
{
public function GetInterceptors(\ReflectionClass $decoratedType)
{
if ($decoratedType->getName() != 'IStream') {
return array();
}
return array(new StreamInterceptor());
}
}
$container = new DiContainer\Container(array(), array(new StreamInterceptorSelector()));
/** @var $stream IStream */
$stream = $container->RegisterType('IStream', 'Stream')->Resolve('IStream');
$value = 'asss';
$stream->Write($value);
print_r($stream);
print $value;