This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
/
BreadcrumbsBuilder.php
207 lines (173 loc) · 4.84 KB
/
BreadcrumbsBuilder.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/*
* This file is part of the BreadcrumbsBundle.
*
* (c) Yonel Ceruto <yonelceruto@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yceruto\Bundle\BreadcrumbsBundle;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
/**
* This builder works in 2 modes:
*
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
* * 2.4+ mode where you must pass a RequestStack instance in the constructor.
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class BreadcrumbsBuilder
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var Request
*/
private $request;
/**
* @var Router
*/
private $router;
/**
* @var TraceableUrlMatcher
*/
private $matcher;
public function __construct(Router $router, RequestStack $requestStack = null)
{
$this->router = $router;
$this->requestStack = $requestStack;
}
/**
* BC with SF 2.3
*
* @param Request|null $request
*/
public function setRequest(Request $request = null)
{
$this->request = $request;
}
/**
* Create a empty breadcrumb
*
* @return Breadcrumbs
*/
public function create()
{
return new Breadcrumbs();
}
/**
* Create a breadcrumb through current request path
*
* @return Breadcrumbs
*/
public function createFromRequest()
{
if (empty($this->matcher)) {
$this->matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $this->router->getContext());
}
$breadcrumbs = new Breadcrumbs();
$parent = null;
$paths = $this->getBreadcrumbsPaths();
foreach ($paths as $path) {
if ($node = $this->createBreadcrumbsNode($path, $parent)) {
$breadcrumbs->addNode($node);
$parent = $path;
}
}
return $breadcrumbs;
}
/**
* Get all breadcrumbs paths from current request path
*
* @return array of string
*/
private function getBreadcrumbsPaths()
{
$parts = array();
$pathInfo = trim($this->getRequest()->getPathInfo(), '/');
if ($pathInfo) {
$parts = explode('/', $pathInfo);
}
array_unshift($parts, '/');
$path = '';
$paths = array();
foreach ($parts as $part) {
$path .= $part;
$paths[] = $path;
if ('/' !== $part) {
$path .= '/';
$paths[] = $path;
}
}
return $paths;
}
/**
* Create a breadcrumbs node from path
*
* @param string $path
* @param string $parent
*
* @return BreadcrumbsNode|bool
*/
private function createBreadcrumbsNode($path, $parent)
{
// use $baseUrl for no prod environments e.g dev 'app_dev.php'
$baseUrl = $this->getRequest()->getBaseUrl();
$traces = $this->matcher->getTraces($path);
foreach ($traces as $trace) {
if (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
$label = $this->getLabel($path, $parent, $trace['name']);
$node = new BreadcrumbsNode();
$node->setLabel($label);
$node->setPath($baseUrl.$path);
return $node;
}
}
return false;
}
private function getRequest()
{
return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
}
/**
* Get label
*
* @param $path
* @param $parent
* @param $name
*
* @return string
*/
private function getLabel($path, $parent, $name)
{
$route = $this->router->getRouteCollection()->get($name);
// get label through settings
$label = $route->getDefault('breadcrumbs_label');
if (empty($label)) {
// get label through path
$compiledRoute = $route->compile();
$vars = $compiledRoute->getVariables();
if (empty($vars)) {
$label = substr($path, strlen($parent));
} elseif (preg_match($compiledRoute->getRegex(), $path, $match)) {
$label = $match[end($vars)];
}
$label = trim(preg_replace('[\W|_]', ' ', $label));
if (is_numeric($label)) {
$label = null;
}
}
if (empty($label)) {
// get label through route name
$label = 'breadcrumbs.'.$name;
return $label;
}
return $label;
}
}