You are here

class JsonRpcMethod in JSON-RPC 8

Same name and namespace in other branches
  1. 2.x src/Annotation/JsonRpcMethod.php \Drupal\jsonrpc\Annotation\JsonRpcMethod

Defines a JsonRpcParameterDefinition annotation object.

Hierarchy

Expanded class hierarchy of JsonRpcMethod

See also

\Drupal\jsonrpc\Plugin\JsonRpcServiceManager

Plugin API

2 files declare their use of JsonRpcMethod
AnnotationNormalizer.php in modules/jsonrpc_discovery/src/Normalizer/AnnotationNormalizer.php
JsonRpcMethodManager.php in src/Plugin/JsonRpcMethodManager.php
1 string reference to 'JsonRpcMethod'
AnnotationNormalizer::getAnnotationType in modules/jsonrpc_discovery/src/Normalizer/AnnotationNormalizer.php
Extract the annotation type.
4 classes are annotated with JsonRpcMethod
AddPermissionToRole in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/AddPermissionToRole.php
A method to add permissions to a role.
ListPermissions in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/ListPermissions.php
RPC method to list all the permissions.
Plugins in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/Plugins.php
Lists the plugin definitions of a given type.
RouteBuilder in modules/jsonrpc_core/src/Plugin/jsonrpc/Method/RouteBuilder.php
RPC method to rebuild the routes.

File

src/Annotation/JsonRpcMethod.php, line 18

Namespace

Drupal\jsonrpc\Annotation
View source
class JsonRpcMethod extends AnnotationBase implements MethodInterface {

  /**
   * The access required to use this RPC method.
   *
   * @var mixed
   */
  public $access;

  /**
   * The class method to call.
   *
   * Optional. If the method ID is 'foo.bar', this defaults to 'bar'. If the
   * method ID does not contain a dot (.), defaults to 'execute'.
   *
   * @var string
   */
  public $call;

  /**
   * How to use this method.
   *
   * @var \Drupal\Core\Annotation\Translation
   *
   * @ingroup plugin_translatable
   */
  public $usage;

  /**
   * The parameters for this method.
   *
   * Can be a keyed array where the parameter names are the keys or an indexed
   * array for positional parameters.
   *
   * @var \Drupal\jsonrpc\Annotation\JsonRpcParameterDefinition[]
   */
  public $params = [];

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this
      ->getId();
  }

  /**
   * {@inheritdoc}
   */
  public function call() {
    if (!isset($this->call)) {
      $this->call = 'execute';
    }
    return $this->call;
  }

  /**
   * {@inheritdoc}
   */
  public function getUsage() {
    $this->usage;
  }

  /**
   * {@inheritdoc}
   */
  public function getParams() {
    return $this->params;
  }

  /**
   * {@inheritdoc}
   */
  public function areParamsPositional() {
    return array_reduce(array_keys($this
      ->getParams()), function ($positional, $key) {
      return $positional ? !is_string($key) : $positional;
    }, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function access($operation = 'execute', AccountInterface $account = NULL, $return_as_object = FALSE) {
    $account = $account ?: \Drupal::currentUser();
    switch ($operation) {
      case 'execute':
        if (is_callable($this->access)) {
          return call_user_func_array($this->access, [
            $operation,
            $account,
            $return_as_object,
          ]);
        }
        $access_result = AccessResult::allowed();
        foreach ($this->access as $permission) {
          $access_result = $access_result
            ->andIf(AccessResult::allowedIfHasPermission($account, $permission));
        }
        break;
      case 'view':
        $access_result = $this
          ->access('execute', $account, $return_as_object);
        break;
      default:
        $access_result = AccessResult::neutral();
        break;
    }
    return $return_as_object ? $access_result : $access_result
      ->isAllowed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotationBase::$class protected property The class used for this annotated class.
AnnotationBase::$id public property The annotated class ID. 1
AnnotationBase::$provider protected property The provider of the annotated class.
AnnotationBase::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass
AnnotationBase::getId public function Gets the unique ID for this annotated class. Overrides AnnotationInterface::getId 1
AnnotationBase::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider
AnnotationBase::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass
AnnotationBase::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider
JsonRpcMethod::$access public property The access required to use this RPC method.
JsonRpcMethod::$call public property The class method to call.
JsonRpcMethod::$params public property The parameters for this method.
JsonRpcMethod::$usage public property How to use this method.
JsonRpcMethod::access public function Checks data value access. Overrides AccessibleInterface::access
JsonRpcMethod::areParamsPositional public function Whether the parameters are by-position. Overrides MethodInterface::areParamsPositional
JsonRpcMethod::call public function The class method to call. Overrides MethodInterface::call
JsonRpcMethod::get public function Gets the value of an annotation. Overrides AnnotationInterface::get
JsonRpcMethod::getParams public function The parameters for this method. Overrides MethodInterface::getParams
JsonRpcMethod::getUsage public function How to use this method. Overrides MethodInterface::getUsage
JsonRpcMethod::id public function Gets the unique identifier of the plugin. Overrides PluginDefinitionInterface::id