annotate core/lib/Drupal/Core/Entity/EntityResolverManager.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
Chris@0 6 use Symfony\Component\Routing\Route;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Sets the entity route parameter converter options automatically.
Chris@0 10 *
Chris@0 11 * If controllers of routes with route parameters, type-hint the parameters with
Chris@0 12 * an entity interface, upcasting is done automatically.
Chris@0 13 */
Chris@0 14 class EntityResolverManager {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * The entity manager.
Chris@0 18 *
Chris@0 19 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 20 */
Chris@0 21 protected $entityManager;
Chris@0 22
Chris@0 23 /**
Chris@0 24 * The class resolver.
Chris@0 25 *
Chris@0 26 * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
Chris@0 27 */
Chris@0 28 protected $classResolver;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Constructs a new EntityRouteAlterSubscriber.
Chris@0 32 *
Chris@0 33 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 34 * The entity manager.
Chris@0 35 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
Chris@0 36 * The class resolver.
Chris@0 37 */
Chris@0 38 public function __construct(EntityManagerInterface $entity_manager, ClassResolverInterface $class_resolver) {
Chris@0 39 $this->entityManager = $entity_manager;
Chris@0 40 $this->classResolver = $class_resolver;
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Gets the controller class using route defaults.
Chris@0 45 *
Chris@0 46 * By design we cannot support all possible routes, but just the ones which
Chris@0 47 * use the defaults provided by core, which are _controller and _form.
Chris@0 48 *
Chris@0 49 * Rather than creating an instance of every controller determine the class
Chris@0 50 * and method that would be used. This is not possible for the service:method
Chris@0 51 * notation as the runtime container does not allow static introspection.
Chris@0 52 *
Chris@0 53 * @see \Drupal\Core\Controller\ControllerResolver::getControllerFromDefinition()
Chris@0 54 * @see \Drupal\Core\Controller\ClassResolver::getInstanceFromDefinition()
Chris@0 55 *
Chris@0 56 * @param array $defaults
Chris@0 57 * The default values provided by the route.
Chris@0 58 *
Chris@0 59 * @return string|null
Chris@0 60 * Returns the controller class, otherwise NULL.
Chris@0 61 */
Chris@0 62 protected function getControllerClass(array $defaults) {
Chris@0 63 $controller = NULL;
Chris@0 64 if (isset($defaults['_controller'])) {
Chris@0 65 $controller = $defaults['_controller'];
Chris@0 66 }
Chris@0 67
Chris@0 68 if (isset($defaults['_form'])) {
Chris@0 69 $controller = $defaults['_form'];
Chris@0 70 // Check if the class exists and if so use the buildForm() method from the
Chris@0 71 // interface.
Chris@0 72 if (class_exists($controller)) {
Chris@0 73 return [$controller, 'buildForm'];
Chris@0 74 }
Chris@0 75 }
Chris@0 76
Chris@0 77 if (strpos($controller, ':') === FALSE) {
Chris@0 78 if (method_exists($controller, '__invoke')) {
Chris@0 79 return [$controller, '__invoke'];
Chris@0 80 }
Chris@0 81 if (function_exists($controller)) {
Chris@0 82 return $controller;
Chris@0 83 }
Chris@0 84 return NULL;
Chris@0 85 }
Chris@0 86
Chris@0 87 $count = substr_count($controller, ':');
Chris@0 88 if ($count == 1) {
Chris@0 89 // Controller in the service:method notation. Get the information from the
Chris@0 90 // service. This is dangerous as the controller could depend on services
Chris@0 91 // that could not exist at this point. There is however no other way to
Chris@0 92 // do it, as the container does not allow static introspection.
Chris@0 93 list($class_or_service, $method) = explode(':', $controller, 2);
Chris@0 94 return [$this->classResolver->getInstanceFromDefinition($class_or_service), $method];
Chris@0 95 }
Chris@0 96 elseif (strpos($controller, '::') !== FALSE) {
Chris@0 97 // Controller in the class::method notation.
Chris@0 98 return explode('::', $controller, 2);
Chris@0 99 }
Chris@0 100
Chris@0 101 return NULL;
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Sets the upcasting information using reflection.
Chris@0 106 *
Chris@0 107 * @param string|array $controller
Chris@0 108 * A PHP callable representing the controller.
Chris@0 109 * @param \Symfony\Component\Routing\Route $route
Chris@0 110 * The route object to populate without upcasting information.
Chris@0 111 *
Chris@0 112 * @return bool
Chris@0 113 * Returns TRUE if the upcasting parameters could be set, FALSE otherwise.
Chris@0 114 */
Chris@0 115 protected function setParametersFromReflection($controller, Route $route) {
Chris@0 116 $entity_types = $this->getEntityTypes();
Chris@0 117 $parameter_definitions = $route->getOption('parameters') ?: [];
Chris@0 118
Chris@0 119 $result = FALSE;
Chris@0 120
Chris@0 121 if (is_array($controller)) {
Chris@0 122 list($instance, $method) = $controller;
Chris@0 123 $reflection = new \ReflectionMethod($instance, $method);
Chris@0 124 }
Chris@0 125 else {
Chris@0 126 $reflection = new \ReflectionFunction($controller);
Chris@0 127 }
Chris@0 128
Chris@0 129 $parameters = $reflection->getParameters();
Chris@0 130 foreach ($parameters as $parameter) {
Chris@0 131 $parameter_name = $parameter->getName();
Chris@0 132 // If the parameter name matches with an entity type try to set the
Chris@0 133 // upcasting information automatically. Therefore take into account that
Chris@0 134 // the user has specified some interface, so the upcasting is intended.
Chris@0 135 if (isset($entity_types[$parameter_name])) {
Chris@0 136 $entity_type = $entity_types[$parameter_name];
Chris@0 137 $entity_class = $entity_type->getClass();
Chris@0 138 if (($reflection_class = $parameter->getClass()) && (is_subclass_of($entity_class, $reflection_class->name) || $entity_class == $reflection_class->name)) {
Chris@0 139 $parameter_definitions += [$parameter_name => []];
Chris@0 140 $parameter_definitions[$parameter_name] += [
Chris@0 141 'type' => 'entity:' . $parameter_name,
Chris@0 142 ];
Chris@0 143 $result = TRUE;
Chris@0 144 }
Chris@0 145 }
Chris@0 146 }
Chris@0 147 if (!empty($parameter_definitions)) {
Chris@0 148 $route->setOption('parameters', $parameter_definitions);
Chris@0 149 }
Chris@0 150 return $result;
Chris@0 151 }
Chris@0 152
Chris@0 153 /**
Chris@0 154 * Sets the upcasting information using the _entity_* route defaults.
Chris@0 155 *
Chris@0 156 * Supports the '_entity_view' and '_entity_form' route defaults.
Chris@0 157 *
Chris@0 158 * @param \Symfony\Component\Routing\Route $route
Chris@0 159 * The route object.
Chris@0 160 */
Chris@0 161 protected function setParametersFromEntityInformation(Route $route) {
Chris@0 162 if ($entity_view = $route->getDefault('_entity_view')) {
Chris@0 163 list($entity_type) = explode('.', $entity_view, 2);
Chris@0 164 }
Chris@0 165 elseif ($entity_form = $route->getDefault('_entity_form')) {
Chris@0 166 list($entity_type) = explode('.', $entity_form, 2);
Chris@0 167 }
Chris@0 168
Chris@0 169 // Do not add parameter information if the route does not declare a
Chris@0 170 // parameter in the first place. This is the case for add forms, for
Chris@0 171 // example.
Chris@0 172 if (isset($entity_type) && isset($this->getEntityTypes()[$entity_type]) && (strpos($route->getPath(), '{' . $entity_type . '}') !== FALSE)) {
Chris@0 173 $parameter_definitions = $route->getOption('parameters') ?: [];
Chris@0 174
Chris@0 175 // First try to figure out whether there is already a parameter upcasting
Chris@0 176 // the same entity type already.
Chris@0 177 foreach ($parameter_definitions as $info) {
Chris@0 178 if (isset($info['type']) && (strpos($info['type'], 'entity:') === 0)) {
Chris@0 179 // The parameter types are in the form 'entity:$entity_type'.
Chris@0 180 list(, $parameter_entity_type) = explode(':', $info['type'], 2);
Chris@0 181 if ($parameter_entity_type == $entity_type) {
Chris@0 182 return;
Chris@0 183 }
Chris@0 184 }
Chris@0 185 }
Chris@0 186
Chris@0 187 if (!isset($parameter_definitions[$entity_type])) {
Chris@0 188 $parameter_definitions[$entity_type] = [];
Chris@0 189 }
Chris@0 190 $parameter_definitions[$entity_type] += [
Chris@0 191 'type' => 'entity:' . $entity_type,
Chris@0 192 ];
Chris@0 193 if (!empty($parameter_definitions)) {
Chris@0 194 $route->setOption('parameters', $parameter_definitions);
Chris@0 195 }
Chris@0 196 }
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * Set the upcasting route objects.
Chris@0 201 *
Chris@0 202 * @param \Symfony\Component\Routing\Route $route
Chris@0 203 * The route object to add the upcasting information onto.
Chris@0 204 */
Chris@0 205 public function setRouteOptions(Route $route) {
Chris@0 206 if ($controller = $this->getControllerClass($route->getDefaults())) {
Chris@0 207 // Try to use reflection.
Chris@0 208 if ($this->setParametersFromReflection($controller, $route)) {
Chris@0 209 return;
Chris@0 210 }
Chris@0 211 }
Chris@0 212
Chris@0 213 // Try to use _entity_* information on the route.
Chris@0 214 $this->setParametersFromEntityInformation($route);
Chris@0 215 }
Chris@0 216
Chris@0 217 /**
Chris@0 218 * Gets the list of all entity types.
Chris@0 219 *
Chris@0 220 * @return \Drupal\Core\Entity\EntityTypeInterface[]
Chris@0 221 */
Chris@0 222 protected function getEntityTypes() {
Chris@0 223 if (!isset($this->entityTypes)) {
Chris@0 224 $this->entityTypes = $this->entityManager->getDefinitions();
Chris@0 225 }
Chris@0 226 return $this->entityTypes;
Chris@0 227 }
Chris@0 228
Chris@0 229 }