ServiceEndpoint.php in Services 9.0.x
File
src/Routing/ServiceEndpoint.php
View source
<?php
namespace Drupal\services\Routing;
use Symfony\Component\Routing\Route;
class ServiceEndpoint {
public function routes() {
$routes = array();
foreach (\Drupal::entityTypeManager()
->getStorage('service_endpoint')
->loadMultiple() as $endpoint) {
foreach ($endpoint
->loadResourceProviders() as $resource) {
$instance = $resource
->createServicePluginInstance();
$parameters = [];
foreach ($instance
->getContextDefinitions() as $context_id => $context) {
$parameters[$context_id] = [
'type' => $context
->getDataType(),
];
}
$route = (new Route('/' . $endpoint
->getEndpoint() . '/' . $instance
->getPath()))
->setDefaults([
'_controller' => '\\Drupal\\services\\Controller\\Services::processRequest',
'service_endpoint_id' => $endpoint
->id(),
'service_definition_id' => $instance
->getPluginId(),
])
->setOptions([
'parameters' => $parameters,
'_auth' => $resource
->getAuthentication(),
])
->setMethods($instance
->getMethods());
if ($formats = $resource
->getFormats()) {
$route
->setRequirement('_format', implode('|', array_keys($formats)));
}
if ($resource
->getNoCache()) {
$route
->setOption('no_cache', TRUE);
}
$route
->setRequirement('_check_services_csrf', 'TRUE');
$instance
->processRoute($route);
$routes['services.endpoint.' . $resource
->id()] = $route;
}
}
return $routes;
}
}