| 1 |
178
|
jakob |
<?php |
|
| 2 |
↓
|
jakob |
/** |
|
| 3 |
↓
|
jakob |
* phpillow CouchDB backend |
|
| 4 |
↓
|
jakob |
* |
|
| 5 |
↓
|
jakob |
* This file is part of phpillow. |
|
| 6 |
↓
|
jakob |
* |
|
| 7 |
↓
|
jakob |
* phpillow is free software; you can redistribute it and/or modify it under |
|
| 8 |
↓
|
jakob |
* the terms of the GNU Lesser General Public License as published by the Free |
|
| 9 |
↓
|
jakob |
* Software Foundation; version 3 of the License. |
|
| 10 |
↓
|
jakob |
* |
|
| 11 |
↓
|
jakob |
* phpillow is distributed in the hope that it will be useful, but WITHOUT ANY |
|
| 12 |
↓
|
jakob |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
| 13 |
↓
|
jakob |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
|
| 14 |
↓
|
jakob |
* more details. |
|
| 15 |
↓
|
jakob |
* |
|
| 16 |
↓
|
jakob |
* You should have received a copy of the GNU Lesser General Public License |
|
| 17 |
↓
|
jakob |
* along with phpillow; if not, write to the Free Software Foundation, Inc., 51 |
|
| 18 |
↓
|
jakob |
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 19 |
↓
|
jakob |
* |
|
| 20 |
↓
|
jakob |
* @package Core |
|
| 21 |
↓
|
jakob |
* @version $Revision: 182 $ |
|
| 22 |
↓
|
jakob |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 23 |
↓
|
jakob |
*/ |
|
| 24 |
↓
|
jakob |
|
|
| 25 |
↓
|
jakob |
/** |
|
| 26 |
↓
|
jakob |
* Class representing a file attached to a couchdb document. |
|
| 27 |
↓
|
jakob |
* |
|
| 28 |
↓
|
jakob |
* This class implements a lazy loading mechanism, which does only fetch the |
|
| 29 |
↓
|
jakob |
* binary data of an attachment if it is really accessed. |
|
| 30 |
↓
|
jakob |
* |
|
| 31 |
↓
|
jakob |
* Metadata like content-type and size will accessible, without fetching the |
|
| 32 |
↓
|
jakob |
* data. |
|
| 33 |
↓
|
jakob |
* |
|
| 34 |
↓
|
jakob |
* Once fetched the data will be cached inside the object to provide fast |
|
| 35 |
↓
|
jakob |
* access. |
|
| 36 |
↓
|
jakob |
* |
|
| 37 |
↓
|
jakob |
* @package Core |
|
| 38 |
↓
|
jakob |
* @version $Revision: 182 $ |
|
| 39 |
↓
|
jakob |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 40 |
↓
|
jakob |
* |
|
| 41 |
↓
|
jakob |
* @property-read $data Binary data of the stored document |
|
| 42 |
↓
|
jakob |
* @property-read $contentType The mimetype of the stored attachment |
|
| 43 |
↓
|
jakob |
* @property-read $size Size in bytes of the stored attachment |
|
| 44 |
↓
|
jakob |
*/ |
|
| 45 |
182
|
kore |
class phpillowLazyFile extends phpillowDataResponse |
|
| 46 |
178
|
jakob |
{ |
|
| 47 |
↓
|
jakob |
/** |
|
| 48 |
↓
|
jakob |
* Storage for read-only properties |
|
| 49 |
↓
|
jakob |
* |
|
| 50 |
↓
|
jakob |
* @var array |
|
| 51 |
↓
|
jakob |
*/ |
|
| 52 |
↓
|
jakob |
protected $properties = array( |
|
| 53 |
↓
|
jakob |
'data' => null, |
|
| 54 |
↓
|
jakob |
'contentType' => null, |
|
| 55 |
↓
|
jakob |
'length' => null, |
|
| 56 |
↓
|
jakob |
); |
|
| 57 |
↓
|
jakob |
|
|
| 58 |
↓
|
jakob |
/** |
|
| 59 |
↓
|
jakob |
* Url which provides access to the couchdb attachment associated with this |
|
| 60 |
↓
|
jakob |
* lazy file loader. |
|
| 61 |
↓
|
jakob |
* |
|
| 62 |
↓
|
jakob |
* @var string |
|
| 63 |
↓
|
jakob |
*/ |
|
| 64 |
↓
|
jakob |
protected $url; |
|
| 65 |
↓
|
jakob |
|
|
| 66 |
↓
|
jakob |
/** |
|
| 67 |
↓
|
jakob |
* phpillowConnection to be used for retrieving the file data if requested. |
|
| 68 |
↓
|
jakob |
* |
|
| 69 |
↓
|
jakob |
* @var phpillowConnection |
|
| 70 |
↓
|
jakob |
*/ |
|
| 71 |
↓
|
jakob |
protected $connection; |
|
| 72 |
↓
|
jakob |
|
|
| 73 |
↓
|
jakob |
|
|
| 74 |
↓
|
jakob |
/** |
|
| 75 |
↓
|
jakob |
* Construct a new lazy File loader object using a given access url, |
|
| 76 |
↓
|
jakob |
* contentType and length in bytes. |
|
| 77 |
↓
|
jakob |
* |
|
| 78 |
↓
|
jakob |
* @param phpillowConnection $connection |
|
| 79 |
↓
|
jakob |
* @param mixed $url |
|
| 80 |
↓
|
jakob |
* @param mixed $contentType |
|
| 81 |
↓
|
jakob |
* @param mixed $length |
|
| 82 |
↓
|
jakob |
*/ |
|
| 83 |
↓
|
jakob |
public function __construct( phpillowConnection $connection, $url, $contentType, $length ) |
|
| 84 |
↓
|
jakob |
{ |
|
| 85 |
↓
|
jakob |
$this->connection = $connection; |
|
| 86 |
↓
|
jakob |
$this->url = $url; |
|
| 87 |
↓
|
jakob |
|
|
| 88 |
↓
|
jakob |
$this->properties['contentType'] = $contentType; |
|
| 89 |
↓
|
jakob |
$this->properties['length'] = $length; |
|
| 90 |
↓
|
jakob |
} |
|
| 91 |
↓
|
jakob |
|
|
| 92 |
↓
|
jakob |
/** |
|
| 93 |
↓
|
jakob |
* Automagic getter for read-only properties |
|
| 94 |
↓
|
jakob |
* |
|
| 95 |
↓
|
jakob |
* @param mixed $key |
|
| 96 |
↓
|
jakob |
* @return mixed |
|
| 97 |
↓
|
jakob |
*/ |
|
| 98 |
↓
|
jakob |
public function __get( $key ) |
|
| 99 |
↓
|
jakob |
{ |
|
| 100 |
↓
|
jakob |
// Check if such an property exists at all |
|
| 101 |
179
|
hco |
if ( !array_key_exists( $key, $this->properties ) ) |
|
| 102 |
178
|
jakob |
{ |
|
| 103 |
↓
|
jakob |
throw new phpillowNoSuchPropertyException( $key ); |
|
| 104 |
↓
|
jakob |
} |
|
| 105 |
↓
|
jakob |
|
|
| 106 |
↓
|
jakob |
switch( $key ) |
|
| 107 |
↓
|
jakob |
{ |
|
| 108 |
↓
|
jakob |
case 'data': |
|
| 109 |
↓
|
jakob |
if ( $this->properties['data'] === null ) |
|
| 110 |
↓
|
jakob |
{ |
|
| 111 |
↓
|
jakob |
$this->properties['data'] = $this->fetchData(); |
|
| 112 |
↓
|
jakob |
} |
|
| 113 |
↓
|
jakob |
default: |
|
| 114 |
↓
|
jakob |
return $this->properties[$key]; |
|
| 115 |
↓
|
jakob |
} |
|
| 116 |
↓
|
jakob |
} |
|
| 117 |
↓
|
jakob |
|
|
| 118 |
↓
|
jakob |
/** |
|
| 119 |
↓
|
jakob |
* Fetch the attachment data from the couchdb and return it |
|
| 120 |
↓
|
jakob |
* |
|
| 121 |
↓
|
jakob |
* @return string |
|
| 122 |
↓
|
jakob |
*/ |
|
| 123 |
↓
|
jakob |
protected function fetchData() |
|
| 124 |
↓
|
jakob |
{ |
|
| 125 |
↓
|
jakob |
return $this->connection->get( |
|
| 126 |
↓
|
jakob |
$this->url, |
|
| 127 |
↓
|
jakob |
null, |
|
| 128 |
↓
|
jakob |
true |
|
| 129 |
↓
|
jakob |
)->data; |
|
| 130 |
↓
|
jakob |
} |
|
| 131 |
↓
|
jakob |
} |
|