Skip to content
Snippets Groups Projects
Commit 9a7d466b authored by Adriano Cori's avatar Adriano Cori Committed by Adriano Cori
Browse files

Issue #2943264 by aronne: Fixed required properties management.

parent 86b22adc
No related branches found
No related tags found
No related merge requests found
......@@ -44,7 +44,7 @@ function http_client_manager_help($route_name, RouteMatchInterface $route_match)
'#type' => 'details',
'#title' => t('Help - Overriding HTTP Services API definitions'),
'#description' => t('HTTP Services API definitions can be overridden in settings.php. Overridable properties are: @list', [
'@list' => implode(', ', HttpServiceApiHandler::getOverridableProperties()),
'@list' => implode(', ', array_keys(HttpServiceApiHandler::getOverridableProperties())),
]),
'code' => [
'#markup' => $code,
......
......@@ -187,6 +187,15 @@ class HttpClient implements HttpClientInterface {
$this->client = $this->createGuzzleClient($config);
}
/**
* Create a new Guzzle Client by config.
*
* @param array $config
* An array of configurations used to create a new Guzzle Client.
*
* @return \GuzzleHttp\Command\Guzzle\GuzzleClient
* A Guzzle Client instance.
*/
private function createGuzzleClient(array $config) {
$client = new Client($config);
return new GuzzleClient($client, $this->loadServiceDescription($config));
......
......@@ -16,6 +16,11 @@ use Drupal\Core\Controller\ControllerResolver;
*/
class HttpServiceApiHandler implements HttpServiceApiHandlerInterface {
/**
* Defines the required property value.
*/
const REQUIRED_PROPERTY = TRUE;
/**
* Drupal root.
*
......@@ -29,12 +34,14 @@ class HttpServiceApiHandler implements HttpServiceApiHandlerInterface {
* @var \Drupal\Core\Extension\ModuleHandler
*/
protected $moduleHandler;
/**
* Drupal\Core\StringTranslation\TranslationManager definition.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $stringTranslation;
/**
* Drupal\Core\Controller\ControllerResolver definition.
*
......@@ -196,7 +203,7 @@ class HttpServiceApiHandler implements HttpServiceApiHandlerInterface {
}
$original = $serviceApi;
$overrides = array_flip(self::getOverridableProperties());
$overrides = self::getOverridableProperties();
$settings[$id] = array_intersect_key($settings[$id], $overrides);
$serviceApi = array_replace_recursive($serviceApi, $settings[$id]);
......@@ -210,14 +217,15 @@ class HttpServiceApiHandler implements HttpServiceApiHandlerInterface {
* Get overridable Service API properties.
*
* @return array
* An array containing a list of overridable property names.
* An associative array where keys are overridable property names and values
* are boolean indicating if the property is required or not.
*/
public static function getOverridableProperties() {
return [
'title',
'api_path',
'config',
'commands',
'title' => self::REQUIRED_PROPERTY,
'api_path' => self::REQUIRED_PROPERTY,
'config' => self::REQUIRED_PROPERTY,
'commands' => !self::REQUIRED_PROPERTY,
];
}
......@@ -233,8 +241,8 @@ class HttpServiceApiHandler implements HttpServiceApiHandlerInterface {
* Whether or not the api is valid.
*/
protected function validateServiceApiDefinition($id, array $serviceApi) {
foreach (self::getOverridableProperties() as $property) {
if (!isset($serviceApi[$property])) {
foreach (self::getOverridableProperties() as $property => $isRequired) {
if ($isRequired && !isset($serviceApi[$property])) {
$message = sprintf('Missing required parameter "%s" in "%s" service api definition', $property, $id);
throw new \RuntimeException($message);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment