/
Crumb.php
156 lines (141 loc) · 3.61 KB
/
Crumb.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace infrajs\crumb;
use infrajs\sequence\Sequence;
class Crumb
{
public $name;
public $parent;
public $child;
public $value;//Строка или null
public $query;
public static $childs = array();
public $counter = 0;
public static $globalcounter = 0;
public $path;//Путь текущей крошки
public static $params;//Всё что после первого амперсанда
public static $get;
public $is;
protected function __construct($right)
{
}
public function getRoot()
{
$root = $this;
while ($root->parent) {
$root = $root->parent;
}
return $root;
}
public function __toString(){
return implode('/', $this->path);
}
public function getInst($name = '')
{
$right = $this->path;
return self::getInstance($name, $right);
}
public static function getInstance($name = '', $right = array())
{
$right = self::right(array_merge($right, self::right($name)));
if (@$right[0] === '') {
$right = array();
}
$short = self::short($right);
if (empty(self::$childs[$short])) {
$that = new self($right);
$that->path = $right;
$that->name = @$right[sizeof($right) - 1];
$that->value = $that->query = $that->is = $that->counter = null;
self::$childs[$short] = $that;
if ($that->name) {
$that->parent = $that->getInst('//');
}
}
return self::$childs[$short];
}
public static function right($short)
{
return Sequence::right($short, '/');
}
public static function short($right)
{
return Sequence::short($right, '/');
}
public function getGET()
{
return self::$get;
}
public static function change($query)
{
$amp = explode('&', $query, 2);
$eq = explode('=', $amp[0], 2);
$sl = explode('/', $eq[0], 2);
if (sizeof($eq) !== 1 && sizeof($sl) === 1) {
//В первой крошке нельзя использовать символ "=" для совместимости с левыми параметрами для главной страницы, которая всё равно покажется
$params = $query;
$query = '';
} else {
$params = (string) @$amp[1];
$query = $amp[0];
}
self::$params = $params;
parse_str($params, self::$get);
$right = self::right($query);
$counter = ++self::$globalcounter;
$inst = self::getInstance();
$old = $inst->path;
//Crumb::$path=$right;
//Crumb::$value=(string)@$right[0];
//Crumb::$query=Crumb::short($right);
//Crumb::$child=Crumb::getInstance((string)@$right[0]);
$that = self::getInstance($right);
$child = null;
while ($that) {
$that->counter = $counter;
$that->is = true;
$that->child = $child;
$that->value = (string) @$right[sizeof($that->path)];
$that->query = self::short(array_slice($right, sizeof($that->path)));
$child = $that;
$that = $that->parent;
};
$that = self::getInstance($old);
if (!$that) {
return;
}
while ($that) {
if ($that->counter == $counter) {
break;
}
$that->is = $that->child = $that->value = $that->query = null;
$that = $that->parent;
};
}
public static function init()
{
//Crumb::$child=Crumb::getInstance();
$query = urldecode(Path::toutf($_SERVER['QUERY_STRING']));
self::change($query);
}
public function toString()
{
return $this->short($this->path);
}
public static function set(&$layer, $name, &$value)
{
if (!isset($layer['dyn'])) {
$layer['dyn'] = array();
}
$layer['dyn'][$name] = $value;
if (isset($layer['parent'])) {
$root = &$layer['parent'][$name];
} else {
$root = &Crumb::getInstance();
}
if ($layer['dyn'][$name]) {
$layer[$name] = &$root->getInst($layer['dyn'][$name]);
} else {
$layer[$name] = &$root;
}
}
}