ServicesRESTServerFactory.inc in Services 7.3
File
servers/rest_server/includes/ServicesRESTServerFactory.inc
View source
<?php
class ServicesRESTServerFactory {
protected $data = array();
static $class_name = 'RESTServer';
public function __construct($data = array()) {
if (!isset($data['endpoint_path'])) {
throw new Exception('ServicesRESTServerFactory constructor requires "endpoint_data" property.');
}
$this->data = $data;
}
public function getRESTServer() {
$content_type_negotiator = $this
->getContentTypeNegotiator();
$context = $this
->getContext();
$resources = $this
->getResources();
$parsers = $this
->getParsers();
$formatters = $this
->getFormatters();
$class_name = static::$class_name;
return new $class_name($context, $content_type_negotiator, $resources, $parsers, $formatters);
}
protected function getContentTypeNegotiator() {
return new ServicesContentTypeNegotiator();
}
protected function getContext() {
$context = new ServicesContext($this->data['endpoint_path']);
$context
->buildFromGlobals();
return $context;
}
protected function getResources() {
$endpoint_name = services_get_server_info('endpoint', '');
$endpoint = services_endpoint_load($endpoint_name);
$resources = services_get_resources($endpoint->name);
module_load_include('inc', 'services', 'includes/services.resource_build');
_services_apply_endpoint($resources, $endpoint, TRUE);
return $resources;
}
protected function getEndpoint() {
$endpoint_name = services_get_server_info('endpoint', '');
return services_endpoint_load($endpoint_name);
}
protected function getEndpointSettings() {
static $settings;
if (empty($settings)) {
$endpoint = $this
->getEndpoint();
$settings = !empty($endpoint->server_settings) ? $endpoint->server_settings : array();
$settings = rest_server_setup_settings($settings);
}
return $settings;
}
protected function getParsers() {
$settings = $this
->getEndpointSettings();
$parsers = rest_server_request_parsers();
foreach (array_keys($parsers) as $key) {
if (!$settings['parsers'][$key]) {
unset($parsers[$key]);
}
}
return $parsers;
}
protected function getFormatters() {
$settings = $this
->getEndpointSettings();
$formatters = rest_server_response_formatters();
foreach (array_keys($formatters) as $key) {
if (!$settings['formatters'][$key]) {
unset($formatters[$key]);
}
}
return $formatters;
}
}