forked from symfony-cmf/menu-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentAwareFactory.php
60 lines (50 loc) · 2.05 KB
/
ContentAwareFactory.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
<?php
namespace Symfony\Cmf\Bundle\MenuBundle;
use Knp\Menu\Silex\RouterAwareFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Cmf\Bundle\RoutingExtraBundle\Routing\DoctrineRouter;
class ContentAwareFactory extends RouterAwareFactory
{
protected $contentRouter;
protected $container;
/**
* @param Container $container to fetch the request in order to determine
* whether this is the current menu item
* @param UrlGeneratorInterface $generator for the parent class
* @param UrlGeneratorInterface $contentRouter to generate routes when
* content is set
* @param string routeName the name of the route to use. DoctrineRouter
* ignores this.
*/
public function __construct(ContainerInterface $container, UrlGeneratorInterface $generator, UrlGeneratorInterface $contentRouter, $contentKey, $routeName = null)
{
parent::__construct($generator);
$this->contentRouter = $contentRouter;
$this->container = $container;
$this->contentKey = $contentKey;
$this->routeName = $routeName;
}
public function createItem($name, array $options = array())
{
$current = false;
if (!empty($options['content'])) {
try {
$request = $this->container->get('request');
if ($request->attributes->get($this->contentKey) == $options['content']) {
$current = true;
}
} catch (\Exception $e) {}
$routeParameters = $options['routeParameters'];
$routeParameters['content'] = $options['content'];
$options['uri'] = $this->contentRouter->generate($this->routeName, $routeParameters, $options['routeAbsolute']);
unset($options['route']);
}
$item = parent::createItem($name, $options);
if ($current) {
$item->setCurrent(true);
}
return $item;
}
}