| 1 |
1
|
kore |
<?php |
|
| 2 |
↓
|
kore |
/** |
|
| 3 |
2
|
kore |
* phpillow CouchDB backend |
|
| 4 |
1
|
kore |
* |
|
| 5 |
2
|
kore |
* This file is part of phpillow. |
|
| 6 |
1
|
kore |
* |
|
| 7 |
3
|
kore |
* phpillow is free software; you can redistribute it and/or modify it under |
|
| 8 |
↓
|
kore |
* the terms of the GNU Lesser General Public License as published by the Free |
|
| 9 |
↓
|
kore |
* Software Foundation; version 3 of the License. |
|
| 10 |
1
|
kore |
* |
|
| 11 |
3
|
kore |
* phpillow is distributed in the hope that it will be useful, but WITHOUT ANY |
|
| 12 |
↓
|
kore |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
| 13 |
↓
|
kore |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
|
| 14 |
↓
|
kore |
* more details. |
|
| 15 |
1
|
kore |
* |
|
| 16 |
3
|
kore |
* You should have received a copy of the GNU Lesser General Public License |
|
| 17 |
↓
|
kore |
* along with phpillow; if not, write to the Free Software Foundation, Inc., 51 |
|
| 18 |
↓
|
kore |
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 19 |
1
|
kore |
* |
|
| 20 |
↓
|
kore |
* @package Core |
|
| 21 |
4
|
kore |
* @version $Revision: 94 $ |
|
| 22 |
3
|
kore |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 23 |
1
|
kore |
*/ |
|
| 24 |
↓
|
kore |
|
|
| 25 |
↓
|
kore |
/** |
|
| 26 |
↓
|
kore |
* Basic couch DB view and document manager / registry. |
|
| 27 |
↓
|
kore |
* |
|
| 28 |
↓
|
kore |
* @package Core |
|
| 29 |
4
|
kore |
* @version $Revision: 94 $ |
|
| 30 |
3
|
kore |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 31 |
1
|
kore |
*/ |
|
| 32 |
3
|
kore |
final class phpillowManager |
|
| 33 |
1
|
kore |
{ |
|
| 34 |
↓
|
kore |
/** |
|
| 35 |
↓
|
kore |
* Initial mapping of view types to view classes. |
|
| 36 |
↓
|
kore |
* |
|
| 37 |
16
|
kore |
* Mapping values may be added by the method setViewClass(). The array to |
|
| 38 |
↓
|
kore |
* store the values looks like: |
|
| 39 |
↓
|
kore |
* <code> |
|
| 40 |
↓
|
kore |
* array( |
|
| 41 |
↓
|
kore |
* 'name' => 'class', |
|
| 42 |
↓
|
kore |
* ... |
|
| 43 |
↓
|
kore |
* ) |
|
| 44 |
↓
|
kore |
* </code> |
|
| 45 |
↓
|
kore |
* |
|
| 46 |
1
|
kore |
* @var array |
|
| 47 |
↓
|
kore |
*/ |
|
| 48 |
↓
|
kore |
protected static $views = array( |
|
| 49 |
↓
|
kore |
); |
|
| 50 |
↓
|
kore |
|
|
| 51 |
↓
|
kore |
/** |
|
| 52 |
↓
|
kore |
* Initial mapping of document types to document classes. |
|
| 53 |
↓
|
kore |
* |
|
| 54 |
16
|
kore |
* Mapping values may be added by the method setDocumentClass(). The array |
|
| 55 |
↓
|
kore |
* to store the values looks like: |
|
| 56 |
↓
|
kore |
* <code> |
|
| 57 |
↓
|
kore |
* array( |
|
| 58 |
↓
|
kore |
* 'name' => 'class', |
|
| 59 |
↓
|
kore |
* ... |
|
| 60 |
↓
|
kore |
* ) |
|
| 61 |
↓
|
kore |
* </code> |
|
| 62 |
↓
|
kore |
* |
|
| 63 |
1
|
kore |
* @var array |
|
| 64 |
↓
|
kore |
*/ |
|
| 65 |
↓
|
kore |
protected static $documents = array( |
|
| 66 |
↓
|
kore |
); |
|
| 67 |
↓
|
kore |
|
|
| 68 |
↓
|
kore |
/** |
|
| 69 |
↓
|
kore |
* Empty protected constructor |
|
| 70 |
↓
|
kore |
* |
|
| 71 |
↓
|
kore |
* We do not want this registry to be instanciated. |
|
| 72 |
↓
|
kore |
* |
|
| 73 |
↓
|
kore |
* @ignore |
|
| 74 |
↓
|
kore |
* @return void |
|
| 75 |
↓
|
kore |
*/ |
|
| 76 |
↓
|
kore |
protected function __construct() |
|
| 77 |
↓
|
kore |
{ |
|
| 78 |
↓
|
kore |
} |
|
| 79 |
↓
|
kore |
|
|
| 80 |
↓
|
kore |
/** |
|
| 81 |
↓
|
kore |
* Set view class |
|
| 82 |
↓
|
kore |
* |
|
| 83 |
↓
|
kore |
* Set a view class for a view type. |
|
| 84 |
↓
|
kore |
* |
|
| 85 |
↓
|
kore |
* @param string $name |
|
| 86 |
↓
|
kore |
* @param string $class |
|
| 87 |
↓
|
kore |
* @return void |
|
| 88 |
↓
|
kore |
*/ |
|
| 89 |
↓
|
kore |
public static function setViewClass( $name, $class ) |
|
| 90 |
↓
|
kore |
{ |
|
| 91 |
↓
|
kore |
self::$views[$name] = $class; |
|
| 92 |
↓
|
kore |
} |
|
| 93 |
↓
|
kore |
|
|
| 94 |
↓
|
kore |
/** |
|
| 95 |
↓
|
kore |
* Return view |
|
| 96 |
↓
|
kore |
* |
|
| 97 |
↓
|
kore |
* Get a view object for the given view type. Throws a |
|
| 98 |
3
|
kore |
* phpillowNoSuchPropertyException if the view does not exist. |
|
| 99 |
1
|
kore |
* |
|
| 100 |
↓
|
kore |
* @param string $name |
|
| 101 |
3
|
kore |
* @return phpillowView |
|
| 102 |
1
|
kore |
*/ |
|
| 103 |
↓
|
kore |
public static function getView( $name ) |
|
| 104 |
↓
|
kore |
{ |
|
| 105 |
↓
|
kore |
// Check if a view with the given name exists. |
|
| 106 |
↓
|
kore |
if ( !isset( self::$views[$name] ) ) |
|
| 107 |
↓
|
kore |
{ |
|
| 108 |
3
|
kore |
throw new phpillowNoSuchPropertyException( $name ); |
|
| 109 |
1
|
kore |
} |
|
| 110 |
↓
|
kore |
|
|
| 111 |
↓
|
kore |
// Instantiate and return view. |
|
| 112 |
↓
|
kore |
$className = self::$views[$name]; |
|
| 113 |
↓
|
kore |
return new $className; |
|
| 114 |
↓
|
kore |
} |
|
| 115 |
↓
|
kore |
|
|
| 116 |
↓
|
kore |
/** |
|
| 117 |
↓
|
kore |
* Set document class |
|
| 118 |
↓
|
kore |
* |
|
| 119 |
↓
|
kore |
* Set a document class for a document type. |
|
| 120 |
↓
|
kore |
* |
|
| 121 |
↓
|
kore |
* @param string $name |
|
| 122 |
↓
|
kore |
* @param string $class |
|
| 123 |
↓
|
kore |
* @return void |
|
| 124 |
↓
|
kore |
*/ |
|
| 125 |
↓
|
kore |
public static function setDocumentClass( $name, $class ) |
|
| 126 |
↓
|
kore |
{ |
|
| 127 |
↓
|
kore |
self::$documents[$name] = $class; |
|
| 128 |
↓
|
kore |
} |
|
| 129 |
↓
|
kore |
|
|
| 130 |
↓
|
kore |
/** |
|
| 131 |
↓
|
kore |
* Create new document |
|
| 132 |
↓
|
kore |
* |
|
| 133 |
↓
|
kore |
* Create a new document of the given type and return it. Throws a |
|
| 134 |
3
|
kore |
* phpillowNoSuchPropertyException if the document does not exist. |
|
| 135 |
1
|
kore |
* |
|
| 136 |
↓
|
kore |
* @param string $name |
|
| 137 |
3
|
kore |
* @return phpillowDocument |
|
| 138 |
1
|
kore |
*/ |
|
| 139 |
↓
|
kore |
public static function createDocument( $name ) |
|
| 140 |
↓
|
kore |
{ |
|
| 141 |
↓
|
kore |
// Check if a document with the given name exists. |
|
| 142 |
↓
|
kore |
if ( !isset( self::$documents[$name] ) ) |
|
| 143 |
↓
|
kore |
{ |
|
| 144 |
3
|
kore |
throw new phpillowNoSuchPropertyException( $name ); |
|
| 145 |
1
|
kore |
} |
|
| 146 |
↓
|
kore |
|
|
| 147 |
↓
|
kore |
// Instantiate and return document. |
|
| 148 |
↓
|
kore |
$className = self::$documents[$name]; |
|
| 149 |
94
|
kore |
return call_user_func( array( $className, 'createNew' ) ); |
|
| 150 |
1
|
kore |
} |
|
| 151 |
↓
|
kore |
|
|
| 152 |
↓
|
kore |
/** |
|
| 153 |
↓
|
kore |
* Fetch document by ID |
|
| 154 |
↓
|
kore |
* |
|
| 155 |
↓
|
kore |
* Fetch the document of the given type with the given ID. Throws a |
|
| 156 |
3
|
kore |
* phpillowNoSuchPropertyException if the document does not exist. |
|
| 157 |
1
|
kore |
* |
|
| 158 |
↓
|
kore |
* @param string $name |
|
| 159 |
↓
|
kore |
* @param string $id |
|
| 160 |
3
|
kore |
* @return phpillowDocument |
|
| 161 |
1
|
kore |
*/ |
|
| 162 |
↓
|
kore |
public static function fetchDocument( $name, $id ) |
|
| 163 |
↓
|
kore |
{ |
|
| 164 |
↓
|
kore |
// Check if a document with the given name exists. |
|
| 165 |
↓
|
kore |
if ( !isset( self::$documents[$name] ) ) |
|
| 166 |
↓
|
kore |
{ |
|
| 167 |
3
|
kore |
throw new phpillowNoSuchPropertyException( $name ); |
|
| 168 |
1
|
kore |
} |
|
| 169 |
↓
|
kore |
|
|
| 170 |
↓
|
kore |
// Instantiate and return document. |
|
| 171 |
↓
|
kore |
$className = self::$documents[$name]; |
|
| 172 |
94
|
kore |
$document = new $className(); |
|
| 173 |
↓
|
kore |
return $document->fetchById( $id ); |
|
| 174 |
1
|
kore |
} |
|
| 175 |
↓
|
kore |
|
|
| 176 |
↓
|
kore |
/** |
|
| 177 |
↓
|
kore |
* Delete document by ID |
|
| 178 |
↓
|
kore |
* |
|
| 179 |
↓
|
kore |
* Delete the document of the given type with the given ID. Throws a |
|
| 180 |
3
|
kore |
* phpillowNoSuchPropertyException if the document does not exist. |
|
| 181 |
1
|
kore |
* |
|
| 182 |
↓
|
kore |
* Deletion means, that all revisions, including the current one, are |
|
| 183 |
↓
|
kore |
* removed. |
|
| 184 |
↓
|
kore |
* |
|
| 185 |
↓
|
kore |
* @param string $name |
|
| 186 |
↓
|
kore |
* @param string $id |
|
| 187 |
↓
|
kore |
* @return void |
|
| 188 |
↓
|
kore |
*/ |
|
| 189 |
↓
|
kore |
public static function deleteDocument( $name, $id ) |
|
| 190 |
↓
|
kore |
{ |
|
| 191 |
↓
|
kore |
// Check if a document with the given name exists. |
|
| 192 |
↓
|
kore |
if ( !isset( self::$documents[$name] ) ) |
|
| 193 |
↓
|
kore |
{ |
|
| 194 |
3
|
kore |
throw new phpillowNoSuchPropertyException( $name ); |
|
| 195 |
1
|
kore |
} |
|
| 196 |
↓
|
kore |
|
|
| 197 |
3
|
kore |
$db = phpillowConnection::getInstance(); |
|
| 198 |
17
|
kore |
$revision = $db->get( $db->getDatabase() . $id ); |
|
| 199 |
↓
|
kore |
|
|
| 200 |
↓
|
kore |
// Only delete the current revision. This should delete all revisions |
|
| 201 |
↓
|
kore |
// in the database except somebody updates the document between the get |
|
| 202 |
↓
|
kore |
// and the delete request |
|
| 203 |
↓
|
kore |
$db->delete( $db->getDatabase() . $id . '?rev=' . $revision->_rev ); |
|
| 204 |
1
|
kore |
} |
|
| 205 |
↓
|
kore |
} |
|
| 206 |
↓
|
kore |
|
|