/
RouteContentEnhancer.php
79 lines (69 loc) · 2.06 KB
/
RouteContentEnhancer.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
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
/**
* This enhancer sets the content to target field if the route provides content.
*
* Only works with RouteObjectInterface routes that can return a referenced
* content.
*
* @author David Buchmann
*/
class RouteContentEnhancer implements RouteEnhancerInterface
{
/**
* @var string field for the route class
*/
protected $routefield;
/**
* @var string field to write hashmap lookup result into
*/
protected $target;
/**
* @param string $routefield the field name of the route class
* @param string $target the field name to set from the map
* @param array $hashmap the map of class names to field values
*/
public function __construct($routefield, $target)
{
$this->routefield = $routefield;
$this->target = $target;
}
/**
* If the route has a non-null content and if that content class is in the
* injected map, returns that controller.
*
* {@inheritDoc}
*/
public function enhance(array $defaults, Request $request)
{
if (isset($defaults[$this->target])) {
// no need to do anything
return $defaults;
}
if (! isset($defaults[$this->routefield])
|| ! $defaults[$this->routefield] instanceof RouteObjectInterface
) {
// we can't determine the content
return $defaults;
}
/** @var $route RouteObjectInterface */
$route = $defaults[$this->routefield];
$content = $route->getContent();
if (! $content) {
// we have no content
return $defaults;
}
$defaults[$this->target] = $content;
return $defaults;
}
}