Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

File: / src/ classes/ file_view.php

Type
text/plain text/plain
Last Author
kore
Version
159
Line Rev. Author Source
1 111 kore <?php
2 kore /**
3 kore * phpillow CouchDB backend
4 kore *
5 kore * This file is part of phpillow.
6 kore *
7 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 kore *
11 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 kore *
16 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 kore *
20 kore * @package Core
21 113 kore * @version $Revision: 159 $
22 111 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 kore */
24 kore
25 kore /**
26 kore * Wrapper base for views in the database, where view functions are locally
27 kore * stored as files.
28 kore *
29 kore * @package Core
30 113 kore * @version $Revision: 159 $
31 111 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
32 kore */
33 kore abstract class phpillowFileView extends phpillowView
34 kore {
35 kore /**
36 kore * View functions to be registered on the server.
37 kore *
38 kore * Contains file names for map and reduce functions, indexed by their name.
39 kore * The view files are only read when the view is updated on the server. The
40 kore * format of the array look like:
41 kore *
42 kore * <code>
43 kore * array(
44 kore * 'name' => array(
45 kore * 'map' => __DIR__ . '/views/map/all.js',
46 kore * 'reduce' => __DIR__ . '/views/reduce/all.js',
47 kore * ),
48 kore * ...
49 kore * )
50 kore * </code>
51 kore *
52 kore * If you do not want to define a reduce function omit the reduce index or
53 kore * use `null` as a value.
54 kore *
55 kore * @var array
56 kore */
57 kore protected $viewFunctions = array();
58 kore
59 kore /**
60 kore * Construct new document
61 159 kore *
62 111 kore * Construct new document
63 159 kore *
64 111 kore * @return void
65 kore */
66 kore public function __construct()
67 kore {
68 kore parent::__construct();
69 kore
70 kore $this->views = $this->viewFunctions;
71 kore }
72 kore
73 kore /**
74 kore * Verify stored views
75 kore *
76 kore * Check if the views stored in the database equal the view definitions
77 159 kore * specified by the vew classes. If the implementation differs update to the
78 111 kore * view specifications in the class.
79 159 kore *
80 111 kore * @return void
81 kore */
82 kore public function verifyView()
83 kore {
84 kore // Fetch view definition from database
85 kore try
86 kore {
87 kore $view = self::fetchById( '_design/' . $this->getViewName() );
88 kore }
89 kore catch ( phpillowResponseNotFoundErrorException $e )
90 kore {
91 kore // If the view does not exist yet, recreate it from current view
92 kore $view = $this;
93 kore }
94 159 kore
95 111 kore // Force setting of view definitions
96 kore $views = array();
97 kore foreach ( $this->viewFunctions as $name => $functions )
98 kore {
99 kore $views[$name]['map'] = file_get_contents( $functions['map'] );
100 kore
101 kore // Check if there is also a reduce function for the given view
102 kore // function.
103 kore if ( isset( $functions['reduce'] ) )
104 kore {
105 kore $views[$name]['reduce'] = file_get_contents( $functions['reduce'] );
106 kore }
107 kore }
108 kore
109 kore $view->views = $views;
110 kore $view->save();
111 kore }
112 kore }
113 kore