You are here

public function RestfulBase::getControllerFromPath in RESTful 7

Return the controller from a given path.

Return value

string The appropriate method to call.

Throws

RestfulBadRequestException

RestfulException

RestfulForbiddenException

RestfulGoneException

1 call to RestfulBase::getControllerFromPath()
RestfulBase::process in plugins/restful/RestfulBase.php
Entry point to process a request.

File

plugins/restful/RestfulBase.php, line 842
Contains RestfulBase.

Class

RestfulBase
Class \RestfulBase

Code

public function getControllerFromPath() {
  $path = $this
    ->getPath();
  $method = $this
    ->getMethod();
  $selected_controller = NULL;
  foreach ($this
    ->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 RestfulGoneException(format_string('The path @path endpoint is not valid.', $params));
    }
    if (!isset($controllers[$method])) {
      $params = array(
        '@method' => strtoupper($method),
      );
      throw new RestfulBadRequestException(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']) && !static::executeCallback(array(
        $this,
        $selected_controller['access callback'],
      ), array(
        $path,
      ))) {
        throw new \RestfulForbiddenException(format_string('You do not have access to this endpoint: @method - @path', array(
          '@method' => $method,
          '@path' => $path,
        )));
      }
      $selected_controller = $selected_controller['callback'];
    }
    break;
  }
  return $selected_controller;
}