-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapplication.php
More file actions
97 lines (84 loc) · 1.61 KB
/
Copy pathapplication.php
File metadata and controls
97 lines (84 loc) · 1.61 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php namespace Feather\Components\Foundation;
use Closure;
use ArrayAccess;
use InvalidArgumentException;
class Application implements ArrayAccess {
/**
* Applications container.
*
* @var array
*/
private $container = array();
/**
* Share a closure such that it is a singleton.
*
* @param Closure $closure
* @return Closure
*/
public function share(Closure $closure)
{
return function($container) use ($closure)
{
// We'll declare a static object here, if the object is not defined
// then we'll get execute the closure and return it.
static $object;
if(is_null($object))
{
$object = $closure($container);
}
return $object;
};
}
/**
* Determine if an offset exists.
*
* @param string $key
* @return bool
*/
public function offsetExists($key)
{
return isset($this->container[$key]);
}
/**
* Get the value at a given offset.
*
* @param string $key
* @return mixed
*/
public function offsetGet($key)
{
if(!$this->offsetExists($key))
{
throw new InvalidArgumentException("Identifier {$key} is not in the container.");
}
return $this->container[$key]($this);
}
/**
* Set the value at a given offset.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
{
if(!$value instanceof Closure)
{
$value = function() use ($value)
{
return $value;
};
}
$this->container[$key] = $value;
}
/**
* Unset a given offset.
*
* @param string $key
* @return void
*/
public function offsetUnset($key)
{
unset($this->container[$key]);
}
}