/** * **Return the Controller instance associated with a Request.** * * As several resolvers can exist for a single application, a resolver must * return false when it is not able to determine the controller. * * The resolver must only throw an exception when it should be able to load * controller but cannot because of some errors made by the developer. * * @param Request $request A Request instance * * @return callable|false A PHP callable representing the Controller, * or false if this resolver is not able to determine the controller. */ public function getController(Request $request) : callable { $controller = $request->attributes->get('_controller', NULL); if (!$this->callbackResolver->isValid($controller)) { return $this->controllerResolver->getController($request); } return $this->callbackResolver->convertCallback($controller); }
/** * Returns true if the string is a valid service method representation or if * the string/array references a class contained in the resolver's classmap. * * @param string $name * * @return bool */ public function isValid($name) { if (parent::isValid($name)) { return true; } if (is_array($name)) { list($cls, $method) = $name; if (is_object($cls)) { return false; // No need to convert } if (is_array($method)) { return true; // Need to convert } } elseif (is_string($name) && strpos($name, '::') > 0) { list($cls, $method) = explode('::', $name); } else { return false; // Can't handle this, maybe already callable } if (isset($this->classmap[$cls])) { return true; // Will use service definition } if (!class_exists($cls) || !method_exists($cls, $method)) { return false; // Can't handle this } $refMethod = new \ReflectionMethod($cls, $method); if ($refMethod->isStatic()) { return false; // Already valid } $constructor = $refMethod->getDeclaringClass()->getConstructor(); // We can create the class if no constructor params, else can't handle it return $constructor === null || $constructor->getNumberOfRequiredParameters() === 0; }
/** * Returns true if the string is a valid service method representation. * * @param string $name * * @return Boolean */ public function isValid($name) { return parent::isValid($name) || is_string($name) && preg_match(static::PAGE_PATTERN, $name); }