-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStack.php
More file actions
96 lines (83 loc) · 1.93 KB
/
Stack.php
File metadata and controls
96 lines (83 loc) · 1.93 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
<?php
/**
* @link http://patchwork2.org/
* @author Ignas Rudaitis <ignas.rudaitis@gmail.com>
* @copyright 2010-2018 Ignas Rudaitis
* @license http://www.opensource.org/licenses/mit-license.html
*/
namespace Patchwork\Stack;
use Patchwork\Exceptions;
function push($offset, $calledClass, ?array $argsOverride = null)
{
State::$items[] = [$offset, $calledClass, $argsOverride];
}
function pop()
{
array_pop(State::$items);
}
function pushFor($offset, $calledClass, $callback, ?array $argsOverride = null)
{
push($offset, $calledClass, $argsOverride);
try {
$callback();
} catch (\Exception $e) {
$exception = $e;
}
pop();
if (isset($exception)) {
throw $exception;
}
}
function top($property = null)
{
$all = all();
$frame = reset($all);
$argsOverride = topArgsOverride();
if ($argsOverride !== null) {
$frame["args"] = $argsOverride;
}
if ($property) {
return isset($frame[$property]) ? $frame[$property] : null;
}
return $frame;
}
function topOffset()
{
if (empty(State::$items)) {
throw new Exceptions\StackEmpty();
}
list($offset, $calledClass) = end(State::$items);
return $offset;
}
function topCalledClass()
{
if (empty(State::$items)) {
throw new Exceptions\StackEmpty();
}
list($offset, $calledClass) = end(State::$items);
return $calledClass;
}
function topArgsOverride()
{
if (empty(State::$items)) {
throw new Exceptions\StackEmpty();
}
list($offset, $calledClass, $argsOverride) = end(State::$items);
return $argsOverride;
}
function all()
{
$backtrace = debug_backtrace();
return array_slice($backtrace, count($backtrace) - topOffset());
}
function allCalledClasses()
{
return array_map(function ($item) {
list($offset, $calledClass) = $item;
return $calledClass;
}, State::$items);
}
class State
{
static $items = [];
}