-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle.php
72 lines (59 loc) · 1.71 KB
/
lifecycle.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
72
<?php
abstract class Lifecycle {
public $class;
function __construct($class) {
$this->class = $class;
$this->triggerAutoload($class);
}
private function triggerAutoload($class) {
class_exists($class);
}
function isOneOf($candidates) {
return in_array($this->class, $candidates);
}
abstract function instantiate($dependencies);
}
class Value extends Lifecycle {
private $instance;
function __construct($instance) {
$this->instance = $instance;
}
function instantiate($dependencies) {
return $this->instance;
}
}
class Factory extends Lifecycle {
function instantiate($dependencies) {
return call_user_func_array(
array(new ReflectionClass($this->class), 'newInstance'),
$dependencies);
}
}
class Reused extends Lifecycle {
private $instance;
function instantiate($dependencies) {
if (! isset($this->instance)) {
$this->instance = call_user_func_array(
array(new ReflectionClass($this->class), 'newInstance'),
$dependencies);
}
return $this->instance;
}
}
class Sessionable extends Lifecycle {
private $slot;
function __construct($class, $slot = false) {
parent::__construct($class);
$this->slot = $slot ? $slot : $class;
}
function instantiate($dependencies) {
@session_start();
if (! isset($_SESSION[$this->slot])) {
$_SESSION[$this->slot] = call_user_func_array(
array(new ReflectionClass($this->class), 'newInstance'),
$dependencies);
}
return $_SESSION[$this->slot];
}
}
?>