summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Service
diff options
context:
space:
mode:
authorEoleDev <root@serverpc.home>2016-03-09 16:17:33 +0100
committerEoleDev <root@serverpc.home>2016-03-09 16:17:33 +0100
commit2b8decb81faeb7928bcbfda84c6f33a003f707fd (patch)
tree0e491d7ae2bf91347b1cf50d2c475625d8f659b4 /server/vendor/php-opencloud/common/src/Common/Service
parentb7ebe1272c1127df290535af2430622b28160bb0 (diff)
parent03ef74d0cfe675a6e18a91f039182ca1b248d8f5 (diff)
Maj Library
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Service')
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Service/AbstractService.php14
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Service/Builder.php170
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Service/ServiceInterface.php14
3 files changed, 198 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Service/AbstractService.php b/server/vendor/php-opencloud/common/src/Common/Service/AbstractService.php
new file mode 100644
index 0000000..f24e684
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Service/AbstractService.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace OpenCloud\Common\Service;
+
+use OpenCloud\Common\Api\Operator;
+
+/**
+ * Represents the top-level abstraction of a service.
+ *
+ * @package OpenCloud\Common\Service
+ */
+abstract class AbstractService extends Operator implements ServiceInterface
+{
+}
diff --git a/server/vendor/php-opencloud/common/src/Common/Service/Builder.php b/server/vendor/php-opencloud/common/src/Common/Service/Builder.php
new file mode 100644
index 0000000..0035070
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Service/Builder.php
@@ -0,0 +1,170 @@
+<?php
+
+namespace OpenCloud\Common\Service;
+
+use GuzzleHttp\Client;
+use GuzzleHttp\ClientInterface;
+use GuzzleHttp\Middleware as GuzzleMiddleware;
+use OpenCloud\Common\Auth\Token;
+use OpenCloud\Common\Transport\HandlerStack;
+use OpenCloud\Common\Transport\Middleware;
+use OpenCloud\Common\Transport\Utils;
+use OpenCloud\Identity\v3\Service;
+
+/**
+ * A Builder for easily creating OpenCloud services.
+ *
+ * @package OpenCloud\Common\Service
+ */
+class Builder
+{
+ /**
+ * Global options that will be applied to every service created by this builder.
+ *
+ * @var array
+ */
+ private $globalOptions = [];
+
+ /** @var string */
+ private $rootNamespace;
+
+ /**
+ * Defaults that will be applied to options if no values are provided by the user.
+ *
+ * @var array
+ */
+ private $defaults = ['urlType' => 'publicURL'];
+
+ /**
+ * @param array $globalOptions Options that will be applied to every service created by this builder.
+ * Eventually they will be merged (and if necessary overridden) by the
+ * service-specific options passed in.
+ */
+ public function __construct(array $globalOptions = [], $rootNamespace = 'OpenCloud')
+ {
+ $this->globalOptions = $globalOptions;
+ $this->rootNamespace = $rootNamespace;
+ }
+
+ /**
+ * Internal method which resolves the API and Service classes for a service.
+ *
+ * @param string $serviceName The name of the service, e.g. Compute
+ * @param int $serviceVersion The major version of the service, e.g. 2
+ *
+ * @return array
+ */
+ private function getClasses($serviceName, $serviceVersion)
+ {
+ $rootNamespace = sprintf("%s\\%s\\v%d", $this->rootNamespace, $serviceName, $serviceVersion);
+
+ return [
+ sprintf("%s\\Api", $rootNamespace),
+ sprintf("%s\\Service", $rootNamespace),
+ ];
+ }
+
+ /**
+ * This method will return an OpenCloud service ready fully built and ready for use. There is
+ * some initial setup that may prohibit users from directly instantiating the service class
+ * directly - this setup includes the configuration of the HTTP client's base URL, and the
+ * attachment of an authentication handler.
+ *
+ * @param $serviceName The name of the service as it appears in the OpenCloud\* namespace
+ * @param $serviceVersion The major version of the service
+ * @param array $serviceOptions The service-specific options to use
+ *
+ * @return \OpenCloud\Common\Service\ServiceInterface
+ *
+ * @throws \Exception
+ */
+ public function createService($serviceName, $serviceVersion, array $serviceOptions = [])
+ {
+ $options = $this->mergeOptions($serviceOptions);
+
+ $this->stockIdentityService($options);
+ $this->stockAuthHandler($options);
+ $this->stockHttpClient($options, $serviceName);
+
+ list($apiClass, $serviceClass) = $this->getClasses($serviceName, $serviceVersion);
+
+ return new $serviceClass($options['httpClient'], new $apiClass());
+ }
+
+ private function stockHttpClient(array &$options, $serviceName)
+ {
+ if (!isset($options['httpClient']) || !($options['httpClient'] instanceof ClientInterface)) {
+ if (strcasecmp($serviceName, 'identity') === 0) {
+ $baseUrl = $options['authUrl'];
+ $stack = $this->getStack($options['authHandler']);
+ } else {
+ list($token, $baseUrl) = $options['identityService']->authenticate($options);
+ $stack = $this->getStack($options['authHandler'], $token);
+ }
+
+ $this->addDebugMiddleware($options, $stack);
+
+ $options['httpClient'] = $this->httpClient($baseUrl, $stack);
+ }
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ private function addDebugMiddleware(array $options, HandlerStack &$stack)
+ {
+ if (!empty($options['debugLog'])
+ && !empty($options['logger'])
+ && !empty($options['messageFormatter'])
+ ) {
+ $stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter']));
+ }
+ }
+
+ private function stockIdentityService(array &$options)
+ {
+ if (!isset($options['identityService'])) {
+ $httpClient = $this->httpClient($options['authUrl'], HandlerStack::create());
+ $options['identityService'] = Service::factory($httpClient);
+ }
+ }
+
+ /**
+ * @param array $options
+ * @codeCoverageIgnore
+ */
+ private function stockAuthHandler(array &$options)
+ {
+ if (!isset($options['authHandler'])) {
+ $options['authHandler'] = function () use ($options) {
+ return $options['identityService']->generateToken($options);
+ };
+ }
+ }
+
+ private function getStack(callable $authHandler, Token $token = null)
+ {
+ $stack = HandlerStack::create();
+ $stack->push(Middleware::authHandler($authHandler, $token));
+ return $stack;
+ }
+
+ private function httpClient($baseUrl, HandlerStack $stack)
+ {
+ return new Client([
+ 'base_uri' => Utils::normalizeUrl($baseUrl),
+ 'handler' => $stack,
+ ]);
+ }
+
+ private function mergeOptions(array $serviceOptions)
+ {
+ $options = array_merge($this->defaults, $this->globalOptions, $serviceOptions);
+
+ if (!isset($options['authUrl'])) {
+ throw new \InvalidArgumentException('"authUrl" is a required option');
+ }
+
+ return $options;
+ }
+}
diff --git a/server/vendor/php-opencloud/common/src/Common/Service/ServiceInterface.php b/server/vendor/php-opencloud/common/src/Common/Service/ServiceInterface.php
new file mode 100644
index 0000000..6ad3089
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Service/ServiceInterface.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace OpenCloud\Common\Service;
+
+use OpenCloud\Common\Api\OperatorInterface;
+
+/**
+ * Service interface.
+ *
+ * @package OpenCloud\Common\Service
+ */
+interface ServiceInterface extends OperatorInterface
+{
+}