ServiceResource.php in Services 8.4
File
src/Entity/ServiceResource.php
View source
<?php
namespace Drupal\services\Entity;
use Drupal\Core\Authentication\AuthenticationProviderInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\services\ServiceResourceInterface;
class ServiceResource extends ConfigEntityBase implements ServiceResourceInterface {
protected $id;
protected $service_plugin_id;
protected $service_endpoint_id;
protected $formats = [];
protected $authentication = [];
protected $no_cache = NULL;
public function id() {
return $this->service_endpoint_id . '.' . strtr($this->service_plugin_id, ':', '.');
}
public function label() {
if ($service_plugin = $this
->getServicePlugin()) {
return $service_plugin['title'];
}
}
public function getFormats() {
if (!empty($this->formats)) {
return array_filter($this->formats);
}
return $this
->getDefaultSettings()
->get('default_formats');
}
public function getAuthentication() {
if (!empty($this->authentication)) {
return array_filter($this->authentication);
}
return $this
->getDefaultSettings()
->get('default_authentication');
}
public function getNoCache() {
if (isset($this->no_cache)) {
return $this->no_cache;
}
return $this
->getDefaultSettings()
->get('default_no_cache');
}
public function getServicePlugin() {
if (!$this
->hasServicePlugin()) {
return FALSE;
}
return $this
->serviceDefinition()
->getDefinition($this->service_plugin_id);
}
public function createServicePluginInstance(array $values = []) {
if (!$this
->hasServicePlugin()) {
return FALSE;
}
return $this
->serviceDefinition()
->createInstance($this->service_plugin_id, $values);
}
public function hasServicePlugin() {
if (!isset($this->service_plugin_id)) {
return FALSE;
}
return $this
->serviceDefinition()
->hasDefinition($this->service_plugin_id);
}
public function getEndpoint() {
return $this
->entityTypeManager()
->getStorage('service_endpoint')
->load($this->service_endpoint_id);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
\Drupal::service('router.builder')
->setRebuildNeeded();
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
\Drupal::service('router.builder')
->setRebuildNeeded();
}
public function getDefaultSettings() {
return $this
->getConfigManager()
->getConfigFactory()
->get('services.settings');
}
public function calculateDependencies() {
parent::calculateDependencies();
if ($this instanceof ServiceResourceInterface) {
foreach ($this
->getAuthentication() as $provider_id) {
$provider = $this
->authenticationCollector()
->getProvider($provider_id);
if (!$provider instanceof AuthenticationProviderInterface) {
continue;
}
$class_info = explode('\\', get_class($provider));
$module_name = $class_info[1];
if (\Drupal::moduleHandler()
->moduleExists($module_name)) {
$this
->addDependency('module', $module_name);
}
}
}
return $this;
}
protected function serviceDefinition() {
return \Drupal::service('plugin.manager.services.service_definition');
}
protected function urlRouteParameters($rel) {
if (!in_array($rel, [
'collection',
'add-page',
'add-form',
], TRUE)) {
$uri_route_parameters['plugin_id'] = $this->service_plugin_id;
$uri_route_parameters['service_endpoint'] = $this->service_endpoint_id;
}
return $uri_route_parameters;
}
protected function authenticationCollector() {
return \Drupal::service('authentication_collector');
}
}