1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @file DesignDoc.php |
||
5 | * @brief This file contains the DesignDoc class. |
||
6 | * @details |
||
7 | * @author Filippo F. Fadda |
||
8 | */ |
||
9 | |||
10 | |||
11 | namespace EoC\Doc; |
||
12 | |||
13 | |||
14 | use EoC\Handler; |
||
15 | |||
16 | |||
17 | /** |
||
18 | * @brief A design document is a special CouchDB document where views, updates, rewrites and many others handlers are |
||
19 | * stored. |
||
20 | * @see http://guide.couchdb.org/editions/1/en/design.html |
||
21 | * @nosubgrouping |
||
22 | */ |
||
23 | final class DesignDoc extends Doc { |
||
24 | |||
25 | // Stores the names of the sections that belong to all the available handlers. |
||
26 | private $sections = []; |
||
27 | |||
28 | |||
29 | public function __construct() { |
||
30 | $this->initSections(); |
||
31 | } |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @brief Creates an instance of DesignDoc class. |
||
36 | * @param string $name The design document name. |
||
37 | * @param string $language The programming language used by the design document for his handlers. |
||
38 | * @return DesignDoc An instance of the class. |
||
39 | */ |
||
40 | public static function create($name, $language = "php") { |
||
41 | $instance = new self(); |
||
42 | |||
43 | $instance->setId($name); |
||
44 | $instance->setLanguage($language); |
||
45 | |||
46 | return $instance; |
||
47 | } |
||
48 | |||
49 | |||
50 | // Initializes the handlers' sections. |
||
51 | private function initSections() { |
||
52 | $this->sections[Handler\ViewHandler::getSection()] = NULL; |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * @brief Removes `_design/` from he document identifier. |
||
58 | */ |
||
59 | protected function fixDocId() { |
||
60 | if (isset($this->meta['_id'])) |
||
61 | $this->meta['_id'] = preg_replace('%\A_design/%m', "", $this->meta['_id']); |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @brief Design documents don't have a class, so we don't provide an implementation. |
||
67 | */ |
||
68 | public function setClass($value) {} |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @brief Design documents don't have a type, so we don't provide an implementation. |
||
73 | */ |
||
74 | public function setType($value) {} |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @brief Gets the document path: `_design/`. |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getPath() { |
||
82 | return "_design/"; |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * @brief Reset the list of handlers. |
||
88 | */ |
||
89 | public function resetHandlers() { |
||
90 | foreach ($this->sections as $name => $value) { |
||
91 | if (array_key_exists($name, $this->meta)) |
||
92 | unset($this->meta[$name]); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * @brief Given a design document section (ex. views, updates, filters, etc.) and an optional handler's name (because |
||
99 | * the handler couldn't have a name), returns the |
||
100 | * @param string $section The section name. |
||
101 | * @param string $name (optional) The handler name. |
||
102 | */ |
||
103 | public function getHandlerAttributes($section, $name = "") { |
||
104 | if (empty($name)) { // The handler doesn't have a name. |
||
105 | if (array_key_exists($section, $this->meta)) |
||
106 | return $this->meta[$section]; |
||
107 | else |
||
108 | throw new \Exception(sprintf("Can't find '%s' handler in the design document.", $section)); |
||
109 | } |
||
110 | else { // The handler has a name. |
||
111 | if (@array_key_exists($name, $this->meta[$section])) |
||
112 | return $this->meta[$section][$name]; |
||
113 | else |
||
114 | throw new \Exception(sprintf("Can't find '%s' handler in the design document '%s' section.", $name, $section)); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * @brief Adds a special handler to the design document. |
||
121 | * @details This method checks the existence of the property `$name`, in fact a design document can have sections |
||
122 | * with multiple handlers, but in some cases there is one and only one handler per section, so that handler doesn't |
||
123 | * have a name. |
||
124 | * @param Handler\IHandler $handler An instance of a subclass of the abstract class DesignIHandler. |
||
125 | */ |
||
126 | public function addHandler(Handler\IHandler $handler) { |
||
127 | $section = $handler->getSection(); |
||
128 | |||
129 | if (property_exists($handler, "name")) { |
||
130 | if (!$handler->isConsistent()) |
||
131 | throw new \Exception(sprintf("The '%s' handler '%s' is not consistent.", $section, $handler->name)); |
||
132 | |||
133 | if (@array_key_exists($handler->name, $this->meta[$section])) |
||
134 | throw new \Exception(sprintf("The '%s' handler '%s' already exists.", $section, $handler->name)); |
||
135 | else |
||
136 | $this->meta[$section][$handler->name] = $handler->asArray(); |
||
137 | } |
||
138 | else { |
||
139 | if (!$handler->isConsistent()) |
||
140 | throw new \Exception(sprintf("The '%s' handler is not consistent.", $section)); |
||
141 | |||
142 | if (array_key_exists($section, $this->meta)) |
||
143 | throw new \Exception(sprintf("The '%s' handler already exists.", $section)); |
||
144 | else |
||
145 | $this->meta[$section] = $handler; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @brief Removes the handler. |
||
152 | * @details Some handlers belong to a section. For example ViewHandler belongs to the 'views' section. To specify |
||
153 | * the appropriate section name, you should use the static method `getSection` available for every handler |
||
154 | * implementation. |
||
155 | * @param string $section The section's name (views, updates, shows, filters, etc). |
||
156 | * @param string $name (optional) The handler's name. |
||
157 | */ |
||
158 | public function removeHandler($section, $name = "") { |
||
159 | if (empty($name)) { // The handler doesn't have a name. |
||
160 | if (array_key_exists($section, $this->meta)) |
||
161 | unset($this->meta[$section]); |
||
162 | else |
||
163 | throw new \Exception(sprintf("Can't find the '%s' handler.", $section)); |
||
164 | } |
||
165 | else { // The handler has a name. |
||
166 | if (@array_key_exists($name, $this->meta[$section])) |
||
167 | unset($this->meta[$section][$name]); |
||
168 | else |
||
169 | throw new \Exception(sprintf("Can't find the '%s' handler '%s'", $section, $name)); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | |||
174 | //! @cond HIDDEN_SYMBOLS |
||
175 | |||
176 | public function getLanguage() { |
||
177 | return $this->meta['language']; |
||
178 | } |
||
179 | |||
180 | |||
181 | public function issetLanguage() { |
||
182 | return isset($this->meta['language']); |
||
183 | } |
||
184 | |||
185 | |||
186 | public function setLanguage($value) { |
||
187 | if (!empty($value)) |
||
188 | $this->meta['language'] = strtolower((string)$value); |
||
189 | else |
||
190 | throw new \InvalidArgumentException("\$language must be a non-empty string."); |
||
191 | } |
||
192 | |||
193 | |||
194 | public function unsetLanguage() { |
||
195 | if ($this->isMetadataPresent('language')) |
||
196 | unset($this->meta['language']); |
||
197 | } |
||
198 | |||
199 | //! @endcond |
||
0 ignored issues
–
show
|
|||
200 | |||
201 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.