abstract class ApiTokenBase in API Tokens 8
Same name and namespace in other branches
- 8.2 src/ApiTokenBase.php \Drupal\api_tokens\ApiTokenBase
Provides a base class for the API token plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\api_tokens\ApiTokenBase implements ApiTokenPluginInterface uses RefinableCacheableDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ApiTokenBase
4 files declare their use of ApiTokenBase
- BlockApiToken.php in api_tokens_example/
src/ Plugin/ ApiToken/ BlockApiToken.php - DateApiToken.php in api_tokens_example/
src/ Plugin/ ApiToken/ DateApiToken.php - NodeApiToken.php in api_tokens_example/
src/ Plugin/ ApiToken/ NodeApiToken.php - UserLinkApiToken.php in api_tokens_example/
src/ Plugin/ ApiToken/ UserLinkApiToken.php
File
- src/
ApiTokenBase.php, line 17
Namespace
Drupal\api_tokensView source
abstract class ApiTokenBase extends PluginBase implements ApiTokenPluginInterface {
use RefinableCacheableDependencyTrait;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The API tokens logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The API token string.
*
* @var string
*/
protected $token;
/**
* The API token parameters string.
*
* @var string
*/
protected $paramString;
/**
* The API token parameters.
*
* @var array
*/
protected $params;
/**
* The API token parameters hash.
*
* @var string
*/
protected $hash;
/**
* The API token build method reflection object.
*
* @var \ReflectionMethod|null
*/
protected $reflector;
/**
* The API token render context.
*
* @var string[]
*/
protected static $context = [];
/**
* Constructs an ApiTokenBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Psr\Log\LoggerInterface $logger
* The API tokens logger.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, RendererInterface $renderer, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
$this->logger = $logger;
$this->paramString = isset($configuration['params']) ? $configuration['params'] : '';
$this->token = "[api:{$plugin_id}{$this->paramString}/]";
$this->params = $this->paramString ? Json::decode($this->paramString) : [];
$this->hash = $this->params ? hash('crc32b', serialize($this->params)) : '';
$this->reflector = method_exists($this, 'build') ? new \ReflectionMethod($this, 'build') : NULL;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('renderer'), $container
->get('logger.factory')
->get('api_tokens'));
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->pluginId;
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->pluginDefinition['label'];
}
/**
* {@inheritdoc}
*/
public function description() {
return $this->pluginDefinition['description'];
}
/**
* {@inheritdoc}
*/
public function provider() {
return $this->pluginDefinition['provider'];
}
/**
* {@inheritdoc}
*/
public function token() {
return $this->token;
}
/**
* {@inheritdoc}
*/
public function paramString() {
return $this->paramString;
}
/**
* {@inheritdoc}
*/
public function params() {
return $this->params;
}
/**
* {@inheritdoc}
*/
public function hash() {
return $this->hash;
}
/**
* {@inheritdoc}
*/
public function reflector() {
return $this->reflector;
}
/**
* {@inheritdoc}
*/
public function validateToken() {
if (!$this->reflector) {
$this->logger
->warning($this
->t('ApiToken plugin "@label" has no "build" method.', [
'@label' => $this
->label(),
]));
return FALSE;
}
if (!is_array($this->params)) {
$this->logger
->warning($this
->t('API token "@token" has invalid parameters format.', [
'@token' => $this->token,
]));
return FALSE;
}
if ($this->reflector
->getNumberOfRequiredParameters() > count($this->params)) {
$this->logger
->warning($this
->t('API token "@token" has not enough parameters.', [
'@token' => $this->token,
]));
return FALSE;
}
$params = [];
foreach ($this->reflector
->getParameters() as $index => $param) {
$use_default = $param
->isOptional() && !isset($this->params[$index]);
$params[$param
->getName()] = $use_default ? $param
->getDefaultValue() : $this->params[$index];
}
$provided_count = count($this->params);
$defined_count = count($params);
if ($provided_count > $defined_count) {
for ($index = $defined_count; $index < $provided_count; ++$index) {
$params[$index] = $this->params[$index];
}
}
if (!$this
->validate($params)) {
$this->logger
->warning($this
->t('API token "@token" has invalid parameters.', [
'@token' => $this->token,
]));
return FALSE;
}
return TRUE;
}
/**
* Validates the API token parameters.
*
* This validation must be context-independent. For example, if some parameter
* is expected to be an entity ID, check only whether it is a valid entity ID,
* but don't check the entity existence/access (these must be checked in the
* build method).
*
* @param array $params
* An array of named API token parameters. If an API token has more
* parameters then it is defined in build method, extra parameters will be
* named by parameter index. For example, if we have the API token
* [api:example[123, ["option1", "option2"], "extra1", "extra2"]/],
* and plugin's build method argument definition is: ...($id, $options),
* the $params will be:
* @code
* [
* 'id' => 123,
* 'options' => ['option1', 'option2'],
* '2' => 'extra1',
* '3' => 'extra2',
* ];
* @endcode
*
* @return bool
*
* @see \Drupal\api_tokens\ApiTokenPluginInterface::validateToken();
*/
public function validate(array $params) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function process() {
static $recursion = FALSE;
if ($recursion) {
return [];
}
$key = "{$this->pluginId}:{$this->hash}";
if (in_array($key, self::$context)) {
$recursion = TRUE;
$this->logger
->warning($this
->t('Recursion detected while rendering @token API token.', [
'@token' => $this->token,
]));
return [];
}
array_push(self::$context, $key);
$build = call_user_func_array([
$this,
'build',
], $this->params);
$this->moduleHandler
->alter('api_token_build', $build, $this);
$this->renderer
->renderPlain($build);
array_pop(self::$context);
if ($recursion) {
self::$context || ($recursion = FALSE);
return [];
}
$this
->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
$build = [
'#markup' => $build['#markup'],
'#attached' => $build['#attached'],
'#cache' => [
'contexts' => $this
->getCacheContexts(),
'tags' => $this
->getCacheTags(),
'max-age' => $this
->getCacheMaxAge(),
],
];
return $build;
}
/**
* {@inheritdoc}
*/
public function fallback() {
return [];
}
/**
* {@inheritdoc}
*/
public function placeholder() {
$placeholder = [
'#lazy_builder' => [
self::class . '::lazyBuilder',
[
$this->pluginId,
$this->paramString,
$this
->validateToken(),
],
],
'#cache' => [
'keys' => !self::$context ? [
'api_token',
$this->pluginId,
$this->hash,
] : NULL,
],
];
return $placeholder;
}
/**
* {@inheritdoc}
*/
public static function lazyBuilder($id, $params, $valid) {
$plugin = \Drupal::service('plugin.manager.api_token')
->createInstance($id, [
'params' => $params,
]);
return $valid ? $plugin
->process() : $plugin
->fallback();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ApiTokenBase:: |
protected static | property | The API token render context. | |
ApiTokenBase:: |
protected | property | The API token parameters hash. | |
ApiTokenBase:: |
protected | property | The API tokens logger. | |
ApiTokenBase:: |
protected | property | The module handler service. | |
ApiTokenBase:: |
protected | property | The API token parameters. | |
ApiTokenBase:: |
protected | property | The API token parameters string. | |
ApiTokenBase:: |
protected | property | The API token build method reflection object. | |
ApiTokenBase:: |
protected | property | The renderer. | |
ApiTokenBase:: |
protected | property | The API token string. | |
ApiTokenBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the administrative description of the API token. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns a build to replace the API token with in case of validation fail. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the API token parameters hash. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the API token ID. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the administrative label of the API token. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public static | function |
The #lazy_builder callback. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the API token parameters. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the API token parameter string. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns a #lazy_builder placeholder for the API tokens filter. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns processed API token build. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the name of the provider that owns this API token. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the API token build method reflection object. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Returns the API token string. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function | Validates the API token parameters. | 3 |
ApiTokenBase:: |
public | function |
Performs one-time context-independent validation of the API token. Overrides ApiTokenPluginInterface:: |
|
ApiTokenBase:: |
public | function |
Constructs an ApiTokenBase object. Overrides PluginBase:: |
|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |