Drupal investigation

RouteContentEnhancer.php 2.1KB

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