annotate vendor/symfony-cmf/routing/Enhancer/RouteContentEnhancer.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony CMF package.
Chris@0 5 *
Chris@0 6 * (c) 2011-2015 Symfony CMF
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Cmf\Component\Routing\Enhancer;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpFoundation\Request;
Chris@0 15 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * This enhancer sets the content to target field if the route provides content.
Chris@0 19 *
Chris@0 20 * Only works with RouteObjectInterface routes that can return a referenced
Chris@0 21 * content.
Chris@0 22 *
Chris@0 23 * @author David Buchmann
Chris@0 24 */
Chris@0 25 class RouteContentEnhancer implements RouteEnhancerInterface
Chris@0 26 {
Chris@0 27 /**
Chris@0 28 * @var string field for the route class
Chris@0 29 */
Chris@0 30 protected $routefield;
Chris@0 31 /**
Chris@0 32 * @var string field to write hashmap lookup result into
Chris@0 33 */
Chris@0 34 protected $target;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * @param string $routefield the field name of the route class
Chris@0 38 * @param string $target the field name to set from the map
Chris@0 39 * @param array $hashmap the map of class names to field values
Chris@0 40 */
Chris@0 41 public function __construct($routefield, $target)
Chris@0 42 {
Chris@0 43 $this->routefield = $routefield;
Chris@0 44 $this->target = $target;
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * If the route has a non-null content and if that content class is in the
Chris@0 49 * injected map, returns that controller.
Chris@0 50 *
Chris@0 51 * {@inheritdoc}
Chris@0 52 */
Chris@0 53 public function enhance(array $defaults, Request $request)
Chris@0 54 {
Chris@0 55 if (isset($defaults[$this->target])) {
Chris@0 56 // no need to do anything
Chris@0 57 return $defaults;
Chris@0 58 }
Chris@0 59
Chris@0 60 if (!isset($defaults[$this->routefield])
Chris@0 61 || !$defaults[$this->routefield] instanceof RouteObjectInterface
Chris@0 62 ) {
Chris@0 63 // we can't determine the content
Chris@0 64 return $defaults;
Chris@0 65 }
Chris@0 66 /** @var $route RouteObjectInterface */
Chris@0 67 $route = $defaults[$this->routefield];
Chris@0 68
Chris@0 69 $content = $route->getContent();
Chris@0 70 if (!$content) {
Chris@0 71 // we have no content
Chris@0 72 return $defaults;
Chris@0 73 }
Chris@0 74 $defaults[$this->target] = $content;
Chris@0 75
Chris@0 76 return $defaults;
Chris@0 77 }
Chris@0 78 }