118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\AdminBreadcrumbsBundle\Model;
|
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
class BreadcrumbManager
|
|
{
|
|
protected $root_item = null;
|
|
protected $breadcrumb;
|
|
|
|
protected $router;
|
|
|
|
protected $default_params;
|
|
protected $default_routename;
|
|
|
|
public function __construct(RouterInterface $router, $default_routename, array $default_params)
|
|
{
|
|
$this->router = $router;
|
|
$this->default_routename = $default_routename;
|
|
$this->default_params = $default_params;
|
|
}
|
|
|
|
public function initBreadcrumb()
|
|
{
|
|
$this->breadcrumb = new Breadcrumb();
|
|
$this->breadcrumb->addItem($this->getRootItem());
|
|
}
|
|
|
|
public function get()
|
|
{
|
|
if ($this->breadcrumb === null) {
|
|
$this->initBreadcrumb();
|
|
}
|
|
|
|
return $this->breadcrumb;
|
|
}
|
|
|
|
public function setRootItem(BreadcrumbItem $root_item)
|
|
{
|
|
$this->root_item = $root_item;
|
|
}
|
|
|
|
public function getRootItem()
|
|
{
|
|
if ($this->root_item === null) {
|
|
$this->root_item = new BreadcrumbItem(
|
|
'trinity.dashboard.index.title',
|
|
$this->router->generate($this->default_routename, $this->default_params),
|
|
'trinity.dashboard.index.helper'
|
|
);
|
|
}
|
|
|
|
return $this->root_item;
|
|
}
|
|
|
|
public function buildCrudBreadcrumbFor(Request $request)
|
|
{
|
|
$routename = $request->get('_route');
|
|
|
|
if ($routename == $this->default_routename) {
|
|
return;
|
|
}
|
|
|
|
if (preg_match('`^.*index`',$routename)) {
|
|
$this->get()->addItem($this->getItemFor($request));
|
|
|
|
return;
|
|
}
|
|
|
|
$this->get()->addItem($this->getItemFor($request,true));
|
|
$this->get()->addItem($this->getItemFor($request));
|
|
}
|
|
|
|
public function getItemFor($request, $index = false)
|
|
{
|
|
$key = $this->generateKey($request, $index);
|
|
$route = $this->generateRoute($request, $index);
|
|
|
|
$item = new BreadcrumbItem(
|
|
sprintf('%s.title', $key),
|
|
$route,
|
|
sprintf('%s.helper', $key)
|
|
);
|
|
|
|
return $item;
|
|
}
|
|
|
|
private function generateRoute(Request $request, $index = false)
|
|
{
|
|
if ($index) {
|
|
$route = preg_replace('`_.*$`','_index', $request->get('_route'));
|
|
|
|
return $this->router->generate($route);
|
|
}
|
|
|
|
return $this->router->generate($request->get('_route'), $request->get('_route_params'));
|
|
}
|
|
|
|
private function generateKey(Request $request, $index = false)
|
|
{
|
|
$datas = explode('::', $request->get('_controller'));
|
|
$action = ($index) ? 'index' : str_replace('Action', '', $datas[1]);
|
|
$path = explode('\\', $datas[0]);
|
|
|
|
if (count($path) < 4) {
|
|
return null;
|
|
}
|
|
|
|
$firm = strtolower($path[0]);
|
|
$bundle = strtolower(str_replace('Bundle', '', $path[count($path)-3]));
|
|
$controller = strtolower(str_replace('Controller', '', end($path)));
|
|
|
|
return sprintf('%s.%s.%s.%s', $firm, $bundle, $controller, $action);
|
|
}
|
|
}
|