Overview
  • Namespace
  • Class

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Collection
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Log
      • Resource
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Collection
      • Resource
    • Identity
      • Constants
      • Resource
    • Image
      • Enum
      • Resource
        • JsonPatch
        • Schema
    • LoadBalancer
      • Collection
      • Enum
      • Resource
    • Networking
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
      • Resource
    • Queues
      • Collection
      • Exception
      • Resource
    • Volume
      • Resource

Classes

  • OpenCloud\Volume\Resource\Snapshot
  • OpenCloud\Volume\Resource\Volume
  • OpenCloud\Volume\Resource\VolumeType
  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 
 18 namespace OpenCloud\ObjectStore\Resource;
 19 
 20 use Guzzle\Http\Message\Response;
 21 use OpenCloud\Common\Base;
 22 use OpenCloud\Common\Service\ServiceInterface;
 23 
 24 /**
 25  * Abstract base class which implements shared functionality of ObjectStore
 26  * resources. Provides support, for example, for metadata-handling and other
 27  * features that are common to the ObjectStore components.
 28  */
 29 abstract class AbstractResource extends Base
 30 {
 31     const GLOBAL_METADATA_PREFIX = 'X';
 32 
 33     /** @var \OpenCloud\Common\Metadata */
 34     protected $metadata;
 35 
 36     /** @var string The FQCN of the metadata object used for the container. */
 37     protected $metadataClass = 'OpenCloud\\Common\\Metadata';
 38 
 39     /** @var \OpenCloud\Common\Service\ServiceInterface The service object. */
 40     protected $service;
 41 
 42     public function __construct(ServiceInterface $service)
 43     {
 44         $this->service = $service;
 45         $this->metadata = new $this->metadataClass;
 46     }
 47 
 48     public function getService()
 49     {
 50         return $this->service;
 51     }
 52 
 53     public function getCdnService()
 54     {
 55         return $this->service->getCDNService();
 56     }
 57 
 58     public function getClient()
 59     {
 60         return $this->service->getClient();
 61     }
 62 
 63     /**
 64      * Factory method that allows for easy instantiation from a Response object.
 65      *
 66      * @param Response         $response
 67      * @param ServiceInterface $service
 68      * @return static
 69      */
 70     public static function fromResponse(Response $response, ServiceInterface $service)
 71     {
 72         $object = new static($service);
 73 
 74         if (null !== ($headers = $response->getHeaders())) {
 75             $object->setMetadata($headers, true);
 76         }
 77 
 78         return $object;
 79     }
 80 
 81     /**
 82      * Trim headers of their resource-specific prefixes.
 83      *
 84      * @param  $headers
 85      * @return array
 86      */
 87     public static function trimHeaders($headers)
 88     {
 89         $output = array();
 90 
 91         foreach ($headers as $header => $value) {
 92             // Only allow allow X-<keyword>-* headers to pass through after stripping them
 93             if (static::headerIsValidMetadata($header) && ($key = self::stripPrefix($header))) {
 94                 $output[$key] = (string) $value;
 95             }
 96         }
 97 
 98         return $output;
 99     }
100 
101     protected static function headerIsValidMetadata($header)
102     {
103         $pattern = sprintf('#^%s\-#i', self::GLOBAL_METADATA_PREFIX);
104 
105         return preg_match($pattern, $header);
106     }
107 
108     /**
109      * Strip an individual header name of its resource-specific prefix.
110      *
111      * @param $header
112      * @return mixed
113      */
114     protected static function stripPrefix($header)
115     {
116         $pattern = '#^' . self::GLOBAL_METADATA_PREFIX . '\-(' . static::METADATA_LABEL . '-)?(Meta-)?#i';
117 
118         return preg_replace($pattern, '', $header);
119     }
120 
121     /**
122      * Prepend/stock the header names with a resource-specific prefix.
123      *
124      * @param array $headers
125      * @return array
126      */
127     public static function stockHeaders(array $headers)
128     {
129         $output = array();
130         $prefix = null;
131         $corsHeaders = array(
132             'Access-Control-Allow-Origin',
133             'Access-Control-Expose-Headers',
134             'Access-Control-Max-Age',
135             'Access-Control-Allow-Credentials',
136             'Access-Control-Allow-Methods',
137             'Access-Control-Allow-Headers'
138         );
139         foreach ($headers as $header => $value) {
140             if (!in_array($header, $corsHeaders)) {
141                 $prefix = self::GLOBAL_METADATA_PREFIX . '-' . static::METADATA_LABEL . '-Meta-';
142             }
143             $output[$prefix . $header] = $value;
144         }
145 
146         return $output;
147     }
148 
149     /**
150      * Set the metadata (local-only) for this object.
151      *
152      * @param      $data
153      * @param bool $constructFromResponse
154      * @return $this
155      */
156     public function setMetadata($data, $constructFromResponse = false)
157     {
158         if ($constructFromResponse) {
159             $metadata = new $this->metadataClass;
160             $metadata->setArray(self::trimHeaders($data));
161             $data = $metadata;
162         }
163 
164         $this->metadata = $data;
165 
166         return $this;
167     }
168 
169     /**
170      * @return \OpenCloud\Common\Metadata
171      */
172     public function getMetadata()
173     {
174         return $this->metadata;
175     }
176 
177     /**
178      * Push local metadata to the API, thereby executing a permanent save.
179      *
180      * @param array $metadata    The array of values you want to set as metadata
181      * @param bool  $stockPrefix Whether to prepend each array key with the metadata-specific prefix. For objects, this
182      *                           would be X-Object-Meta-Foo => Bar
183      * @return mixed
184      */
185     public function saveMetadata(array $metadata, $stockPrefix = true)
186     {
187         $headers = ($stockPrefix === true) ? self::stockHeaders($metadata) : $metadata;
188 
189         return $this->getClient()->post($this->getUrl(), $headers)->send();
190     }
191 
192     /**
193      * Retrieve metadata from the API. This method will then set and return this value.
194      *
195      * @return \OpenCloud\Common\Metadata
196      */
197     public function retrieveMetadata()
198     {
199         $response = $this->getClient()
200             ->head($this->getUrl())
201             ->send();
202 
203         $this->setMetadata($response->getHeaders(), true);
204 
205         return $this->metadata;
206     }
207 
208     /**
209      * To delete or unset a particular metadata item.
210      *
211      * @param $key
212      * @return mixed
213      */
214     public function unsetMetadataItem($key)
215     {
216         $header = sprintf('%s-Remove-%s-Meta-%s', self::GLOBAL_METADATA_PREFIX,
217             static::METADATA_LABEL, $key);
218 
219         $headers = array($header => 'True');
220 
221         return $this->getClient()
222             ->post($this->getUrl(), $headers)
223             ->send();
224     }
225 
226     /**
227      * Append a particular array of values to the existing metadata. Analogous to a merge.
228      *
229      * @param array $values
230      * @return array
231      */
232     public function appendToMetadata(array $values)
233     {
234         return (!empty($this->metadata) && is_array($this->metadata))
235             ? array_merge($this->metadata, $values)
236             : $values;
237     }
238 }
239 
API documentation generated by ApiGen