/lib/OpenCloud/ObjectStore/Resource/AbstractResource.php

https://github.com/unarmedwombat/php-opencloud · PHP · 227 lines · 108 code · 35 blank · 84 comment · 5 complexity · b78470950445e4c7da6c47e39d6ed2a0 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2012-2014 Rackspace US, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace OpenCloud\ObjectStore\Resource;
  18. use Guzzle\Http\Message\Response;
  19. use OpenCloud\Common\Base;
  20. use OpenCloud\Common\Service\ServiceInterface;
  21. /**
  22. * Abstract base class which implements shared functionality of ObjectStore
  23. * resources. Provides support, for example, for metadata-handling and other
  24. * features that are common to the ObjectStore components.
  25. */
  26. abstract class AbstractResource extends Base
  27. {
  28. const GLOBAL_METADATA_PREFIX = 'X';
  29. /** @var \OpenCloud\Common\Metadata */
  30. protected $metadata;
  31. /** @var string The FQCN of the metadata object used for the container. */
  32. protected $metadataClass = 'OpenCloud\\Common\\Metadata';
  33. /** @var \OpenCloud\Common\Service\ServiceInterface The service object. */
  34. protected $service;
  35. public function __construct(ServiceInterface $service)
  36. {
  37. $this->service = $service;
  38. $this->metadata = new $this->metadataClass;
  39. }
  40. public function getService()
  41. {
  42. return $this->service;
  43. }
  44. public function getCdnService()
  45. {
  46. return $this->service->getCDNService();
  47. }
  48. public function getClient()
  49. {
  50. return $this->service->getClient();
  51. }
  52. /**
  53. * Factory method that allows for easy instantiation from a Response object.
  54. *
  55. * @param Response $response
  56. * @param ServiceInterface $service
  57. * @return static
  58. */
  59. public static function fromResponse(Response $response, ServiceInterface $service)
  60. {
  61. $object = new static($service);
  62. if (null !== ($headers = $response->getHeaders())) {
  63. $object->setMetadata($headers, true);
  64. }
  65. return $object;
  66. }
  67. /**
  68. * Trim headers of their resource-specific prefixes.
  69. *
  70. * @param $headers
  71. * @return array
  72. */
  73. public static function trimHeaders($headers)
  74. {
  75. $output = array();
  76. foreach ($headers as $header => $value) {
  77. // Only allow allow X-<keyword>-* headers to pass through after stripping them
  78. if (static::headerIsValidMetadata($header) && ($key = self::stripPrefix($header))) {
  79. $output[$key] = (string)$value;
  80. }
  81. }
  82. return $output;
  83. }
  84. protected static function headerIsValidMetadata($header)
  85. {
  86. $pattern = sprintf('#^%s\-#i', self::GLOBAL_METADATA_PREFIX);
  87. return preg_match($pattern, $header);
  88. }
  89. /**
  90. * Strip an individual header name of its resource-specific prefix.
  91. *
  92. * @param $header
  93. * @return mixed
  94. */
  95. protected static function stripPrefix($header)
  96. {
  97. $pattern = '#^' . self::GLOBAL_METADATA_PREFIX . '\-(' . static::METADATA_LABEL . '-)?(Meta-)?#i';
  98. return preg_replace($pattern, '', $header);
  99. }
  100. /**
  101. * Prepend/stock the header names with a resource-specific prefix.
  102. *
  103. * @param array $headers
  104. * @return array
  105. */
  106. public static function stockHeaders(array $headers)
  107. {
  108. $output = array();
  109. foreach ($headers as $header => $value) {
  110. $prefix = self::GLOBAL_METADATA_PREFIX . '-' . static::METADATA_LABEL . '-Meta-';
  111. $output[$prefix . $header] = $value;
  112. }
  113. return $output;
  114. }
  115. /**
  116. * Set the metadata (local-only) for this object.
  117. *
  118. * @param $data
  119. * @param bool $constructFromResponse
  120. * @return $this
  121. */
  122. public function setMetadata($data, $constructFromResponse = false)
  123. {
  124. if ($constructFromResponse) {
  125. $metadata = new $this->metadataClass;
  126. $metadata->setArray(self::trimHeaders($data));
  127. $data = $metadata;
  128. }
  129. $this->metadata = $data;
  130. return $this;
  131. }
  132. /**
  133. * @return \OpenCloud\Common\Metadata
  134. */
  135. public function getMetadata()
  136. {
  137. return $this->metadata;
  138. }
  139. /**
  140. * Push local metadata to the API, thereby executing a permanent save.
  141. *
  142. * @param array $metadata The array of values you want to set as metadata
  143. * @param bool $stockPrefix Whether to prepend each array key with the metadata-specific prefix. For objects, this
  144. * would be X-Object-Meta-Foo => Bar
  145. * @return mixed
  146. */
  147. public function saveMetadata(array $metadata, $stockPrefix = true)
  148. {
  149. $headers = ($stockPrefix === true) ? self::stockHeaders($metadata) : $metadata;
  150. return $this->getClient()->post($this->getUrl(), $headers)->send();
  151. }
  152. /**
  153. * Retrieve metadata from the API. This method will then set and return this value.
  154. *
  155. * @return \OpenCloud\Common\Metadata
  156. */
  157. public function retrieveMetadata()
  158. {
  159. $response = $this->getClient()
  160. ->head($this->getUrl())
  161. ->send();
  162. $this->setMetadata($response->getHeaders(), true);
  163. return $this->metadata;
  164. }
  165. /**
  166. * To delete or unset a particular metadata item.
  167. *
  168. * @param $key
  169. * @return mixed
  170. */
  171. public function unsetMetadataItem($key)
  172. {
  173. $header = sprintf('%s-Remove-%s-Meta-%s', self::GLOBAL_METADATA_PREFIX,
  174. static::METADATA_LABEL, $key);
  175. $headers = array($header => 'True');
  176. return $this->getClient()
  177. ->post($this->getUrl(), $headers)
  178. ->send();
  179. }
  180. /**
  181. * Append a particular array of values to the existing metadata. Analogous to a merge.
  182. *
  183. * @param array $values
  184. * @return array
  185. */
  186. public function appendToMetadata(array $values)
  187. {
  188. return (!empty($this->metadata) && is_array($this->metadata))
  189. ? array_merge($this->metadata, $values)
  190. : $values;
  191. }
  192. }