Overview

Namespaces

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

Classes

  • AbstractContainer
  • AbstractResource
  • Account
  • CDNContainer
  • Container
  • ContainerMetadata
  • DataObject
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0
  7:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
  8:  */
  9: 
 10: namespace OpenCloud\ObjectStore\Resource;
 11: 
 12: use Guzzle\Http\Message\Response;
 13: use OpenCloud\Common\Base;
 14: use OpenCloud\Common\Service\AbstractService;
 15: 
 16: /**
 17:  * Abstract base class which implements shared functionality of ObjectStore 
 18:  * resources. Provides support, for example, for metadata-handling and other 
 19:  * features that are common to the ObjectStore components.
 20:  */
 21: abstract class AbstractResource extends Base
 22: {
 23:     const GLOBAL_METADATA_PREFIX = 'X';
 24: 
 25:     /**
 26:      * @var \OpenCloud\Common\Metadata
 27:      */
 28:     protected $metadata;
 29: 
 30:     /**
 31:      * @var string The FQCN of the metadata object used for the container.
 32:      */
 33:     protected $metadataClass = 'OpenCloud\\Common\\Metadata';
 34:     
 35:     /**
 36:      * @var AbstractService The service object.
 37:      */
 38:     protected $service;
 39:     
 40:     public function  __construct(AbstractService $service)
 41:     {
 42:         $this->service  = $service;
 43:         $this->metadata = new $this->metadataClass;
 44:     }
 45:     
 46:     public function getService()
 47:     {
 48:         return $this->service;
 49:     }
 50:     
 51:     public function getCdnService()
 52:     {
 53:         return $this->service->getCDNService();
 54:     }
 55:     
 56:     public function getClient()
 57:     {
 58:         return $this->service->getClient();
 59:     }
 60: 
 61:     /**
 62:      * Factory method that allows for easy instantiation from a Response object.
 63:      *
 64:      * @param Response        $response
 65:      * @param AbstractService $service
 66:      * @return static
 67:      */
 68:     public static function fromResponse(Response $response, AbstractService $service)
 69:     {
 70:         $object = new static($service);
 71:         
 72:         if (null !== ($headers = $response->getHeaders())) {
 73:             $object->setMetadata($headers, true);
 74:         }
 75:         
 76:         return $object;
 77:     }
 78: 
 79:     /**
 80:      * Trim headers of their resource-specific prefixes.
 81:      *
 82:      * @param  $headers
 83:      * @return array
 84:      */
 85:     public static function trimHeaders($headers)
 86:     {
 87:         $output = array();
 88:         
 89:         foreach ($headers as $header => $value) {
 90:             // Only allow allow X-* headers to pass through after stripping them
 91:             if (preg_match('#^' . self::GLOBAL_METADATA_PREFIX . '\-#i', $header)
 92:                 && ($key = self::stripPrefix($header))
 93:             ) {
 94:                 $output[$key] = (string) $value;
 95:             }
 96:         }
 97: 
 98:         return $output;
 99:     }
100: 
101:     /**
102:      * Strip an individual header name of its resource-specific prefix.
103:      *
104:      * @param $header
105:      * @return mixed
106:      */
107:     private static function stripPrefix($header)
108:     {
109:         $pattern = '#^' . self::GLOBAL_METADATA_PREFIX . '\-(' . static::METADATA_LABEL . '-)?(Meta-)?#i';
110:         return preg_replace($pattern, '', $header);
111:     }
112: 
113:     /**
114:      * Prepend/stock the header names with a resource-specific prefix.
115:      *
116:      * @param array $headers
117:      * @return array
118:      */
119:     public static function stockHeaders(array $headers)
120:     {
121:         $output = array();
122:         foreach ($headers as $header => $value) {
123:             $prefix = self::GLOBAL_METADATA_PREFIX . '-' . static::METADATA_LABEL . '-Meta-';
124:             $output[$prefix . $header] = $value;
125:         }
126:         return $output;
127:     }
128: 
129:     /**
130:      * Set the metadata (local-only) for this object.
131:      *
132:      * @param      $data
133:      * @param bool $constructFromResponse
134:      * @return $this
135:      */
136:     public function setMetadata($data, $constructFromResponse = false)
137:     {
138:         if ($constructFromResponse) {
139:             $metadata = new $this->metadataClass;
140:             $metadata->setArray(self::trimHeaders($data));
141:             $data = $metadata;
142:         }
143: 
144:         $this->metadata = $data;
145:         return $this;
146:     }
147: 
148:     /**
149:      * @return \OpenCloud\Common\Metadata
150:      */
151:     public function getMetadata()
152:     {
153:         return $this->metadata;
154:     }
155: 
156:     /**
157:      * Push local metadata to the API, thereby executing a permanent save.
158:      *
159:      * @param array $metadata
160:      * @return mixed
161:      */
162:     public function saveMetadata(array $metadata)
163:     {
164:         $headers = self::stockHeaders($metadata);
165:         return $this->getClient()->post($this->getUrl(), $headers)->send();
166:     }
167: 
168:     /**
169:      * Retrieve metadata from the API. This method will then set and return this value.
170:      *
171:      * @return \OpenCloud\Common\Metadata
172:      */
173:     public function retrieveMetadata()
174:     {
175:         $response = $this->getClient()
176:             ->head($this->getUrl())
177:             ->send();
178:         
179:         $this->setMetadata($response->getHeaders(), true);
180:         return $this->metadata;
181:     }
182: 
183:     /**
184:      * To delete or unset a particular metadata item.
185:      *
186:      * @param $key
187:      * @return mixed
188:      */
189:     public function unsetMetadataItem($key)
190:     {
191:         $header = sprintf('%s-Remove-%s-Meta-%s', self::GLOBAL_METADATA_PREFIX, 
192:             static::METADATA_LABEL, $key);
193:         
194:         return $this->getClient()
195:             ->post($this->getUrl(), array($header => 'True'))
196:             ->send();
197:     }
198: 
199:     /**
200:      * Append a particular array of values to the existing metadata. Analogous to a merge.
201:      *
202:      * @param array $values
203:      * @return array
204:      */
205:     public function appendToMetadata(array $values)
206:     {
207:         return (!empty($this->metadata) && is_array($this->metadata)) 
208:             ? array_merge($this->metadata, $values)
209:             : $values;
210:     }
211:     
212: }
213: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0