Laravel 5.6 updates
Travis config update Removed HHVM script as Laravel no longer support HHVM after releasing 5.3
This commit is contained in:
@@ -16,44 +16,22 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* This implementation uses the '_controller' request attribute to determine
|
||||
* the controller to execute and uses the request attributes to determine
|
||||
* the controller method arguments.
|
||||
* the controller to execute.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Tobias Schultze <http://tobion.de>
|
||||
*/
|
||||
class ControllerResolver implements ArgumentResolverInterface, ControllerResolverInterface
|
||||
class ControllerResolver implements ControllerResolverInterface
|
||||
{
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* If the ...$arg functionality is available.
|
||||
*
|
||||
* Requires at least PHP 5.6.0 or HHVM 3.9.1
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $supportsVariadic;
|
||||
|
||||
/**
|
||||
* If scalar types exists.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $supportsScalarTypes;
|
||||
|
||||
public function __construct(LoggerInterface $logger = null)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
|
||||
$this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
|
||||
$this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* This method looks for a '_controller' request attribute that represents
|
||||
* the controller name (a string like ClassName::MethodName).
|
||||
*/
|
||||
public function getController(Request $request)
|
||||
{
|
||||
@@ -66,23 +44,42 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
|
||||
}
|
||||
|
||||
if (\is_array($controller)) {
|
||||
if (isset($controller[0]) && \is_string($controller[0]) && isset($controller[1])) {
|
||||
try {
|
||||
$controller[0] = $this->instantiateController($controller[0]);
|
||||
} catch (\Error | \LogicException $e) {
|
||||
try {
|
||||
// We cannot just check is_callable but have to use reflection because a non-static method
|
||||
// can still be called statically in PHP but we don't want that. This is deprecated in PHP 7, so we
|
||||
// could simplify this with PHP 8.
|
||||
if ((new \ReflectionMethod($controller[0], $controller[1]))->isStatic()) {
|
||||
return $controller;
|
||||
}
|
||||
} catch (\ReflectionException $reflectionException) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
if (!\is_callable($controller)) {
|
||||
throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable. %s', $request->getPathInfo(), $this->getControllerError($controller)));
|
||||
}
|
||||
|
||||
return $controller;
|
||||
}
|
||||
|
||||
if (\is_object($controller)) {
|
||||
if (method_exists($controller, '__invoke')) {
|
||||
return $controller;
|
||||
if (!\is_callable($controller)) {
|
||||
throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable. %s', $request->getPathInfo(), $this->getControllerError($controller)));
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', \get_class($controller), $request->getPathInfo()));
|
||||
return $controller;
|
||||
}
|
||||
|
||||
if (false === strpos($controller, ':')) {
|
||||
if (method_exists($controller, '__invoke')) {
|
||||
return $this->instantiateController($controller);
|
||||
} elseif (\function_exists($controller)) {
|
||||
return $controller;
|
||||
}
|
||||
if (\function_exists($controller)) {
|
||||
return $controller;
|
||||
}
|
||||
|
||||
$callable = $this->createController($controller);
|
||||
@@ -94,93 +91,34 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
|
||||
return $callable;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
|
||||
*/
|
||||
public function getArguments(Request $request, $controller)
|
||||
{
|
||||
@trigger_error(sprintf('The "%s()" method is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
|
||||
|
||||
if (\is_array($controller)) {
|
||||
$r = new \ReflectionMethod($controller[0], $controller[1]);
|
||||
} elseif (\is_object($controller) && !$controller instanceof \Closure) {
|
||||
$r = new \ReflectionObject($controller);
|
||||
$r = $r->getMethod('__invoke');
|
||||
} else {
|
||||
$r = new \ReflectionFunction($controller);
|
||||
}
|
||||
|
||||
return $this->doGetArguments($request, $controller, $r->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param callable $controller
|
||||
* @param \ReflectionParameter[] $parameters
|
||||
*
|
||||
* @return array The arguments to use when calling the action
|
||||
*
|
||||
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
|
||||
*/
|
||||
protected function doGetArguments(Request $request, $controller, array $parameters)
|
||||
{
|
||||
@trigger_error(sprintf('The "%s()" method is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
|
||||
|
||||
$attributes = $request->attributes->all();
|
||||
$arguments = array();
|
||||
foreach ($parameters as $param) {
|
||||
if (array_key_exists($param->name, $attributes)) {
|
||||
if ($this->supportsVariadic && $param->isVariadic() && \is_array($attributes[$param->name])) {
|
||||
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
|
||||
} else {
|
||||
$arguments[] = $attributes[$param->name];
|
||||
}
|
||||
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
|
||||
$arguments[] = $request;
|
||||
} elseif ($param->isDefaultValueAvailable()) {
|
||||
$arguments[] = $param->getDefaultValue();
|
||||
} elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) {
|
||||
$arguments[] = null;
|
||||
} else {
|
||||
if (\is_array($controller)) {
|
||||
$repr = sprintf('%s::%s()', \get_class($controller[0]), $controller[1]);
|
||||
} elseif (\is_object($controller)) {
|
||||
$repr = \get_class($controller);
|
||||
} else {
|
||||
$repr = $controller;
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name));
|
||||
}
|
||||
}
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a callable for the given controller.
|
||||
*
|
||||
* @param string $controller A Controller string
|
||||
*
|
||||
* @return callable A PHP callable
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function createController($controller)
|
||||
{
|
||||
if (false === strpos($controller, '::')) {
|
||||
throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
|
||||
return $this->instantiateController($controller);
|
||||
}
|
||||
|
||||
list($class, $method) = explode('::', $controller, 2);
|
||||
|
||||
if (!class_exists($class)) {
|
||||
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
|
||||
}
|
||||
try {
|
||||
return array($this->instantiateController($class), $method);
|
||||
} catch (\Error | \LogicException $e) {
|
||||
try {
|
||||
if ((new \ReflectionMethod($class, $method))->isStatic()) {
|
||||
return $class.'::'.$method;
|
||||
}
|
||||
} catch (\ReflectionException $reflectionException) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return array($this->instantiateController($class), $method);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,24 +137,25 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
|
||||
{
|
||||
if (\is_string($callable)) {
|
||||
if (false !== strpos($callable, '::')) {
|
||||
$callable = explode('::', $callable);
|
||||
}
|
||||
|
||||
if (class_exists($callable) && !method_exists($callable, '__invoke')) {
|
||||
return sprintf('Class "%s" does not have a method "__invoke".', $callable);
|
||||
}
|
||||
|
||||
if (!\function_exists($callable)) {
|
||||
$callable = explode('::', $callable, 2);
|
||||
} else {
|
||||
return sprintf('Function "%s" does not exist.', $callable);
|
||||
}
|
||||
}
|
||||
|
||||
if (!\is_array($callable)) {
|
||||
return sprintf('Invalid type for controller given, expected string or array, got "%s".', \gettype($callable));
|
||||
if (\is_object($callable)) {
|
||||
$availableMethods = $this->getClassMethodsWithoutMagicMethods($callable);
|
||||
$alternativeMsg = $availableMethods ? sprintf(' or use one of the available methods: "%s"', implode('", "', $availableMethods)) : '';
|
||||
|
||||
return sprintf('Controller class "%s" cannot be called without a method name. You need to implement "__invoke"%s.', \get_class($callable), $alternativeMsg);
|
||||
}
|
||||
|
||||
if (2 !== \count($callable)) {
|
||||
return 'Invalid format for controller, expected array(controller, method) or controller::method.';
|
||||
if (!\is_array($callable)) {
|
||||
return sprintf('Invalid type for controller given, expected string, array or object, got "%s".', \gettype($callable));
|
||||
}
|
||||
|
||||
if (!isset($callable[0]) || !isset($callable[1]) || 2 !== \count($callable)) {
|
||||
return 'Invalid array callable, expected array(controller, method).';
|
||||
}
|
||||
|
||||
list($controller, $method) = $callable;
|
||||
@@ -231,7 +170,7 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
|
||||
return sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className);
|
||||
}
|
||||
|
||||
$collection = get_class_methods($controller);
|
||||
$collection = $this->getClassMethodsWithoutMagicMethods($controller);
|
||||
|
||||
$alternatives = array();
|
||||
|
||||
@@ -255,4 +194,13 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
private function getClassMethodsWithoutMagicMethods($classOrObject)
|
||||
{
|
||||
$methods = get_class_methods($classOrObject);
|
||||
|
||||
return array_filter($methods, function (string $method) {
|
||||
return 0 !== strncmp($method, '__', 2);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user