4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing\Enhancer;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
18 * This enhancer sets the content to target field if the route provides content.
20 * Only works with RouteObjectInterface routes that can return a referenced
23 * @author David Buchmann
25 class RouteContentEnhancer implements RouteEnhancerInterface
28 * @var string field for the route class
30 protected $routefield;
32 * @var string field to write hashmap lookup result into
37 * @param string $routefield the field name of the route class
38 * @param string $target the field name to set from the map
39 * @param array $hashmap the map of class names to field values
41 public function __construct($routefield, $target)
43 $this->routefield = $routefield;
44 $this->target = $target;
48 * If the route has a non-null content and if that content class is in the
49 * injected map, returns that controller.
53 public function enhance(array $defaults, Request $request)
55 if (isset($defaults[$this->target])) {
56 // no need to do anything
60 if (!isset($defaults[$this->routefield])
61 || !$defaults[$this->routefield] instanceof RouteObjectInterface
63 // we can't determine the content
66 /** @var $route RouteObjectInterface */
67 $route = $defaults[$this->routefield];
69 $content = $route->getContent();
74 $defaults[$this->target] = $content;