You are here

public function Resource::getControllerFromPath in RESTful 7.2

Return the controller for a given path.

Parameters

string $path: (optional) The path to use. If none is provided the path from the resource will be used.

ResourceInterface $resource: (optional) Use the passed in resource instead of $this. This is mainly used by decorator resources.

Return value

callable A callable as expected by ResourceManager::executeCallback.

Throws

BadRequestException

ForbiddenException

GoneException

NotImplementedException

ServerConfigurationException

Overrides ResourceInterface::getControllerFromPath

See also

ResourceManager::executeCallback()

1 call to Resource::getControllerFromPath()
Resource::process in src/Plugin/resource/Resource.php
Controller function that passes the data along and executes right action.

File

src/Plugin/resource/Resource.php, line 425
Contains \Drupal\restful\Plugin\resource\Resource.

Class

Resource

Namespace

Drupal\restful\Plugin\resource

Code

public function getControllerFromPath($path = NULL, ResourceInterface $resource = NULL) {
  if (empty($resource)) {
    $resource = $this;
  }
  $path = $path ?: $resource
    ->getPath();
  $method = $resource
    ->getRequest()
    ->getMethod();
  $selected_controller = NULL;
  foreach ($resource
    ->getControllers() as $pattern => $controllers) {

    // Find the controllers for the provided path.
    if ($pattern != $path && !($pattern && preg_match('/' . $pattern . '/', $path))) {
      continue;
    }
    if ($controllers === FALSE) {

      // Method isn't valid anymore, due to a deprecated API endpoint.
      $params = array(
        '@path' => $path,
      );
      throw new GoneException(format_string('The path @path endpoint is not valid.', $params));
    }
    if (!isset($controllers[$method])) {
      $params = array(
        '@method' => strtoupper($method),
      );
      throw new BadRequestException(format_string('The http method @method is not allowed for this path.', $params));
    }

    // We found the controller, so we can break.
    $selected_controller = $controllers[$method];
    if (is_array($selected_controller)) {

      // If there is a custom access method for this endpoint check it.
      if (!empty($selected_controller['access callback']) && !ResourceManager::executeCallback(array(
        $resource,
        $selected_controller['access callback'],
      ), array(
        $path,
      ))) {
        throw new ForbiddenException(sprintf('You do not have access to this endpoint: %s - %s', $method, $path));
      }
      $selected_controller = $selected_controller['callback'];
    }

    // Create the callable from the method string.
    if (!ResourceManager::isValidCallback($selected_controller)) {

      // This means that the provided value means to be a public method on the
      // current class.
      $selected_controller = array(
        $resource,
        $selected_controller,
      );
    }
    break;
  }
  if (empty($selected_controller)) {
    throw new NotImplementedException(sprintf('There is no handler for "%s" on the path: %s', $resource
      ->getRequest()
      ->getMethod(), $path));
  }
  return $selected_controller;
}