@@ -15,10 +15,10 @@ |
||
15 | 15 | */ |
16 | 16 | class TsvFormat extends CsvFormat |
17 | 17 | { |
18 | - const MIME = 'text/csv'; |
|
19 | - const EXTENSION = 'csv'; |
|
20 | - public static $delimiter = "\t"; |
|
21 | - public static $enclosure = '"'; |
|
22 | - public static $escape = '\\'; |
|
23 | - public static $haveHeaders = null; |
|
18 | + const MIME = 'text/csv'; |
|
19 | + const EXTENSION = 'csv'; |
|
20 | + public static $delimiter = "\t"; |
|
21 | + public static $enclosure = '"'; |
|
22 | + public static $escape = '\\'; |
|
23 | + public static $haveHeaders = null; |
|
24 | 24 | } |
25 | 25 | \ No newline at end of file |
@@ -20,334 +20,334 @@ |
||
20 | 20 | */ |
21 | 21 | class XmlFormat extends Format |
22 | 22 | { |
23 | - const MIME = 'application/xml'; |
|
24 | - const EXTENSION = 'xml'; |
|
23 | + const MIME = 'application/xml'; |
|
24 | + const EXTENSION = 'xml'; |
|
25 | 25 | |
26 | - // ================================================================== |
|
27 | - // |
|
28 | - // Properties related to reading/parsing/decoding xml |
|
29 | - // |
|
30 | - // ------------------------------------------------------------------ |
|
31 | - public static $importSettingsFromXml = false; |
|
32 | - public static $parseAttributes = true; |
|
33 | - public static $parseNamespaces = true; |
|
34 | - public static $parseTextNodeAsProperty = true; |
|
26 | + // ================================================================== |
|
27 | + // |
|
28 | + // Properties related to reading/parsing/decoding xml |
|
29 | + // |
|
30 | + // ------------------------------------------------------------------ |
|
31 | + public static $importSettingsFromXml = false; |
|
32 | + public static $parseAttributes = true; |
|
33 | + public static $parseNamespaces = true; |
|
34 | + public static $parseTextNodeAsProperty = true; |
|
35 | 35 | |
36 | - // ================================================================== |
|
37 | - // |
|
38 | - // Properties related to writing/encoding xml |
|
39 | - // |
|
40 | - // ------------------------------------------------------------------ |
|
41 | - public static $useTextNodeProperty = true; |
|
42 | - public static $useNamespaces = true; |
|
43 | - public static $cdataNames = array(); |
|
36 | + // ================================================================== |
|
37 | + // |
|
38 | + // Properties related to writing/encoding xml |
|
39 | + // |
|
40 | + // ------------------------------------------------------------------ |
|
41 | + public static $useTextNodeProperty = true; |
|
42 | + public static $useNamespaces = true; |
|
43 | + public static $cdataNames = array(); |
|
44 | 44 | |
45 | - // ================================================================== |
|
46 | - // |
|
47 | - // Common Properties |
|
48 | - // |
|
49 | - // ------------------------------------------------------------------ |
|
50 | - public static $attributeNames = array(); |
|
51 | - public static $textNodeName = 'text'; |
|
52 | - public static $namespaces = array(); |
|
53 | - public static $namespacedProperties = array(); |
|
54 | - /** |
|
55 | - * Default name for the root node. |
|
56 | - * |
|
57 | - * @var string $rootNodeName |
|
58 | - */ |
|
59 | - public static $rootName = 'response'; |
|
60 | - public static $defaultTagName = 'item'; |
|
45 | + // ================================================================== |
|
46 | + // |
|
47 | + // Common Properties |
|
48 | + // |
|
49 | + // ------------------------------------------------------------------ |
|
50 | + public static $attributeNames = array(); |
|
51 | + public static $textNodeName = 'text'; |
|
52 | + public static $namespaces = array(); |
|
53 | + public static $namespacedProperties = array(); |
|
54 | + /** |
|
55 | + * Default name for the root node. |
|
56 | + * |
|
57 | + * @var string $rootNodeName |
|
58 | + */ |
|
59 | + public static $rootName = 'response'; |
|
60 | + public static $defaultTagName = 'item'; |
|
61 | 61 | |
62 | - /** |
|
63 | - * When you decode an XML its structure is copied to the static vars |
|
64 | - * we can use this function to echo them out and then copy paste inside |
|
65 | - * our service methods |
|
66 | - * |
|
67 | - * @return string PHP source code to reproduce the configuration |
|
68 | - */ |
|
69 | - public static function exportCurrentSettings() |
|
70 | - { |
|
71 | - $s = 'XmlFormat::$rootName = "' . (self::$rootName) . "\";\n"; |
|
72 | - $s .= 'XmlFormat::$attributeNames = ' . |
|
73 | - (var_export(self::$attributeNames, true)) . ";\n"; |
|
74 | - $s .= 'XmlFormat::$defaultTagName = "' . |
|
75 | - self::$defaultTagName . "\";\n"; |
|
76 | - $s .= 'XmlFormat::$parseAttributes = ' . |
|
77 | - (self::$parseAttributes ? 'true' : 'false') . ";\n"; |
|
78 | - $s .= 'XmlFormat::$parseNamespaces = ' . |
|
79 | - (self::$parseNamespaces ? 'true' : 'false') . ";\n"; |
|
80 | - if (self::$parseNamespaces) { |
|
81 | - $s .= 'XmlFormat::$namespaces = ' . |
|
82 | - (var_export(self::$namespaces, true)) . ";\n"; |
|
83 | - $s .= 'XmlFormat::$namespacedProperties = ' . |
|
84 | - (var_export(self::$namespacedProperties, true)) . ";\n"; |
|
85 | - } |
|
62 | + /** |
|
63 | + * When you decode an XML its structure is copied to the static vars |
|
64 | + * we can use this function to echo them out and then copy paste inside |
|
65 | + * our service methods |
|
66 | + * |
|
67 | + * @return string PHP source code to reproduce the configuration |
|
68 | + */ |
|
69 | + public static function exportCurrentSettings() |
|
70 | + { |
|
71 | + $s = 'XmlFormat::$rootName = "' . (self::$rootName) . "\";\n"; |
|
72 | + $s .= 'XmlFormat::$attributeNames = ' . |
|
73 | + (var_export(self::$attributeNames, true)) . ";\n"; |
|
74 | + $s .= 'XmlFormat::$defaultTagName = "' . |
|
75 | + self::$defaultTagName . "\";\n"; |
|
76 | + $s .= 'XmlFormat::$parseAttributes = ' . |
|
77 | + (self::$parseAttributes ? 'true' : 'false') . ";\n"; |
|
78 | + $s .= 'XmlFormat::$parseNamespaces = ' . |
|
79 | + (self::$parseNamespaces ? 'true' : 'false') . ";\n"; |
|
80 | + if (self::$parseNamespaces) { |
|
81 | + $s .= 'XmlFormat::$namespaces = ' . |
|
82 | + (var_export(self::$namespaces, true)) . ";\n"; |
|
83 | + $s .= 'XmlFormat::$namespacedProperties = ' . |
|
84 | + (var_export(self::$namespacedProperties, true)) . ";\n"; |
|
85 | + } |
|
86 | 86 | |
87 | - return $s; |
|
88 | - } |
|
87 | + return $s; |
|
88 | + } |
|
89 | 89 | |
90 | - public function encode($data, $humanReadable = false) |
|
91 | - { |
|
92 | - $data = Obj::toArray($data); |
|
93 | - $xml = new XMLWriter(); |
|
94 | - $xml->openMemory(); |
|
95 | - $xml->startDocument('1.0', $this->charset); |
|
96 | - if ($humanReadable) { |
|
97 | - $xml->setIndent(true); |
|
98 | - $xml->setIndentString(' '); |
|
99 | - } |
|
100 | - static::$useNamespaces && isset(static::$namespacedProperties[static::$rootName]) |
|
101 | - ? |
|
102 | - $xml->startElementNs( |
|
103 | - static::$namespacedProperties[static::$rootName], |
|
104 | - static::$rootName, |
|
105 | - static::$namespaces[static::$namespacedProperties[static::$rootName]] |
|
106 | - ) |
|
107 | - : |
|
108 | - $xml->startElement(static::$rootName); |
|
109 | - if (static::$useNamespaces) { |
|
110 | - foreach (static::$namespaces as $prefix => $ns) { |
|
111 | - if (isset(static::$namespacedProperties[static::$rootName]) |
|
112 | - && static::$namespacedProperties[static::$rootName] == $prefix |
|
113 | - ) { |
|
114 | - continue; |
|
115 | - } |
|
116 | - $prefix = 'xmlns' . (empty($prefix) ? '' : ':' . $prefix); |
|
117 | - $xml->writeAttribute($prefix, $ns); |
|
118 | - } |
|
119 | - } |
|
120 | - $this->write($xml, $data, static::$rootName); |
|
121 | - $xml->endElement(); |
|
90 | + public function encode($data, $humanReadable = false) |
|
91 | + { |
|
92 | + $data = Obj::toArray($data); |
|
93 | + $xml = new XMLWriter(); |
|
94 | + $xml->openMemory(); |
|
95 | + $xml->startDocument('1.0', $this->charset); |
|
96 | + if ($humanReadable) { |
|
97 | + $xml->setIndent(true); |
|
98 | + $xml->setIndentString(' '); |
|
99 | + } |
|
100 | + static::$useNamespaces && isset(static::$namespacedProperties[static::$rootName]) |
|
101 | + ? |
|
102 | + $xml->startElementNs( |
|
103 | + static::$namespacedProperties[static::$rootName], |
|
104 | + static::$rootName, |
|
105 | + static::$namespaces[static::$namespacedProperties[static::$rootName]] |
|
106 | + ) |
|
107 | + : |
|
108 | + $xml->startElement(static::$rootName); |
|
109 | + if (static::$useNamespaces) { |
|
110 | + foreach (static::$namespaces as $prefix => $ns) { |
|
111 | + if (isset(static::$namespacedProperties[static::$rootName]) |
|
112 | + && static::$namespacedProperties[static::$rootName] == $prefix |
|
113 | + ) { |
|
114 | + continue; |
|
115 | + } |
|
116 | + $prefix = 'xmlns' . (empty($prefix) ? '' : ':' . $prefix); |
|
117 | + $xml->writeAttribute($prefix, $ns); |
|
118 | + } |
|
119 | + } |
|
120 | + $this->write($xml, $data, static::$rootName); |
|
121 | + $xml->endElement(); |
|
122 | 122 | |
123 | - return $xml->outputMemory(); |
|
124 | - } |
|
123 | + return $xml->outputMemory(); |
|
124 | + } |
|
125 | 125 | |
126 | - public function write(XMLWriter $xml, $data, $parent) |
|
127 | - { |
|
128 | - $text = array(); |
|
129 | - if (is_array($data)) { |
|
130 | - if (static::$useTextNodeProperty && isset($data[static::$textNodeName])) { |
|
131 | - $text [] = $data[static::$textNodeName]; |
|
132 | - unset($data[static::$textNodeName]); |
|
133 | - } |
|
134 | - $attributes = array_flip(static::$attributeNames); |
|
135 | - //make sure we deal with attributes first |
|
136 | - $temp = array(); |
|
137 | - foreach ($data as $key => $value) { |
|
138 | - if (isset($attributes[$key])) { |
|
139 | - $temp[$key] = $data[$key]; |
|
140 | - unset($data[$key]); |
|
141 | - } |
|
142 | - } |
|
143 | - $data = array_merge($temp, $data); |
|
144 | - foreach ($data as $key => $value) { |
|
145 | - if (is_numeric($key)) { |
|
146 | - if (!is_array($value)) { |
|
147 | - $text [] = $value; |
|
148 | - continue; |
|
149 | - } |
|
150 | - $key = static::$defaultTagName; |
|
151 | - } |
|
152 | - $useNS = static::$useNamespaces |
|
153 | - && !empty(static::$namespacedProperties[$key]) |
|
154 | - && false === strpos($key, ':'); |
|
155 | - if (is_array($value)) { |
|
156 | - $useNS |
|
157 | - ? $xml->startElementNs( |
|
158 | - static::$namespacedProperties[$key], |
|
159 | - $key, |
|
160 | - null |
|
161 | - ) |
|
162 | - : $xml->startElement($key); |
|
163 | - $this->write($xml, $value, $key); |
|
164 | - $xml->endElement(); |
|
165 | - continue; |
|
166 | - } elseif (is_bool($value)) { |
|
167 | - $value = $value ? 'true' : 'false'; |
|
168 | - } |
|
169 | - if (isset($attributes[$key])) { |
|
170 | - $xml->writeAttribute($useNS ? static::$namespacedProperties[$key] . ':' . $key : $key, $value); |
|
171 | - } else { |
|
172 | - $useNS |
|
173 | - ? |
|
174 | - $xml->startElementNs( |
|
175 | - static::$namespacedProperties[$key], |
|
176 | - $key, |
|
177 | - null |
|
178 | - ) |
|
179 | - : $xml->startElement($key); |
|
180 | - $this->write($xml, $value, $key); |
|
181 | - $xml->endElement(); |
|
182 | - } |
|
183 | - } |
|
184 | - } else { |
|
185 | - $text [] = (string)$data; |
|
186 | - } |
|
187 | - if (!empty($text)) { |
|
188 | - if (count($text) == 1) { |
|
189 | - in_array($parent, static::$cdataNames) |
|
190 | - ? $xml->writeCdata(implode('', $text)) |
|
191 | - : $xml->text(implode('', $text)); |
|
192 | - } else { |
|
193 | - foreach ($text as $t) { |
|
194 | - $xml->writeElement(static::$textNodeName, $t); |
|
195 | - } |
|
196 | - } |
|
197 | - } |
|
198 | - } |
|
126 | + public function write(XMLWriter $xml, $data, $parent) |
|
127 | + { |
|
128 | + $text = array(); |
|
129 | + if (is_array($data)) { |
|
130 | + if (static::$useTextNodeProperty && isset($data[static::$textNodeName])) { |
|
131 | + $text [] = $data[static::$textNodeName]; |
|
132 | + unset($data[static::$textNodeName]); |
|
133 | + } |
|
134 | + $attributes = array_flip(static::$attributeNames); |
|
135 | + //make sure we deal with attributes first |
|
136 | + $temp = array(); |
|
137 | + foreach ($data as $key => $value) { |
|
138 | + if (isset($attributes[$key])) { |
|
139 | + $temp[$key] = $data[$key]; |
|
140 | + unset($data[$key]); |
|
141 | + } |
|
142 | + } |
|
143 | + $data = array_merge($temp, $data); |
|
144 | + foreach ($data as $key => $value) { |
|
145 | + if (is_numeric($key)) { |
|
146 | + if (!is_array($value)) { |
|
147 | + $text [] = $value; |
|
148 | + continue; |
|
149 | + } |
|
150 | + $key = static::$defaultTagName; |
|
151 | + } |
|
152 | + $useNS = static::$useNamespaces |
|
153 | + && !empty(static::$namespacedProperties[$key]) |
|
154 | + && false === strpos($key, ':'); |
|
155 | + if (is_array($value)) { |
|
156 | + $useNS |
|
157 | + ? $xml->startElementNs( |
|
158 | + static::$namespacedProperties[$key], |
|
159 | + $key, |
|
160 | + null |
|
161 | + ) |
|
162 | + : $xml->startElement($key); |
|
163 | + $this->write($xml, $value, $key); |
|
164 | + $xml->endElement(); |
|
165 | + continue; |
|
166 | + } elseif (is_bool($value)) { |
|
167 | + $value = $value ? 'true' : 'false'; |
|
168 | + } |
|
169 | + if (isset($attributes[$key])) { |
|
170 | + $xml->writeAttribute($useNS ? static::$namespacedProperties[$key] . ':' . $key : $key, $value); |
|
171 | + } else { |
|
172 | + $useNS |
|
173 | + ? |
|
174 | + $xml->startElementNs( |
|
175 | + static::$namespacedProperties[$key], |
|
176 | + $key, |
|
177 | + null |
|
178 | + ) |
|
179 | + : $xml->startElement($key); |
|
180 | + $this->write($xml, $value, $key); |
|
181 | + $xml->endElement(); |
|
182 | + } |
|
183 | + } |
|
184 | + } else { |
|
185 | + $text [] = (string)$data; |
|
186 | + } |
|
187 | + if (!empty($text)) { |
|
188 | + if (count($text) == 1) { |
|
189 | + in_array($parent, static::$cdataNames) |
|
190 | + ? $xml->writeCdata(implode('', $text)) |
|
191 | + : $xml->text(implode('', $text)); |
|
192 | + } else { |
|
193 | + foreach ($text as $t) { |
|
194 | + $xml->writeElement(static::$textNodeName, $t); |
|
195 | + } |
|
196 | + } |
|
197 | + } |
|
198 | + } |
|
199 | 199 | |
200 | - public function decode($data) |
|
201 | - { |
|
202 | - try { |
|
203 | - if ($data == '') { |
|
204 | - return array(); |
|
205 | - } |
|
206 | - libxml_use_internal_errors(true); |
|
207 | - libxml_disable_entity_loader(true); |
|
208 | - $xml = simplexml_load_string($data, |
|
209 | - "SimpleXMLElement", LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_COMPACT); |
|
210 | - if (false === $xml) { |
|
211 | - $error = libxml_get_last_error(); |
|
212 | - throw new RestException(400, 'Malformed XML. ' |
|
213 | - . trim($error->message, "\r\n") . ' at line ' . $error->line); |
|
214 | - } |
|
215 | - libxml_clear_errors(); |
|
216 | - if (static::$importSettingsFromXml) { |
|
217 | - static::$attributeNames = array(); |
|
218 | - static::$namespacedProperties = array(); |
|
219 | - static::$namespaces = array(); |
|
220 | - static::$rootName = $xml->getName(); |
|
221 | - $namespaces = $xml->getNamespaces(); |
|
222 | - if (count($namespaces)) { |
|
223 | - $p = strpos($data, $xml->getName()); |
|
224 | - if ($p && $data[$p - 1] == ':') { |
|
225 | - $s = strpos($data, '<') + 1; |
|
226 | - $prefix = substr($data, $s, $p - $s - 1); |
|
227 | - static::$namespacedProperties[static::$rootName] = $prefix; |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - $data = $this->read($xml); |
|
232 | - if (count($data) == 1 && isset($data[static::$textNodeName])) { |
|
233 | - $data = $data[static::$textNodeName]; |
|
234 | - } |
|
200 | + public function decode($data) |
|
201 | + { |
|
202 | + try { |
|
203 | + if ($data == '') { |
|
204 | + return array(); |
|
205 | + } |
|
206 | + libxml_use_internal_errors(true); |
|
207 | + libxml_disable_entity_loader(true); |
|
208 | + $xml = simplexml_load_string($data, |
|
209 | + "SimpleXMLElement", LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_COMPACT); |
|
210 | + if (false === $xml) { |
|
211 | + $error = libxml_get_last_error(); |
|
212 | + throw new RestException(400, 'Malformed XML. ' |
|
213 | + . trim($error->message, "\r\n") . ' at line ' . $error->line); |
|
214 | + } |
|
215 | + libxml_clear_errors(); |
|
216 | + if (static::$importSettingsFromXml) { |
|
217 | + static::$attributeNames = array(); |
|
218 | + static::$namespacedProperties = array(); |
|
219 | + static::$namespaces = array(); |
|
220 | + static::$rootName = $xml->getName(); |
|
221 | + $namespaces = $xml->getNamespaces(); |
|
222 | + if (count($namespaces)) { |
|
223 | + $p = strpos($data, $xml->getName()); |
|
224 | + if ($p && $data[$p - 1] == ':') { |
|
225 | + $s = strpos($data, '<') + 1; |
|
226 | + $prefix = substr($data, $s, $p - $s - 1); |
|
227 | + static::$namespacedProperties[static::$rootName] = $prefix; |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + $data = $this->read($xml); |
|
232 | + if (count($data) == 1 && isset($data[static::$textNodeName])) { |
|
233 | + $data = $data[static::$textNodeName]; |
|
234 | + } |
|
235 | 235 | |
236 | - return $data; |
|
237 | - } catch (\RuntimeException $e) { |
|
238 | - throw new RestException(400, |
|
239 | - "Error decoding request. " . $e->getMessage()); |
|
240 | - } |
|
241 | - } |
|
236 | + return $data; |
|
237 | + } catch (\RuntimeException $e) { |
|
238 | + throw new RestException(400, |
|
239 | + "Error decoding request. " . $e->getMessage()); |
|
240 | + } |
|
241 | + } |
|
242 | 242 | |
243 | - public function read(SimpleXMLElement $xml, $namespaces = null) |
|
244 | - { |
|
245 | - $r = array(); |
|
246 | - $text = (string)$xml; |
|
243 | + public function read(SimpleXMLElement $xml, $namespaces = null) |
|
244 | + { |
|
245 | + $r = array(); |
|
246 | + $text = (string)$xml; |
|
247 | 247 | |
248 | - if (static::$parseAttributes) { |
|
249 | - $attributes = $xml->attributes(); |
|
250 | - foreach ($attributes as $key => $value) { |
|
251 | - if (static::$importSettingsFromXml |
|
252 | - && !in_array($key, static::$attributeNames) |
|
253 | - ) { |
|
254 | - static::$attributeNames[] = $key; |
|
255 | - } |
|
256 | - $r[$key] = static::setType((string)$value); |
|
257 | - } |
|
258 | - } |
|
259 | - $children = $xml->children(); |
|
260 | - foreach ($children as $key => $value) { |
|
261 | - if ($key == static::$defaultTagName) { |
|
262 | - $r[] = $this->read($value); |
|
263 | - } elseif (isset($r[$key])) { |
|
264 | - if (is_array($r[$key])) { |
|
265 | - if ($r[$key] != array_values($r[$key])) { |
|
266 | - $r[$key] = array($r[$key]); |
|
267 | - } |
|
268 | - } else { |
|
269 | - $r[$key] = array($r[$key]); |
|
270 | - } |
|
271 | - $r[$key][] = $this->read($value, $namespaces); |
|
272 | - } else { |
|
273 | - $r[$key] = $this->read($value); |
|
274 | - } |
|
275 | - } |
|
248 | + if (static::$parseAttributes) { |
|
249 | + $attributes = $xml->attributes(); |
|
250 | + foreach ($attributes as $key => $value) { |
|
251 | + if (static::$importSettingsFromXml |
|
252 | + && !in_array($key, static::$attributeNames) |
|
253 | + ) { |
|
254 | + static::$attributeNames[] = $key; |
|
255 | + } |
|
256 | + $r[$key] = static::setType((string)$value); |
|
257 | + } |
|
258 | + } |
|
259 | + $children = $xml->children(); |
|
260 | + foreach ($children as $key => $value) { |
|
261 | + if ($key == static::$defaultTagName) { |
|
262 | + $r[] = $this->read($value); |
|
263 | + } elseif (isset($r[$key])) { |
|
264 | + if (is_array($r[$key])) { |
|
265 | + if ($r[$key] != array_values($r[$key])) { |
|
266 | + $r[$key] = array($r[$key]); |
|
267 | + } |
|
268 | + } else { |
|
269 | + $r[$key] = array($r[$key]); |
|
270 | + } |
|
271 | + $r[$key][] = $this->read($value, $namespaces); |
|
272 | + } else { |
|
273 | + $r[$key] = $this->read($value); |
|
274 | + } |
|
275 | + } |
|
276 | 276 | |
277 | - if (static::$parseNamespaces) { |
|
278 | - if (is_null($namespaces)) { |
|
279 | - $namespaces = $xml->getDocNamespaces(true); |
|
280 | - } |
|
281 | - foreach ($namespaces as $prefix => $ns) { |
|
282 | - static::$namespaces[$prefix] = $ns; |
|
283 | - if (static::$parseAttributes) { |
|
284 | - $attributes = $xml->attributes($ns); |
|
285 | - foreach ($attributes as $key => $value) { |
|
286 | - if (isset($r[$key])) { |
|
287 | - $key = "{$prefix}:$key"; |
|
288 | - } |
|
289 | - if (static::$importSettingsFromXml |
|
290 | - && !in_array($key, static::$attributeNames) |
|
291 | - ) { |
|
292 | - static::$namespacedProperties[$key] = $prefix; |
|
293 | - static::$attributeNames[] = $key; |
|
294 | - } |
|
295 | - $r[$key] = static::setType((string)$value); |
|
296 | - } |
|
297 | - } |
|
298 | - $children = $xml->children($ns); |
|
299 | - foreach ($children as $key => $value) { |
|
300 | - if (static::$importSettingsFromXml) { |
|
301 | - static::$namespacedProperties[$key] = $prefix; |
|
302 | - } |
|
303 | - if (isset($r[$key])) { |
|
304 | - if (is_array($r[$key])) { |
|
305 | - if ($r[$key] != array_values($r[$key])) { |
|
306 | - $r[$key] = array($r[$key]); |
|
307 | - } |
|
308 | - } else { |
|
309 | - $r[$key] = array($r[$key]); |
|
310 | - } |
|
311 | - $r[$key][] = $this->read($value, $namespaces); |
|
312 | - } else { |
|
313 | - $r[$key] = $this->read($value, $namespaces); |
|
314 | - } |
|
315 | - } |
|
316 | - } |
|
317 | - } |
|
277 | + if (static::$parseNamespaces) { |
|
278 | + if (is_null($namespaces)) { |
|
279 | + $namespaces = $xml->getDocNamespaces(true); |
|
280 | + } |
|
281 | + foreach ($namespaces as $prefix => $ns) { |
|
282 | + static::$namespaces[$prefix] = $ns; |
|
283 | + if (static::$parseAttributes) { |
|
284 | + $attributes = $xml->attributes($ns); |
|
285 | + foreach ($attributes as $key => $value) { |
|
286 | + if (isset($r[$key])) { |
|
287 | + $key = "{$prefix}:$key"; |
|
288 | + } |
|
289 | + if (static::$importSettingsFromXml |
|
290 | + && !in_array($key, static::$attributeNames) |
|
291 | + ) { |
|
292 | + static::$namespacedProperties[$key] = $prefix; |
|
293 | + static::$attributeNames[] = $key; |
|
294 | + } |
|
295 | + $r[$key] = static::setType((string)$value); |
|
296 | + } |
|
297 | + } |
|
298 | + $children = $xml->children($ns); |
|
299 | + foreach ($children as $key => $value) { |
|
300 | + if (static::$importSettingsFromXml) { |
|
301 | + static::$namespacedProperties[$key] = $prefix; |
|
302 | + } |
|
303 | + if (isset($r[$key])) { |
|
304 | + if (is_array($r[$key])) { |
|
305 | + if ($r[$key] != array_values($r[$key])) { |
|
306 | + $r[$key] = array($r[$key]); |
|
307 | + } |
|
308 | + } else { |
|
309 | + $r[$key] = array($r[$key]); |
|
310 | + } |
|
311 | + $r[$key][] = $this->read($value, $namespaces); |
|
312 | + } else { |
|
313 | + $r[$key] = $this->read($value, $namespaces); |
|
314 | + } |
|
315 | + } |
|
316 | + } |
|
317 | + } |
|
318 | 318 | |
319 | - if (empty($text) && $text !== '0') { |
|
320 | - if (empty($r)) { |
|
321 | - return null; |
|
322 | - } |
|
323 | - } else { |
|
324 | - empty($r) |
|
325 | - ? $r = static::setType($text) |
|
326 | - : ( |
|
327 | - static::$parseTextNodeAsProperty |
|
328 | - ? $r[static::$textNodeName] = static::setType($text) |
|
329 | - : $r[] = static::setType($text) |
|
330 | - ); |
|
331 | - } |
|
319 | + if (empty($text) && $text !== '0') { |
|
320 | + if (empty($r)) { |
|
321 | + return null; |
|
322 | + } |
|
323 | + } else { |
|
324 | + empty($r) |
|
325 | + ? $r = static::setType($text) |
|
326 | + : ( |
|
327 | + static::$parseTextNodeAsProperty |
|
328 | + ? $r[static::$textNodeName] = static::setType($text) |
|
329 | + : $r[] = static::setType($text) |
|
330 | + ); |
|
331 | + } |
|
332 | 332 | |
333 | - return $r; |
|
334 | - } |
|
333 | + return $r; |
|
334 | + } |
|
335 | 335 | |
336 | - public static function setType($value) |
|
337 | - { |
|
338 | - if (empty($value) && $value !== '0') { |
|
339 | - return null; |
|
340 | - } |
|
341 | - if ($value == 'true') { |
|
342 | - return true; |
|
343 | - } |
|
344 | - if ($value == 'false') { |
|
345 | - return true; |
|
346 | - } |
|
347 | - if (is_numeric($value)) { |
|
348 | - return 0 + $value; |
|
349 | - } |
|
336 | + public static function setType($value) |
|
337 | + { |
|
338 | + if (empty($value) && $value !== '0') { |
|
339 | + return null; |
|
340 | + } |
|
341 | + if ($value == 'true') { |
|
342 | + return true; |
|
343 | + } |
|
344 | + if ($value == 'false') { |
|
345 | + return true; |
|
346 | + } |
|
347 | + if (is_numeric($value)) { |
|
348 | + return 0 + $value; |
|
349 | + } |
|
350 | 350 | |
351 | - return $value; |
|
352 | - } |
|
351 | + return $value; |
|
352 | + } |
|
353 | 353 | } |
@@ -68,20 +68,20 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public static function exportCurrentSettings() |
70 | 70 | { |
71 | - $s = 'XmlFormat::$rootName = "' . (self::$rootName) . "\";\n"; |
|
72 | - $s .= 'XmlFormat::$attributeNames = ' . |
|
73 | - (var_export(self::$attributeNames, true)) . ";\n"; |
|
74 | - $s .= 'XmlFormat::$defaultTagName = "' . |
|
75 | - self::$defaultTagName . "\";\n"; |
|
76 | - $s .= 'XmlFormat::$parseAttributes = ' . |
|
77 | - (self::$parseAttributes ? 'true' : 'false') . ";\n"; |
|
78 | - $s .= 'XmlFormat::$parseNamespaces = ' . |
|
79 | - (self::$parseNamespaces ? 'true' : 'false') . ";\n"; |
|
71 | + $s = 'XmlFormat::$rootName = "'.(self::$rootName)."\";\n"; |
|
72 | + $s .= 'XmlFormat::$attributeNames = '. |
|
73 | + (var_export(self::$attributeNames, true)).";\n"; |
|
74 | + $s .= 'XmlFormat::$defaultTagName = "'. |
|
75 | + self::$defaultTagName."\";\n"; |
|
76 | + $s .= 'XmlFormat::$parseAttributes = '. |
|
77 | + (self::$parseAttributes ? 'true' : 'false').";\n"; |
|
78 | + $s .= 'XmlFormat::$parseNamespaces = '. |
|
79 | + (self::$parseNamespaces ? 'true' : 'false').";\n"; |
|
80 | 80 | if (self::$parseNamespaces) { |
81 | - $s .= 'XmlFormat::$namespaces = ' . |
|
82 | - (var_export(self::$namespaces, true)) . ";\n"; |
|
83 | - $s .= 'XmlFormat::$namespacedProperties = ' . |
|
84 | - (var_export(self::$namespacedProperties, true)) . ";\n"; |
|
81 | + $s .= 'XmlFormat::$namespaces = '. |
|
82 | + (var_export(self::$namespaces, true)).";\n"; |
|
83 | + $s .= 'XmlFormat::$namespacedProperties = '. |
|
84 | + (var_export(self::$namespacedProperties, true)).";\n"; |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | return $s; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | ) { |
114 | 114 | continue; |
115 | 115 | } |
116 | - $prefix = 'xmlns' . (empty($prefix) ? '' : ':' . $prefix); |
|
116 | + $prefix = 'xmlns'.(empty($prefix) ? '' : ':'.$prefix); |
|
117 | 117 | $xml->writeAttribute($prefix, $ns); |
118 | 118 | } |
119 | 119 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $value = $value ? 'true' : 'false'; |
168 | 168 | } |
169 | 169 | if (isset($attributes[$key])) { |
170 | - $xml->writeAttribute($useNS ? static::$namespacedProperties[$key] . ':' . $key : $key, $value); |
|
170 | + $xml->writeAttribute($useNS ? static::$namespacedProperties[$key].':'.$key : $key, $value); |
|
171 | 171 | } else { |
172 | 172 | $useNS |
173 | 173 | ? |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | } |
183 | 183 | } |
184 | 184 | } else { |
185 | - $text [] = (string)$data; |
|
185 | + $text [] = (string) $data; |
|
186 | 186 | } |
187 | 187 | if (!empty($text)) { |
188 | 188 | if (count($text) == 1) { |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | if (false === $xml) { |
211 | 211 | $error = libxml_get_last_error(); |
212 | 212 | throw new RestException(400, 'Malformed XML. ' |
213 | - . trim($error->message, "\r\n") . ' at line ' . $error->line); |
|
213 | + . trim($error->message, "\r\n").' at line '.$error->line); |
|
214 | 214 | } |
215 | 215 | libxml_clear_errors(); |
216 | 216 | if (static::$importSettingsFromXml) { |
@@ -236,14 +236,14 @@ discard block |
||
236 | 236 | return $data; |
237 | 237 | } catch (\RuntimeException $e) { |
238 | 238 | throw new RestException(400, |
239 | - "Error decoding request. " . $e->getMessage()); |
|
239 | + "Error decoding request. ".$e->getMessage()); |
|
240 | 240 | } |
241 | 241 | } |
242 | 242 | |
243 | 243 | public function read(SimpleXMLElement $xml, $namespaces = null) |
244 | 244 | { |
245 | 245 | $r = array(); |
246 | - $text = (string)$xml; |
|
246 | + $text = (string) $xml; |
|
247 | 247 | |
248 | 248 | if (static::$parseAttributes) { |
249 | 249 | $attributes = $xml->attributes(); |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | ) { |
254 | 254 | static::$attributeNames[] = $key; |
255 | 255 | } |
256 | - $r[$key] = static::setType((string)$value); |
|
256 | + $r[$key] = static::setType((string) $value); |
|
257 | 257 | } |
258 | 258 | } |
259 | 259 | $children = $xml->children(); |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | static::$namespacedProperties[$key] = $prefix; |
293 | 293 | static::$attributeNames[] = $key; |
294 | 294 | } |
295 | - $r[$key] = static::setType((string)$value); |
|
295 | + $r[$key] = static::setType((string) $value); |
|
296 | 296 | } |
297 | 297 | } |
298 | 298 | $children = $xml->children($ns); |
@@ -38,525 +38,525 @@ |
||
38 | 38 | */ |
39 | 39 | class HtmlFormat extends DependentFormat |
40 | 40 | { |
41 | - const BLADE = 'Illuminate\View\View'; |
|
42 | - const TWIG = 'Twig\Environment'; |
|
43 | - const MUSTACHE = 'Mustache_Engine'; |
|
44 | - |
|
45 | - public static $mime = 'text/html'; |
|
46 | - public static $extension = 'html'; |
|
47 | - public static $view; |
|
48 | - public static $errorView = 'debug.php'; |
|
49 | - public static $template = 'php'; |
|
50 | - public static $handleSession = true; |
|
51 | - public static $convertResponseToArray = false; |
|
52 | - |
|
53 | - public static $useSmartViews = true; |
|
54 | - /** |
|
55 | - * @var null|string defaults to template named folder in Defaults::$cacheDirectory |
|
56 | - */ |
|
57 | - public static $cacheDirectory = null; |
|
58 | - /** |
|
59 | - * @var array global key value pair to be supplied to the templates. All |
|
60 | - * keys added here will be available as a variable inside the template |
|
61 | - */ |
|
62 | - public static $data = array(); |
|
63 | - /** |
|
64 | - * @var string set it to the location of your the view files. Defaults to |
|
65 | - * views folder which is same level as vendor directory. |
|
66 | - */ |
|
67 | - public static $viewPath; |
|
68 | - /** |
|
69 | - * @var array template and its custom extension key value pair |
|
70 | - */ |
|
71 | - public static $customTemplateExtensions = array('blade' => 'blade.php'); |
|
72 | - /** |
|
73 | - * @var bool used internally for error handling |
|
74 | - */ |
|
75 | - protected static $parseViewMetadata = true; |
|
76 | - /** |
|
77 | - * @var Restler; |
|
78 | - */ |
|
79 | - public $restler; |
|
80 | - |
|
81 | - public function __construct() |
|
82 | - { |
|
83 | - //============ SESSION MANAGEMENT =============// |
|
84 | - if (static::$handleSession) { |
|
85 | - if (session_start() && isset($_SESSION['flash'])) { |
|
86 | - static::$data['flash'] = $_SESSION['flash']; |
|
87 | - unset($_SESSION['flash']); |
|
88 | - } |
|
89 | - } |
|
90 | - if (!static::$viewPath) { |
|
91 | - $array = explode('vendor', __DIR__, 2); |
|
92 | - static::$viewPath = $array[0] . 'views'; |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - public function getDependencyMap() |
|
97 | - { |
|
98 | - return array( |
|
99 | - self::BLADE => 'illuminate/view:^8', |
|
100 | - self::TWIG => 'twig/twig:^3', |
|
101 | - self::MUSTACHE => 'mustache/mustache:dev-master', |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - public static function blade(array $data, $debug = true) |
|
106 | - { |
|
107 | - $resolver = new EngineResolver(); |
|
108 | - $files = new Filesystem(); |
|
109 | - $compiler = new BladeCompiler($files, static::$cacheDirectory); |
|
110 | - $engine = new CompilerEngine($compiler); |
|
111 | - $resolver->register('blade', function () use ($engine) { |
|
112 | - return $engine; |
|
113 | - }); |
|
114 | - $phpEngine = new PhpEngine($files); |
|
115 | - $resolver->register('php', function () use ($phpEngine) { |
|
116 | - return $phpEngine; |
|
117 | - }); |
|
118 | - |
|
119 | - /** @var Restler $restler */ |
|
120 | - $restler = Scope::get('Restler'); |
|
121 | - |
|
122 | - //Lets expose shortcuts for our classes |
|
123 | - spl_autoload_register(function ($className) use ($restler) { |
|
124 | - if (isset($restler->apiMethodInfo->metadata['scope'][$className])) { |
|
125 | - return class_alias($restler->apiMethodInfo->metadata['scope'][$className], $className); |
|
126 | - } |
|
127 | - if (isset(Scope::$classAliases[$className])) { |
|
128 | - return class_alias(Scope::$classAliases[$className], $className); |
|
129 | - } |
|
130 | - return false; |
|
131 | - }, true, true); |
|
132 | - |
|
133 | - $viewFinder = new FileViewFinder($files, array(static::$viewPath)); |
|
134 | - $factory = new Factory($resolver, $viewFinder, new Dispatcher()); |
|
135 | - $path = $viewFinder->find(self::$view); |
|
136 | - $view = new View($factory, $engine, self::$view, $path, $data); |
|
137 | - $factory->callCreator($view); |
|
138 | - return $view->render(); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param array|object $data |
|
143 | - * @param bool $debug |
|
144 | - * |
|
145 | - * @return string |
|
146 | - * @throws \Twig\Error\LoaderError |
|
147 | - * @throws \Twig\Error\RuntimeError |
|
148 | - * @throws \Twig\Error\SyntaxError |
|
149 | - */ |
|
150 | - public static function twig($data, $debug = true) |
|
151 | - { |
|
152 | - $loader = new FilesystemLoader(static::$viewPath); |
|
153 | - $twig = new Environment($loader, array( |
|
154 | - 'cache' => is_string(static::$cacheDirectory) |
|
155 | - ? static::$cacheDirectory : false, |
|
156 | - 'debug' => $debug, |
|
157 | - 'use_strict_variables' => $debug, |
|
158 | - )); |
|
159 | - if ($debug) { |
|
160 | - $twig->addExtension(new DebugExtension()); |
|
161 | - } |
|
162 | - |
|
163 | - $twig->addFunction( |
|
164 | - new TwigFunction( |
|
165 | - 'form', |
|
166 | - 'Luracast\Restler\UI\Forms::get', |
|
167 | - array('is_safe' => array('html')) |
|
168 | - ) |
|
169 | - ); |
|
170 | - $twig->addFunction( |
|
171 | - new TwigFunction( |
|
172 | - 'form_key', |
|
173 | - 'Luracast\Restler\UI\Forms::key' |
|
174 | - ) |
|
175 | - ); |
|
176 | - $twig->addFunction( |
|
177 | - new TwigFunction( |
|
178 | - 'nav', |
|
179 | - 'Luracast\Restler\UI\Nav::get' |
|
180 | - ) |
|
181 | - ); |
|
182 | - |
|
183 | - $twig->registerUndefinedFunctionCallback(function ($name) { |
|
184 | - if ( |
|
185 | - isset(HtmlFormat::$data[$name]) && |
|
186 | - is_callable(HtmlFormat::$data[$name]) |
|
187 | - ) { |
|
188 | - return new TwigFunction( |
|
189 | - $name, |
|
190 | - HtmlFormat::$data[$name] |
|
191 | - ); |
|
192 | - } |
|
193 | - return false; |
|
194 | - }); |
|
195 | - |
|
196 | - $template = $twig->load(static::getViewFile()); |
|
197 | - return $template->render((array)$data); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @param array|object $data |
|
202 | - * @param bool $debug |
|
203 | - * |
|
204 | - * @return string |
|
205 | - */ |
|
206 | - public static function handlebar($data, $debug = true) |
|
207 | - { |
|
208 | - return static::mustache($data, $debug); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @param array|object $data |
|
213 | - * @param bool $debug |
|
214 | - * |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - public static function mustache($data, $debug = true) |
|
218 | - { |
|
219 | - $data = (array)$data; |
|
220 | - if (!isset($data['nav'])) { |
|
221 | - $data['nav'] = array_values(Nav::get()); |
|
222 | - } |
|
223 | - $options = array( |
|
224 | - 'loader' => new \Mustache_Loader_FilesystemLoader( |
|
225 | - static::$viewPath, |
|
226 | - array('extension' => static::getViewExtension()) |
|
227 | - ), |
|
228 | - 'helpers' => array( |
|
229 | - 'form' => function ($text, \Mustache_LambdaHelper $m) { |
|
230 | - $params = explode(',', $m->render($text)); |
|
231 | - return call_user_func_array( |
|
232 | - 'Luracast\Restler\UI\Forms::get', |
|
233 | - $params |
|
234 | - ); |
|
235 | - }, |
|
236 | - ) |
|
237 | - ); |
|
238 | - if (!$debug) { |
|
239 | - $options['cache'] = static::$cacheDirectory; |
|
240 | - } |
|
241 | - $m = new \Mustache_Engine($options); |
|
242 | - return $m->render(static::getViewFile(), $data); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @param array|object $data |
|
247 | - * @param bool $debug |
|
248 | - * |
|
249 | - * @return string |
|
250 | - * @throws RestException |
|
251 | - */ |
|
252 | - public static function php($data, $debug = true) |
|
253 | - { |
|
254 | - if (static::$view == 'debug') { |
|
255 | - static::$viewPath = dirname(__DIR__) . '/views'; |
|
256 | - } |
|
257 | - $view = static::getViewFile(true); |
|
258 | - |
|
259 | - if (!is_readable($view)) { |
|
260 | - throw new RestException( |
|
261 | - 500, |
|
262 | - "view file `$view` is not readable. " . |
|
263 | - 'Check for file presence and file permissions' |
|
264 | - ); |
|
265 | - } |
|
266 | - |
|
267 | - $path = static::$viewPath . DIRECTORY_SEPARATOR; |
|
268 | - $template = function ($view) use ($data, $path) { |
|
269 | - $data = (array)$data; |
|
270 | - $form = function () { |
|
271 | - return call_user_func_array( |
|
272 | - 'Luracast\Restler\UI\Forms::get', |
|
273 | - func_get_args() |
|
274 | - ); |
|
275 | - }; |
|
276 | - if (!isset($data['form'])) { |
|
277 | - $data['form'] = $form; |
|
278 | - } |
|
279 | - $nav = function () { |
|
280 | - return call_user_func_array( |
|
281 | - 'Luracast\Restler\UI\Nav::get', |
|
282 | - func_get_args() |
|
283 | - ); |
|
284 | - }; |
|
285 | - if (!isset($data['nav'])) { |
|
286 | - $data['nav'] = $nav; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - $_ = function () use ($data, $path) { |
|
291 | - extract($data); |
|
292 | - $args = func_get_args(); |
|
293 | - $task = array_shift($args); |
|
294 | - switch ($task) { |
|
295 | - case 'require': |
|
296 | - case 'include': |
|
297 | - $file = $path . $args[0]; |
|
298 | - if (is_readable($file)) { |
|
299 | - if ( |
|
300 | - isset($args[1]) && |
|
301 | - ($arrays = Util::nestedValue($data, $args[1])) |
|
302 | - ) { |
|
303 | - $str = ''; |
|
304 | - foreach ($arrays as $arr) { |
|
305 | - extract((array)$arr); |
|
306 | - $str .= include $file; |
|
307 | - } |
|
308 | - return $str; |
|
309 | - } else { |
|
310 | - return include $file; |
|
311 | - } |
|
312 | - } |
|
313 | - break; |
|
314 | - case 'if': |
|
315 | - if (count($args) < 2) { |
|
316 | - $args[1] = ''; |
|
317 | - } |
|
318 | - if (count($args) < 3) { |
|
319 | - $args[2] = ''; |
|
320 | - } |
|
321 | - return $args[0] ? $args[1] : $args[2]; |
|
322 | - break; |
|
323 | - default: |
|
324 | - if (isset($data[$task]) && is_callable($data[$task])) { |
|
325 | - return call_user_func_array($data[$task], $args); |
|
326 | - } |
|
327 | - } |
|
328 | - return ''; |
|
329 | - }; |
|
330 | - extract($data); |
|
331 | - return @include $view; |
|
332 | - }; |
|
333 | - $value = $template($view); |
|
334 | - if (is_string($value)) { |
|
335 | - return $value; |
|
336 | - } |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Encode the given data in the format |
|
341 | - * |
|
342 | - * @param array $data resulting data that needs to |
|
343 | - * be encoded in the given format |
|
344 | - * @param boolean $humanReadable set to TRUE when restler |
|
345 | - * is not running in production mode. |
|
346 | - * Formatter has to make the encoded |
|
347 | - * output more human readable |
|
348 | - * |
|
349 | - * @return string encoded string |
|
350 | - * @throws \Exception |
|
351 | - */ |
|
352 | - public function encode($data, $humanReadable = false) |
|
353 | - { |
|
354 | - if (!is_readable(static::$viewPath)) { |
|
355 | - throw new \Exception( |
|
356 | - 'The views directory `' |
|
357 | - . self::$viewPath . '` should exist with read permission.' |
|
358 | - ); |
|
359 | - } |
|
360 | - static::$data['basePath'] = dirname($_SERVER['SCRIPT_NAME']); |
|
361 | - static::$data['baseUrl'] = $this->restler->getBaseUrl(); |
|
362 | - static::$data['currentPath'] = $this->restler->url; |
|
363 | - |
|
364 | - try { |
|
365 | - $exception = $this->restler->exception; |
|
366 | - $success = is_null($exception); |
|
367 | - $error = $success ? null : $exception->getMessage(); |
|
368 | - $data = array( |
|
369 | - 'response' => static::$convertResponseToArray |
|
370 | - ? Obj::toArray($data) |
|
371 | - : $data, |
|
372 | - 'stages' => $this->restler->getEvents(), |
|
373 | - 'success' => $success, |
|
374 | - 'error' => $error |
|
375 | - ); |
|
376 | - $info = $data['api'] = $this->restler->apiMethodInfo; |
|
377 | - $metadata = Util::nestedValue( |
|
378 | - $this->restler, 'apiMethodInfo', 'metadata' |
|
379 | - ); |
|
380 | - $view = $success ? 'view' : 'errorView'; |
|
381 | - $value = false; |
|
382 | - if (static::$parseViewMetadata && isset($metadata[$view])) { |
|
383 | - if (is_array($metadata[$view])) { |
|
384 | - self::$view = $metadata[$view]['description']; |
|
385 | - $value = Util::nestedValue( |
|
386 | - $metadata[$view], 'properties', 'value' |
|
387 | - ); |
|
388 | - } else { |
|
389 | - self::$view = $metadata[$view]; |
|
390 | - } |
|
391 | - } elseif (!self::$view) { |
|
392 | - self::$view = static::guessViewName($this->restler->url); |
|
393 | - } |
|
394 | - if ( |
|
395 | - isset($metadata['param']) |
|
396 | - && (!$value || 0 === strpos($value, 'request')) |
|
397 | - ) { |
|
398 | - $params = $metadata['param']; |
|
399 | - foreach ($params as $index => &$param) { |
|
400 | - $index = intval($index); |
|
401 | - if (is_numeric($index)) { |
|
402 | - $param['value'] = $this |
|
403 | - ->restler |
|
404 | - ->apiMethodInfo |
|
405 | - ->parameters[$index]; |
|
406 | - } |
|
407 | - } |
|
408 | - $data['request']['parameters'] = $params; |
|
409 | - } |
|
410 | - if ($value) { |
|
411 | - $data = Util::nestedValue($data, explode('.', $value)); |
|
412 | - } |
|
413 | - if (is_array($data)) { |
|
414 | - $data += static::$data; |
|
415 | - } |
|
416 | - if (false === ($i = strrpos(self::$view, '.'))) { |
|
417 | - $template = self::$template; |
|
418 | - } else { |
|
419 | - self::$template = $template = substr(self::$view, $i + 1); |
|
420 | - self::$view = substr(self::$view, 0, $i); |
|
421 | - } |
|
422 | - if (!static::$cacheDirectory) { |
|
423 | - static::$cacheDirectory = Defaults::$cacheDirectory . DIRECTORY_SEPARATOR . $template; |
|
424 | - if (!file_exists(static::$cacheDirectory)) { |
|
425 | - if (!mkdir(static::$cacheDirectory, 0770, true)) { |
|
426 | - throw new RestException(500, |
|
427 | - 'Unable to create cache directory `' . static::$cacheDirectory . '`'); |
|
428 | - } |
|
429 | - } |
|
430 | - } |
|
431 | - if (method_exists($class = get_called_class(), $template)) { |
|
432 | - if ($template == 'blade') { |
|
433 | - $this->checkDependency(self::BLADE); |
|
434 | - } elseif ($template == 'twig') { |
|
435 | - $this->checkDependency(self::TWIG); |
|
436 | - } elseif ($template == 'mustache' || $template == 'handlebar') { |
|
437 | - $this->checkDependency(self::MUSTACHE); |
|
438 | - } |
|
439 | - return call_user_func("$class::$template", $data, $humanReadable); |
|
440 | - } |
|
441 | - throw new RestException(500, "Unsupported template system `$template`"); |
|
442 | - } catch (Exception $e) { |
|
443 | - static::$parseViewMetadata = false; |
|
444 | - $this->reset(); |
|
445 | - throw $e; |
|
446 | - } |
|
447 | - } |
|
448 | - |
|
449 | - public static function guessViewName($path) |
|
450 | - { |
|
451 | - if (empty($path)) { |
|
452 | - $path = 'index'; |
|
453 | - } elseif (strpos($path, '/')) { |
|
454 | - $path .= '/index'; |
|
455 | - } |
|
456 | - $file = static::$viewPath . '/' . $path . '.' . static::getViewExtension(); |
|
457 | - |
|
458 | - return static::$useSmartViews && is_readable($file) |
|
459 | - ? $path |
|
460 | - : static::$errorView; |
|
461 | - } |
|
462 | - |
|
463 | - public static function getViewExtension() |
|
464 | - { |
|
465 | - return isset(static::$customTemplateExtensions[static::$template]) |
|
466 | - ? static::$customTemplateExtensions[static::$template] |
|
467 | - : static::$template; |
|
468 | - } |
|
469 | - |
|
470 | - public static function getViewFile($fullPath = false, $includeExtension = true) |
|
471 | - { |
|
472 | - $v = $fullPath ? static::$viewPath . '/' : ''; |
|
473 | - $v .= static::$view; |
|
474 | - if ($includeExtension) { |
|
475 | - $v .= '.' . static::getViewExtension(); |
|
476 | - } |
|
477 | - return $v; |
|
478 | - } |
|
479 | - |
|
480 | - private function reset() |
|
481 | - { |
|
482 | - static::$mime = 'text/html'; |
|
483 | - static::$extension = 'html'; |
|
484 | - static::$view = 'debug'; |
|
485 | - static::$template = 'php'; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * Decode the given data from the format |
|
490 | - * |
|
491 | - * @param string $data |
|
492 | - * data sent from client to |
|
493 | - * the api in the given format. |
|
494 | - * |
|
495 | - * @return array associative array of the parsed data |
|
496 | - * |
|
497 | - * @throws RestException |
|
498 | - */ |
|
499 | - public function decode($data) |
|
500 | - { |
|
501 | - throw new RestException(500, 'HtmlFormat is write only'); |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * @return bool false as HTML format is write only |
|
506 | - */ |
|
507 | - public function isReadable() |
|
508 | - { |
|
509 | - return false; |
|
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * Get MIME type => Extension mappings as an associative array |
|
514 | - * |
|
515 | - * @return array list of mime strings for the format |
|
516 | - * @example array('application/json'=>'json'); |
|
517 | - */ |
|
518 | - public function getMIMEMap() |
|
519 | - { |
|
520 | - return array( |
|
521 | - static::$mime => static::$extension |
|
522 | - ); |
|
523 | - } |
|
524 | - |
|
525 | - /** |
|
526 | - * Set the selected MIME type |
|
527 | - * |
|
528 | - * @param string $mime MIME type |
|
529 | - */ |
|
530 | - public function setMIME($mime) |
|
531 | - { |
|
532 | - static::$mime = $mime; |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * Get selected MIME type |
|
537 | - */ |
|
538 | - public function getMIME() |
|
539 | - { |
|
540 | - return static::$mime; |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Get the selected file extension |
|
545 | - * |
|
546 | - * @return string file extension |
|
547 | - */ |
|
548 | - public function getExtension() |
|
549 | - { |
|
550 | - return static::$extension; |
|
551 | - } |
|
552 | - |
|
553 | - /** |
|
554 | - * Set the selected file extension |
|
555 | - * |
|
556 | - * @param string $extension file extension |
|
557 | - */ |
|
558 | - public function setExtension($extension) |
|
559 | - { |
|
560 | - static::$extension = $extension; |
|
561 | - } |
|
41 | + const BLADE = 'Illuminate\View\View'; |
|
42 | + const TWIG = 'Twig\Environment'; |
|
43 | + const MUSTACHE = 'Mustache_Engine'; |
|
44 | + |
|
45 | + public static $mime = 'text/html'; |
|
46 | + public static $extension = 'html'; |
|
47 | + public static $view; |
|
48 | + public static $errorView = 'debug.php'; |
|
49 | + public static $template = 'php'; |
|
50 | + public static $handleSession = true; |
|
51 | + public static $convertResponseToArray = false; |
|
52 | + |
|
53 | + public static $useSmartViews = true; |
|
54 | + /** |
|
55 | + * @var null|string defaults to template named folder in Defaults::$cacheDirectory |
|
56 | + */ |
|
57 | + public static $cacheDirectory = null; |
|
58 | + /** |
|
59 | + * @var array global key value pair to be supplied to the templates. All |
|
60 | + * keys added here will be available as a variable inside the template |
|
61 | + */ |
|
62 | + public static $data = array(); |
|
63 | + /** |
|
64 | + * @var string set it to the location of your the view files. Defaults to |
|
65 | + * views folder which is same level as vendor directory. |
|
66 | + */ |
|
67 | + public static $viewPath; |
|
68 | + /** |
|
69 | + * @var array template and its custom extension key value pair |
|
70 | + */ |
|
71 | + public static $customTemplateExtensions = array('blade' => 'blade.php'); |
|
72 | + /** |
|
73 | + * @var bool used internally for error handling |
|
74 | + */ |
|
75 | + protected static $parseViewMetadata = true; |
|
76 | + /** |
|
77 | + * @var Restler; |
|
78 | + */ |
|
79 | + public $restler; |
|
80 | + |
|
81 | + public function __construct() |
|
82 | + { |
|
83 | + //============ SESSION MANAGEMENT =============// |
|
84 | + if (static::$handleSession) { |
|
85 | + if (session_start() && isset($_SESSION['flash'])) { |
|
86 | + static::$data['flash'] = $_SESSION['flash']; |
|
87 | + unset($_SESSION['flash']); |
|
88 | + } |
|
89 | + } |
|
90 | + if (!static::$viewPath) { |
|
91 | + $array = explode('vendor', __DIR__, 2); |
|
92 | + static::$viewPath = $array[0] . 'views'; |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + public function getDependencyMap() |
|
97 | + { |
|
98 | + return array( |
|
99 | + self::BLADE => 'illuminate/view:^8', |
|
100 | + self::TWIG => 'twig/twig:^3', |
|
101 | + self::MUSTACHE => 'mustache/mustache:dev-master', |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + public static function blade(array $data, $debug = true) |
|
106 | + { |
|
107 | + $resolver = new EngineResolver(); |
|
108 | + $files = new Filesystem(); |
|
109 | + $compiler = new BladeCompiler($files, static::$cacheDirectory); |
|
110 | + $engine = new CompilerEngine($compiler); |
|
111 | + $resolver->register('blade', function () use ($engine) { |
|
112 | + return $engine; |
|
113 | + }); |
|
114 | + $phpEngine = new PhpEngine($files); |
|
115 | + $resolver->register('php', function () use ($phpEngine) { |
|
116 | + return $phpEngine; |
|
117 | + }); |
|
118 | + |
|
119 | + /** @var Restler $restler */ |
|
120 | + $restler = Scope::get('Restler'); |
|
121 | + |
|
122 | + //Lets expose shortcuts for our classes |
|
123 | + spl_autoload_register(function ($className) use ($restler) { |
|
124 | + if (isset($restler->apiMethodInfo->metadata['scope'][$className])) { |
|
125 | + return class_alias($restler->apiMethodInfo->metadata['scope'][$className], $className); |
|
126 | + } |
|
127 | + if (isset(Scope::$classAliases[$className])) { |
|
128 | + return class_alias(Scope::$classAliases[$className], $className); |
|
129 | + } |
|
130 | + return false; |
|
131 | + }, true, true); |
|
132 | + |
|
133 | + $viewFinder = new FileViewFinder($files, array(static::$viewPath)); |
|
134 | + $factory = new Factory($resolver, $viewFinder, new Dispatcher()); |
|
135 | + $path = $viewFinder->find(self::$view); |
|
136 | + $view = new View($factory, $engine, self::$view, $path, $data); |
|
137 | + $factory->callCreator($view); |
|
138 | + return $view->render(); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param array|object $data |
|
143 | + * @param bool $debug |
|
144 | + * |
|
145 | + * @return string |
|
146 | + * @throws \Twig\Error\LoaderError |
|
147 | + * @throws \Twig\Error\RuntimeError |
|
148 | + * @throws \Twig\Error\SyntaxError |
|
149 | + */ |
|
150 | + public static function twig($data, $debug = true) |
|
151 | + { |
|
152 | + $loader = new FilesystemLoader(static::$viewPath); |
|
153 | + $twig = new Environment($loader, array( |
|
154 | + 'cache' => is_string(static::$cacheDirectory) |
|
155 | + ? static::$cacheDirectory : false, |
|
156 | + 'debug' => $debug, |
|
157 | + 'use_strict_variables' => $debug, |
|
158 | + )); |
|
159 | + if ($debug) { |
|
160 | + $twig->addExtension(new DebugExtension()); |
|
161 | + } |
|
162 | + |
|
163 | + $twig->addFunction( |
|
164 | + new TwigFunction( |
|
165 | + 'form', |
|
166 | + 'Luracast\Restler\UI\Forms::get', |
|
167 | + array('is_safe' => array('html')) |
|
168 | + ) |
|
169 | + ); |
|
170 | + $twig->addFunction( |
|
171 | + new TwigFunction( |
|
172 | + 'form_key', |
|
173 | + 'Luracast\Restler\UI\Forms::key' |
|
174 | + ) |
|
175 | + ); |
|
176 | + $twig->addFunction( |
|
177 | + new TwigFunction( |
|
178 | + 'nav', |
|
179 | + 'Luracast\Restler\UI\Nav::get' |
|
180 | + ) |
|
181 | + ); |
|
182 | + |
|
183 | + $twig->registerUndefinedFunctionCallback(function ($name) { |
|
184 | + if ( |
|
185 | + isset(HtmlFormat::$data[$name]) && |
|
186 | + is_callable(HtmlFormat::$data[$name]) |
|
187 | + ) { |
|
188 | + return new TwigFunction( |
|
189 | + $name, |
|
190 | + HtmlFormat::$data[$name] |
|
191 | + ); |
|
192 | + } |
|
193 | + return false; |
|
194 | + }); |
|
195 | + |
|
196 | + $template = $twig->load(static::getViewFile()); |
|
197 | + return $template->render((array)$data); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @param array|object $data |
|
202 | + * @param bool $debug |
|
203 | + * |
|
204 | + * @return string |
|
205 | + */ |
|
206 | + public static function handlebar($data, $debug = true) |
|
207 | + { |
|
208 | + return static::mustache($data, $debug); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @param array|object $data |
|
213 | + * @param bool $debug |
|
214 | + * |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + public static function mustache($data, $debug = true) |
|
218 | + { |
|
219 | + $data = (array)$data; |
|
220 | + if (!isset($data['nav'])) { |
|
221 | + $data['nav'] = array_values(Nav::get()); |
|
222 | + } |
|
223 | + $options = array( |
|
224 | + 'loader' => new \Mustache_Loader_FilesystemLoader( |
|
225 | + static::$viewPath, |
|
226 | + array('extension' => static::getViewExtension()) |
|
227 | + ), |
|
228 | + 'helpers' => array( |
|
229 | + 'form' => function ($text, \Mustache_LambdaHelper $m) { |
|
230 | + $params = explode(',', $m->render($text)); |
|
231 | + return call_user_func_array( |
|
232 | + 'Luracast\Restler\UI\Forms::get', |
|
233 | + $params |
|
234 | + ); |
|
235 | + }, |
|
236 | + ) |
|
237 | + ); |
|
238 | + if (!$debug) { |
|
239 | + $options['cache'] = static::$cacheDirectory; |
|
240 | + } |
|
241 | + $m = new \Mustache_Engine($options); |
|
242 | + return $m->render(static::getViewFile(), $data); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @param array|object $data |
|
247 | + * @param bool $debug |
|
248 | + * |
|
249 | + * @return string |
|
250 | + * @throws RestException |
|
251 | + */ |
|
252 | + public static function php($data, $debug = true) |
|
253 | + { |
|
254 | + if (static::$view == 'debug') { |
|
255 | + static::$viewPath = dirname(__DIR__) . '/views'; |
|
256 | + } |
|
257 | + $view = static::getViewFile(true); |
|
258 | + |
|
259 | + if (!is_readable($view)) { |
|
260 | + throw new RestException( |
|
261 | + 500, |
|
262 | + "view file `$view` is not readable. " . |
|
263 | + 'Check for file presence and file permissions' |
|
264 | + ); |
|
265 | + } |
|
266 | + |
|
267 | + $path = static::$viewPath . DIRECTORY_SEPARATOR; |
|
268 | + $template = function ($view) use ($data, $path) { |
|
269 | + $data = (array)$data; |
|
270 | + $form = function () { |
|
271 | + return call_user_func_array( |
|
272 | + 'Luracast\Restler\UI\Forms::get', |
|
273 | + func_get_args() |
|
274 | + ); |
|
275 | + }; |
|
276 | + if (!isset($data['form'])) { |
|
277 | + $data['form'] = $form; |
|
278 | + } |
|
279 | + $nav = function () { |
|
280 | + return call_user_func_array( |
|
281 | + 'Luracast\Restler\UI\Nav::get', |
|
282 | + func_get_args() |
|
283 | + ); |
|
284 | + }; |
|
285 | + if (!isset($data['nav'])) { |
|
286 | + $data['nav'] = $nav; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + $_ = function () use ($data, $path) { |
|
291 | + extract($data); |
|
292 | + $args = func_get_args(); |
|
293 | + $task = array_shift($args); |
|
294 | + switch ($task) { |
|
295 | + case 'require': |
|
296 | + case 'include': |
|
297 | + $file = $path . $args[0]; |
|
298 | + if (is_readable($file)) { |
|
299 | + if ( |
|
300 | + isset($args[1]) && |
|
301 | + ($arrays = Util::nestedValue($data, $args[1])) |
|
302 | + ) { |
|
303 | + $str = ''; |
|
304 | + foreach ($arrays as $arr) { |
|
305 | + extract((array)$arr); |
|
306 | + $str .= include $file; |
|
307 | + } |
|
308 | + return $str; |
|
309 | + } else { |
|
310 | + return include $file; |
|
311 | + } |
|
312 | + } |
|
313 | + break; |
|
314 | + case 'if': |
|
315 | + if (count($args) < 2) { |
|
316 | + $args[1] = ''; |
|
317 | + } |
|
318 | + if (count($args) < 3) { |
|
319 | + $args[2] = ''; |
|
320 | + } |
|
321 | + return $args[0] ? $args[1] : $args[2]; |
|
322 | + break; |
|
323 | + default: |
|
324 | + if (isset($data[$task]) && is_callable($data[$task])) { |
|
325 | + return call_user_func_array($data[$task], $args); |
|
326 | + } |
|
327 | + } |
|
328 | + return ''; |
|
329 | + }; |
|
330 | + extract($data); |
|
331 | + return @include $view; |
|
332 | + }; |
|
333 | + $value = $template($view); |
|
334 | + if (is_string($value)) { |
|
335 | + return $value; |
|
336 | + } |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Encode the given data in the format |
|
341 | + * |
|
342 | + * @param array $data resulting data that needs to |
|
343 | + * be encoded in the given format |
|
344 | + * @param boolean $humanReadable set to TRUE when restler |
|
345 | + * is not running in production mode. |
|
346 | + * Formatter has to make the encoded |
|
347 | + * output more human readable |
|
348 | + * |
|
349 | + * @return string encoded string |
|
350 | + * @throws \Exception |
|
351 | + */ |
|
352 | + public function encode($data, $humanReadable = false) |
|
353 | + { |
|
354 | + if (!is_readable(static::$viewPath)) { |
|
355 | + throw new \Exception( |
|
356 | + 'The views directory `' |
|
357 | + . self::$viewPath . '` should exist with read permission.' |
|
358 | + ); |
|
359 | + } |
|
360 | + static::$data['basePath'] = dirname($_SERVER['SCRIPT_NAME']); |
|
361 | + static::$data['baseUrl'] = $this->restler->getBaseUrl(); |
|
362 | + static::$data['currentPath'] = $this->restler->url; |
|
363 | + |
|
364 | + try { |
|
365 | + $exception = $this->restler->exception; |
|
366 | + $success = is_null($exception); |
|
367 | + $error = $success ? null : $exception->getMessage(); |
|
368 | + $data = array( |
|
369 | + 'response' => static::$convertResponseToArray |
|
370 | + ? Obj::toArray($data) |
|
371 | + : $data, |
|
372 | + 'stages' => $this->restler->getEvents(), |
|
373 | + 'success' => $success, |
|
374 | + 'error' => $error |
|
375 | + ); |
|
376 | + $info = $data['api'] = $this->restler->apiMethodInfo; |
|
377 | + $metadata = Util::nestedValue( |
|
378 | + $this->restler, 'apiMethodInfo', 'metadata' |
|
379 | + ); |
|
380 | + $view = $success ? 'view' : 'errorView'; |
|
381 | + $value = false; |
|
382 | + if (static::$parseViewMetadata && isset($metadata[$view])) { |
|
383 | + if (is_array($metadata[$view])) { |
|
384 | + self::$view = $metadata[$view]['description']; |
|
385 | + $value = Util::nestedValue( |
|
386 | + $metadata[$view], 'properties', 'value' |
|
387 | + ); |
|
388 | + } else { |
|
389 | + self::$view = $metadata[$view]; |
|
390 | + } |
|
391 | + } elseif (!self::$view) { |
|
392 | + self::$view = static::guessViewName($this->restler->url); |
|
393 | + } |
|
394 | + if ( |
|
395 | + isset($metadata['param']) |
|
396 | + && (!$value || 0 === strpos($value, 'request')) |
|
397 | + ) { |
|
398 | + $params = $metadata['param']; |
|
399 | + foreach ($params as $index => &$param) { |
|
400 | + $index = intval($index); |
|
401 | + if (is_numeric($index)) { |
|
402 | + $param['value'] = $this |
|
403 | + ->restler |
|
404 | + ->apiMethodInfo |
|
405 | + ->parameters[$index]; |
|
406 | + } |
|
407 | + } |
|
408 | + $data['request']['parameters'] = $params; |
|
409 | + } |
|
410 | + if ($value) { |
|
411 | + $data = Util::nestedValue($data, explode('.', $value)); |
|
412 | + } |
|
413 | + if (is_array($data)) { |
|
414 | + $data += static::$data; |
|
415 | + } |
|
416 | + if (false === ($i = strrpos(self::$view, '.'))) { |
|
417 | + $template = self::$template; |
|
418 | + } else { |
|
419 | + self::$template = $template = substr(self::$view, $i + 1); |
|
420 | + self::$view = substr(self::$view, 0, $i); |
|
421 | + } |
|
422 | + if (!static::$cacheDirectory) { |
|
423 | + static::$cacheDirectory = Defaults::$cacheDirectory . DIRECTORY_SEPARATOR . $template; |
|
424 | + if (!file_exists(static::$cacheDirectory)) { |
|
425 | + if (!mkdir(static::$cacheDirectory, 0770, true)) { |
|
426 | + throw new RestException(500, |
|
427 | + 'Unable to create cache directory `' . static::$cacheDirectory . '`'); |
|
428 | + } |
|
429 | + } |
|
430 | + } |
|
431 | + if (method_exists($class = get_called_class(), $template)) { |
|
432 | + if ($template == 'blade') { |
|
433 | + $this->checkDependency(self::BLADE); |
|
434 | + } elseif ($template == 'twig') { |
|
435 | + $this->checkDependency(self::TWIG); |
|
436 | + } elseif ($template == 'mustache' || $template == 'handlebar') { |
|
437 | + $this->checkDependency(self::MUSTACHE); |
|
438 | + } |
|
439 | + return call_user_func("$class::$template", $data, $humanReadable); |
|
440 | + } |
|
441 | + throw new RestException(500, "Unsupported template system `$template`"); |
|
442 | + } catch (Exception $e) { |
|
443 | + static::$parseViewMetadata = false; |
|
444 | + $this->reset(); |
|
445 | + throw $e; |
|
446 | + } |
|
447 | + } |
|
448 | + |
|
449 | + public static function guessViewName($path) |
|
450 | + { |
|
451 | + if (empty($path)) { |
|
452 | + $path = 'index'; |
|
453 | + } elseif (strpos($path, '/')) { |
|
454 | + $path .= '/index'; |
|
455 | + } |
|
456 | + $file = static::$viewPath . '/' . $path . '.' . static::getViewExtension(); |
|
457 | + |
|
458 | + return static::$useSmartViews && is_readable($file) |
|
459 | + ? $path |
|
460 | + : static::$errorView; |
|
461 | + } |
|
462 | + |
|
463 | + public static function getViewExtension() |
|
464 | + { |
|
465 | + return isset(static::$customTemplateExtensions[static::$template]) |
|
466 | + ? static::$customTemplateExtensions[static::$template] |
|
467 | + : static::$template; |
|
468 | + } |
|
469 | + |
|
470 | + public static function getViewFile($fullPath = false, $includeExtension = true) |
|
471 | + { |
|
472 | + $v = $fullPath ? static::$viewPath . '/' : ''; |
|
473 | + $v .= static::$view; |
|
474 | + if ($includeExtension) { |
|
475 | + $v .= '.' . static::getViewExtension(); |
|
476 | + } |
|
477 | + return $v; |
|
478 | + } |
|
479 | + |
|
480 | + private function reset() |
|
481 | + { |
|
482 | + static::$mime = 'text/html'; |
|
483 | + static::$extension = 'html'; |
|
484 | + static::$view = 'debug'; |
|
485 | + static::$template = 'php'; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * Decode the given data from the format |
|
490 | + * |
|
491 | + * @param string $data |
|
492 | + * data sent from client to |
|
493 | + * the api in the given format. |
|
494 | + * |
|
495 | + * @return array associative array of the parsed data |
|
496 | + * |
|
497 | + * @throws RestException |
|
498 | + */ |
|
499 | + public function decode($data) |
|
500 | + { |
|
501 | + throw new RestException(500, 'HtmlFormat is write only'); |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * @return bool false as HTML format is write only |
|
506 | + */ |
|
507 | + public function isReadable() |
|
508 | + { |
|
509 | + return false; |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * Get MIME type => Extension mappings as an associative array |
|
514 | + * |
|
515 | + * @return array list of mime strings for the format |
|
516 | + * @example array('application/json'=>'json'); |
|
517 | + */ |
|
518 | + public function getMIMEMap() |
|
519 | + { |
|
520 | + return array( |
|
521 | + static::$mime => static::$extension |
|
522 | + ); |
|
523 | + } |
|
524 | + |
|
525 | + /** |
|
526 | + * Set the selected MIME type |
|
527 | + * |
|
528 | + * @param string $mime MIME type |
|
529 | + */ |
|
530 | + public function setMIME($mime) |
|
531 | + { |
|
532 | + static::$mime = $mime; |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * Get selected MIME type |
|
537 | + */ |
|
538 | + public function getMIME() |
|
539 | + { |
|
540 | + return static::$mime; |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Get the selected file extension |
|
545 | + * |
|
546 | + * @return string file extension |
|
547 | + */ |
|
548 | + public function getExtension() |
|
549 | + { |
|
550 | + return static::$extension; |
|
551 | + } |
|
552 | + |
|
553 | + /** |
|
554 | + * Set the selected file extension |
|
555 | + * |
|
556 | + * @param string $extension file extension |
|
557 | + */ |
|
558 | + public function setExtension($extension) |
|
559 | + { |
|
560 | + static::$extension = $extension; |
|
561 | + } |
|
562 | 562 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | } |
90 | 90 | if (!static::$viewPath) { |
91 | 91 | $array = explode('vendor', __DIR__, 2); |
92 | - static::$viewPath = $array[0] . 'views'; |
|
92 | + static::$viewPath = $array[0].'views'; |
|
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
@@ -108,11 +108,11 @@ discard block |
||
108 | 108 | $files = new Filesystem(); |
109 | 109 | $compiler = new BladeCompiler($files, static::$cacheDirectory); |
110 | 110 | $engine = new CompilerEngine($compiler); |
111 | - $resolver->register('blade', function () use ($engine) { |
|
111 | + $resolver->register('blade', function() use ($engine) { |
|
112 | 112 | return $engine; |
113 | 113 | }); |
114 | 114 | $phpEngine = new PhpEngine($files); |
115 | - $resolver->register('php', function () use ($phpEngine) { |
|
115 | + $resolver->register('php', function() use ($phpEngine) { |
|
116 | 116 | return $phpEngine; |
117 | 117 | }); |
118 | 118 | |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | $restler = Scope::get('Restler'); |
121 | 121 | |
122 | 122 | //Lets expose shortcuts for our classes |
123 | - spl_autoload_register(function ($className) use ($restler) { |
|
123 | + spl_autoload_register(function($className) use ($restler) { |
|
124 | 124 | if (isset($restler->apiMethodInfo->metadata['scope'][$className])) { |
125 | 125 | return class_alias($restler->apiMethodInfo->metadata['scope'][$className], $className); |
126 | 126 | } |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | ) |
181 | 181 | ); |
182 | 182 | |
183 | - $twig->registerUndefinedFunctionCallback(function ($name) { |
|
183 | + $twig->registerUndefinedFunctionCallback(function($name) { |
|
184 | 184 | if ( |
185 | 185 | isset(HtmlFormat::$data[$name]) && |
186 | 186 | is_callable(HtmlFormat::$data[$name]) |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | }); |
195 | 195 | |
196 | 196 | $template = $twig->load(static::getViewFile()); |
197 | - return $template->render((array)$data); |
|
197 | + return $template->render((array) $data); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | /** |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | */ |
217 | 217 | public static function mustache($data, $debug = true) |
218 | 218 | { |
219 | - $data = (array)$data; |
|
219 | + $data = (array) $data; |
|
220 | 220 | if (!isset($data['nav'])) { |
221 | 221 | $data['nav'] = array_values(Nav::get()); |
222 | 222 | } |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | array('extension' => static::getViewExtension()) |
227 | 227 | ), |
228 | 228 | 'helpers' => array( |
229 | - 'form' => function ($text, \Mustache_LambdaHelper $m) { |
|
229 | + 'form' => function($text, \Mustache_LambdaHelper $m) { |
|
230 | 230 | $params = explode(',', $m->render($text)); |
231 | 231 | return call_user_func_array( |
232 | 232 | 'Luracast\Restler\UI\Forms::get', |
@@ -252,22 +252,22 @@ discard block |
||
252 | 252 | public static function php($data, $debug = true) |
253 | 253 | { |
254 | 254 | if (static::$view == 'debug') { |
255 | - static::$viewPath = dirname(__DIR__) . '/views'; |
|
255 | + static::$viewPath = dirname(__DIR__).'/views'; |
|
256 | 256 | } |
257 | 257 | $view = static::getViewFile(true); |
258 | 258 | |
259 | 259 | if (!is_readable($view)) { |
260 | 260 | throw new RestException( |
261 | 261 | 500, |
262 | - "view file `$view` is not readable. " . |
|
262 | + "view file `$view` is not readable. ". |
|
263 | 263 | 'Check for file presence and file permissions' |
264 | 264 | ); |
265 | 265 | } |
266 | 266 | |
267 | - $path = static::$viewPath . DIRECTORY_SEPARATOR; |
|
268 | - $template = function ($view) use ($data, $path) { |
|
269 | - $data = (array)$data; |
|
270 | - $form = function () { |
|
267 | + $path = static::$viewPath.DIRECTORY_SEPARATOR; |
|
268 | + $template = function($view) use ($data, $path) { |
|
269 | + $data = (array) $data; |
|
270 | + $form = function() { |
|
271 | 271 | return call_user_func_array( |
272 | 272 | 'Luracast\Restler\UI\Forms::get', |
273 | 273 | func_get_args() |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | if (!isset($data['form'])) { |
277 | 277 | $data['form'] = $form; |
278 | 278 | } |
279 | - $nav = function () { |
|
279 | + $nav = function() { |
|
280 | 280 | return call_user_func_array( |
281 | 281 | 'Luracast\Restler\UI\Nav::get', |
282 | 282 | func_get_args() |
@@ -287,14 +287,14 @@ discard block |
||
287 | 287 | } |
288 | 288 | |
289 | 289 | |
290 | - $_ = function () use ($data, $path) { |
|
290 | + $_ = function() use ($data, $path) { |
|
291 | 291 | extract($data); |
292 | 292 | $args = func_get_args(); |
293 | 293 | $task = array_shift($args); |
294 | 294 | switch ($task) { |
295 | 295 | case 'require': |
296 | 296 | case 'include': |
297 | - $file = $path . $args[0]; |
|
297 | + $file = $path.$args[0]; |
|
298 | 298 | if (is_readable($file)) { |
299 | 299 | if ( |
300 | 300 | isset($args[1]) && |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | ) { |
303 | 303 | $str = ''; |
304 | 304 | foreach ($arrays as $arr) { |
305 | - extract((array)$arr); |
|
305 | + extract((array) $arr); |
|
306 | 306 | $str .= include $file; |
307 | 307 | } |
308 | 308 | return $str; |
@@ -354,7 +354,7 @@ discard block |
||
354 | 354 | if (!is_readable(static::$viewPath)) { |
355 | 355 | throw new \Exception( |
356 | 356 | 'The views directory `' |
357 | - . self::$viewPath . '` should exist with read permission.' |
|
357 | + . self::$viewPath.'` should exist with read permission.' |
|
358 | 358 | ); |
359 | 359 | } |
360 | 360 | static::$data['basePath'] = dirname($_SERVER['SCRIPT_NAME']); |
@@ -420,11 +420,11 @@ discard block |
||
420 | 420 | self::$view = substr(self::$view, 0, $i); |
421 | 421 | } |
422 | 422 | if (!static::$cacheDirectory) { |
423 | - static::$cacheDirectory = Defaults::$cacheDirectory . DIRECTORY_SEPARATOR . $template; |
|
423 | + static::$cacheDirectory = Defaults::$cacheDirectory.DIRECTORY_SEPARATOR.$template; |
|
424 | 424 | if (!file_exists(static::$cacheDirectory)) { |
425 | 425 | if (!mkdir(static::$cacheDirectory, 0770, true)) { |
426 | 426 | throw new RestException(500, |
427 | - 'Unable to create cache directory `' . static::$cacheDirectory . '`'); |
|
427 | + 'Unable to create cache directory `'.static::$cacheDirectory.'`'); |
|
428 | 428 | } |
429 | 429 | } |
430 | 430 | } |
@@ -453,7 +453,7 @@ discard block |
||
453 | 453 | } elseif (strpos($path, '/')) { |
454 | 454 | $path .= '/index'; |
455 | 455 | } |
456 | - $file = static::$viewPath . '/' . $path . '.' . static::getViewExtension(); |
|
456 | + $file = static::$viewPath.'/'.$path.'.'.static::getViewExtension(); |
|
457 | 457 | |
458 | 458 | return static::$useSmartViews && is_readable($file) |
459 | 459 | ? $path |
@@ -469,10 +469,10 @@ discard block |
||
469 | 469 | |
470 | 470 | public static function getViewFile($fullPath = false, $includeExtension = true) |
471 | 471 | { |
472 | - $v = $fullPath ? static::$viewPath . '/' : ''; |
|
472 | + $v = $fullPath ? static::$viewPath.'/' : ''; |
|
473 | 473 | $v .= static::$view; |
474 | 474 | if ($includeExtension) { |
475 | - $v .= '.' . static::getViewExtension(); |
|
475 | + $v .= '.'.static::getViewExtension(); |
|
476 | 476 | } |
477 | 477 | return $v; |
478 | 478 | } |
@@ -18,20 +18,20 @@ |
||
18 | 18 | */ |
19 | 19 | class YamlFormat extends DependentFormat |
20 | 20 | { |
21 | - const MIME = 'text/plain'; |
|
22 | - const EXTENSION = 'yaml'; |
|
21 | + const MIME = 'text/plain'; |
|
22 | + const EXTENSION = 'yaml'; |
|
23 | 23 | |
24 | - const PACKAGE_NAME = 'symfony/yaml:*'; |
|
25 | - const EXTERNAL_CLASS = 'Symfony\Component\Yaml\Yaml'; |
|
24 | + const PACKAGE_NAME = 'symfony/yaml:*'; |
|
25 | + const EXTERNAL_CLASS = 'Symfony\Component\Yaml\Yaml'; |
|
26 | 26 | |
27 | - public function encode($data, $humanReadable = false) |
|
28 | - { |
|
29 | - return @Yaml::dump(Obj::toArray($data), $humanReadable ? 10 : 4); |
|
30 | - } |
|
27 | + public function encode($data, $humanReadable = false) |
|
28 | + { |
|
29 | + return @Yaml::dump(Obj::toArray($data), $humanReadable ? 10 : 4); |
|
30 | + } |
|
31 | 31 | |
32 | - public function decode($data) |
|
33 | - { |
|
34 | - return Yaml::parse($data); |
|
35 | - } |
|
32 | + public function decode($data) |
|
33 | + { |
|
34 | + return Yaml::parse($data); |
|
35 | + } |
|
36 | 36 | } |
37 | 37 |
@@ -17,131 +17,131 @@ |
||
17 | 17 | */ |
18 | 18 | class UploadFormat extends Format |
19 | 19 | { |
20 | - const MIME = 'multipart/form-data'; |
|
21 | - const EXTENSION = 'post'; |
|
22 | - public static $errors = array( |
|
23 | - 0 => false, |
|
24 | - 1 => "The uploaded file exceeds the maximum allowed size", |
|
25 | - 2 => "The uploaded file exceeds the maximum allowed size", |
|
26 | - 3 => "The uploaded file was only partially uploaded", |
|
27 | - 4 => "No file was uploaded", |
|
28 | - 6 => "Missing a temporary folder", |
|
29 | - 7 => "Failed to write file to disk", |
|
30 | - 8 => "A PHP extension stopped the file upload" |
|
31 | - ); |
|
32 | - /** |
|
33 | - * use it if you need to restrict uploads based on file type |
|
34 | - * setting it as an empty array allows all file types |
|
35 | - * default is to allow only png and jpeg images |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public static $allowedMimeTypes = array('image/jpeg', 'image/png'); |
|
40 | - /** |
|
41 | - * use it to restrict uploads based on file size |
|
42 | - * set it to 0 to allow all sizes |
|
43 | - * please note that it upload restrictions in the server |
|
44 | - * takes precedence so it has to be lower than or equal to that |
|
45 | - * default value is 1MB (1024x1024)bytes |
|
46 | - * usual value for the server is 8388608 |
|
47 | - * |
|
48 | - * @var int |
|
49 | - */ |
|
50 | - public static $maximumFileSize = 1048576; |
|
51 | - /** |
|
52 | - * Your own validation function for validating each uploaded file |
|
53 | - * it can return false or throw an exception for invalid file |
|
54 | - * use anonymous function / closure in PHP 5.3 and above |
|
55 | - * use function name in other cases |
|
56 | - * |
|
57 | - * @var Callable |
|
58 | - */ |
|
59 | - public static $customValidationFunction; |
|
60 | - /** |
|
61 | - * Since exceptions are triggered way before at the `get` stage |
|
62 | - * |
|
63 | - * @var bool |
|
64 | - */ |
|
65 | - public static $suppressExceptionsAsError = false; |
|
20 | + const MIME = 'multipart/form-data'; |
|
21 | + const EXTENSION = 'post'; |
|
22 | + public static $errors = array( |
|
23 | + 0 => false, |
|
24 | + 1 => "The uploaded file exceeds the maximum allowed size", |
|
25 | + 2 => "The uploaded file exceeds the maximum allowed size", |
|
26 | + 3 => "The uploaded file was only partially uploaded", |
|
27 | + 4 => "No file was uploaded", |
|
28 | + 6 => "Missing a temporary folder", |
|
29 | + 7 => "Failed to write file to disk", |
|
30 | + 8 => "A PHP extension stopped the file upload" |
|
31 | + ); |
|
32 | + /** |
|
33 | + * use it if you need to restrict uploads based on file type |
|
34 | + * setting it as an empty array allows all file types |
|
35 | + * default is to allow only png and jpeg images |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public static $allowedMimeTypes = array('image/jpeg', 'image/png'); |
|
40 | + /** |
|
41 | + * use it to restrict uploads based on file size |
|
42 | + * set it to 0 to allow all sizes |
|
43 | + * please note that it upload restrictions in the server |
|
44 | + * takes precedence so it has to be lower than or equal to that |
|
45 | + * default value is 1MB (1024x1024)bytes |
|
46 | + * usual value for the server is 8388608 |
|
47 | + * |
|
48 | + * @var int |
|
49 | + */ |
|
50 | + public static $maximumFileSize = 1048576; |
|
51 | + /** |
|
52 | + * Your own validation function for validating each uploaded file |
|
53 | + * it can return false or throw an exception for invalid file |
|
54 | + * use anonymous function / closure in PHP 5.3 and above |
|
55 | + * use function name in other cases |
|
56 | + * |
|
57 | + * @var Callable |
|
58 | + */ |
|
59 | + public static $customValidationFunction; |
|
60 | + /** |
|
61 | + * Since exceptions are triggered way before at the `get` stage |
|
62 | + * |
|
63 | + * @var bool |
|
64 | + */ |
|
65 | + public static $suppressExceptionsAsError = false; |
|
66 | 66 | |
67 | - protected static function checkFile(& $file, $doMimeCheck = false, $doSizeCheck = false) |
|
68 | - { |
|
69 | - try { |
|
70 | - if ($file['error']) { |
|
71 | - //server is throwing an error |
|
72 | - //assume that the error is due to maximum size limit |
|
73 | - throw new RestException($file['error'] > 5 ? 500 : 413, static::$errors[$file['error']]); |
|
74 | - } |
|
75 | - $typeElements = explode('/', $file['type']); |
|
76 | - $genericType = $typeElements[0].'/*'; |
|
77 | - if ( |
|
78 | - $doMimeCheck |
|
79 | - && !( |
|
80 | - in_array($file['type'], self::$allowedMimeTypes) |
|
81 | - || in_array($genericType, self::$allowedMimeTypes) |
|
82 | - ) |
|
83 | - ) { |
|
84 | - throw new RestException(403, "File type ({$file['type']}) is not supported."); |
|
85 | - } |
|
86 | - if ($doSizeCheck && $file['size'] > self::$maximumFileSize) { |
|
87 | - throw new RestException(413, "Uploaded file ({$file['name']}) is too big."); |
|
88 | - } |
|
89 | - if (self::$customValidationFunction) { |
|
90 | - if (!call_user_func(self::$customValidationFunction, $file)) { |
|
91 | - throw new RestException(403, "File ({$file['name']}) is not supported."); |
|
92 | - } |
|
93 | - } |
|
94 | - } catch (RestException $e) { |
|
95 | - if (static::$suppressExceptionsAsError) { |
|
96 | - $file['error'] = $e->getCode() == 413 ? 1 : 6; |
|
97 | - $file['exception'] = $e; |
|
98 | - } else { |
|
99 | - throw $e; |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
67 | + protected static function checkFile(& $file, $doMimeCheck = false, $doSizeCheck = false) |
|
68 | + { |
|
69 | + try { |
|
70 | + if ($file['error']) { |
|
71 | + //server is throwing an error |
|
72 | + //assume that the error is due to maximum size limit |
|
73 | + throw new RestException($file['error'] > 5 ? 500 : 413, static::$errors[$file['error']]); |
|
74 | + } |
|
75 | + $typeElements = explode('/', $file['type']); |
|
76 | + $genericType = $typeElements[0].'/*'; |
|
77 | + if ( |
|
78 | + $doMimeCheck |
|
79 | + && !( |
|
80 | + in_array($file['type'], self::$allowedMimeTypes) |
|
81 | + || in_array($genericType, self::$allowedMimeTypes) |
|
82 | + ) |
|
83 | + ) { |
|
84 | + throw new RestException(403, "File type ({$file['type']}) is not supported."); |
|
85 | + } |
|
86 | + if ($doSizeCheck && $file['size'] > self::$maximumFileSize) { |
|
87 | + throw new RestException(413, "Uploaded file ({$file['name']}) is too big."); |
|
88 | + } |
|
89 | + if (self::$customValidationFunction) { |
|
90 | + if (!call_user_func(self::$customValidationFunction, $file)) { |
|
91 | + throw new RestException(403, "File ({$file['name']}) is not supported."); |
|
92 | + } |
|
93 | + } |
|
94 | + } catch (RestException $e) { |
|
95 | + if (static::$suppressExceptionsAsError) { |
|
96 | + $file['error'] = $e->getCode() == 413 ? 1 : 6; |
|
97 | + $file['exception'] = $e; |
|
98 | + } else { |
|
99 | + throw $e; |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - public function encode($data, $humanReadable = false) |
|
105 | - { |
|
106 | - throw new RestException(500, 'UploadFormat is read only'); |
|
107 | - } |
|
104 | + public function encode($data, $humanReadable = false) |
|
105 | + { |
|
106 | + throw new RestException(500, 'UploadFormat is read only'); |
|
107 | + } |
|
108 | 108 | |
109 | - public function decode($data) |
|
110 | - { |
|
111 | - $doMimeCheck = !empty(self::$allowedMimeTypes); |
|
112 | - $doSizeCheck = self::$maximumFileSize ? TRUE : FALSE; |
|
113 | - //validate |
|
114 | - foreach ($_FILES as & $file) { |
|
115 | - if (is_array($file['error'])) { |
|
116 | - foreach ($file['error'] as $i => $error) { |
|
117 | - $innerFile = array(); |
|
118 | - foreach ($file as $property => $value) { |
|
119 | - $innerFile[$property] = $value[$i]; |
|
120 | - } |
|
121 | - if ($innerFile['name']) |
|
122 | - static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
109 | + public function decode($data) |
|
110 | + { |
|
111 | + $doMimeCheck = !empty(self::$allowedMimeTypes); |
|
112 | + $doSizeCheck = self::$maximumFileSize ? TRUE : FALSE; |
|
113 | + //validate |
|
114 | + foreach ($_FILES as & $file) { |
|
115 | + if (is_array($file['error'])) { |
|
116 | + foreach ($file['error'] as $i => $error) { |
|
117 | + $innerFile = array(); |
|
118 | + foreach ($file as $property => $value) { |
|
119 | + $innerFile[$property] = $value[$i]; |
|
120 | + } |
|
121 | + if ($innerFile['name']) |
|
122 | + static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
123 | 123 | |
124 | - if (isset($innerFile['exception'])) { |
|
125 | - $file['error'][$i] = $innerFile['error']; |
|
126 | - $file['exception'] = $innerFile['exception']; |
|
127 | - break; |
|
128 | - } |
|
129 | - } |
|
130 | - } else { |
|
131 | - if ($file['name']) |
|
132 | - static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
133 | - if (isset($innerFile['exception'])) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - //sort file order if needed; |
|
139 | - return UrlEncodedFormat::decoderTypeFix($_FILES + $_POST); |
|
140 | - } |
|
124 | + if (isset($innerFile['exception'])) { |
|
125 | + $file['error'][$i] = $innerFile['error']; |
|
126 | + $file['exception'] = $innerFile['exception']; |
|
127 | + break; |
|
128 | + } |
|
129 | + } |
|
130 | + } else { |
|
131 | + if ($file['name']) |
|
132 | + static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
133 | + if (isset($innerFile['exception'])) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + //sort file order if needed; |
|
139 | + return UrlEncodedFormat::decoderTypeFix($_FILES + $_POST); |
|
140 | + } |
|
141 | 141 | |
142 | - function isWritable() |
|
143 | - { |
|
144 | - return false; |
|
145 | - } |
|
142 | + function isWritable() |
|
143 | + { |
|
144 | + return false; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | } |
@@ -118,8 +118,9 @@ discard block |
||
118 | 118 | foreach ($file as $property => $value) { |
119 | 119 | $innerFile[$property] = $value[$i]; |
120 | 120 | } |
121 | - if ($innerFile['name']) |
|
122 | - static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
121 | + if ($innerFile['name']) { |
|
122 | + static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
123 | + } |
|
123 | 124 | |
124 | 125 | if (isset($innerFile['exception'])) { |
125 | 126 | $file['error'][$i] = $innerFile['error']; |
@@ -128,8 +129,9 @@ discard block |
||
128 | 129 | } |
129 | 130 | } |
130 | 131 | } else { |
131 | - if ($file['name']) |
|
132 | - static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
132 | + if ($file['name']) { |
|
133 | + static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
134 | + } |
|
133 | 135 | if (isset($innerFile['exception'])) { |
134 | 136 | break; |
135 | 137 | } |
@@ -15,95 +15,95 @@ |
||
15 | 15 | */ |
16 | 16 | interface iFormat |
17 | 17 | { |
18 | - /** |
|
19 | - * Get MIME type => Extension mappings as an associative array |
|
20 | - * |
|
21 | - * @return array list of mime strings for the format |
|
22 | - * @example array('application/json'=>'json'); |
|
23 | - */ |
|
24 | - public function getMIMEMap(); |
|
18 | + /** |
|
19 | + * Get MIME type => Extension mappings as an associative array |
|
20 | + * |
|
21 | + * @return array list of mime strings for the format |
|
22 | + * @example array('application/json'=>'json'); |
|
23 | + */ |
|
24 | + public function getMIMEMap(); |
|
25 | 25 | |
26 | - /** |
|
27 | - * Set the selected MIME type |
|
28 | - * |
|
29 | - * @param string $mime |
|
30 | - * MIME type |
|
31 | - */ |
|
32 | - public function setMIME($mime); |
|
26 | + /** |
|
27 | + * Set the selected MIME type |
|
28 | + * |
|
29 | + * @param string $mime |
|
30 | + * MIME type |
|
31 | + */ |
|
32 | + public function setMIME($mime); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Content-Type field of the HTTP header can send a charset |
|
36 | - * parameter in the HTTP header to specify the character |
|
37 | - * encoding of the document. |
|
38 | - * This information is passed |
|
39 | - * here so that Format class can encode data accordingly |
|
40 | - * Format class may choose to ignore this and use its |
|
41 | - * default character set. |
|
42 | - * |
|
43 | - * @param string $charset |
|
44 | - * Example utf-8 |
|
45 | - */ |
|
46 | - public function setCharset($charset); |
|
34 | + /** |
|
35 | + * Content-Type field of the HTTP header can send a charset |
|
36 | + * parameter in the HTTP header to specify the character |
|
37 | + * encoding of the document. |
|
38 | + * This information is passed |
|
39 | + * here so that Format class can encode data accordingly |
|
40 | + * Format class may choose to ignore this and use its |
|
41 | + * default character set. |
|
42 | + * |
|
43 | + * @param string $charset |
|
44 | + * Example utf-8 |
|
45 | + */ |
|
46 | + public function setCharset($charset); |
|
47 | 47 | |
48 | - /** |
|
49 | - * Content-Type accepted by the Format class |
|
50 | - * |
|
51 | - * @return string $charset Example utf-8 |
|
52 | - */ |
|
53 | - public function getCharset(); |
|
48 | + /** |
|
49 | + * Content-Type accepted by the Format class |
|
50 | + * |
|
51 | + * @return string $charset Example utf-8 |
|
52 | + */ |
|
53 | + public function getCharset(); |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get selected MIME type |
|
57 | - */ |
|
58 | - public function getMIME(); |
|
55 | + /** |
|
56 | + * Get selected MIME type |
|
57 | + */ |
|
58 | + public function getMIME(); |
|
59 | 59 | |
60 | - /** |
|
61 | - * Set the selected file extension |
|
62 | - * |
|
63 | - * @param string $extension |
|
64 | - * file extension |
|
65 | - */ |
|
66 | - public function setExtension($extension); |
|
60 | + /** |
|
61 | + * Set the selected file extension |
|
62 | + * |
|
63 | + * @param string $extension |
|
64 | + * file extension |
|
65 | + */ |
|
66 | + public function setExtension($extension); |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get the selected file extension |
|
70 | - * |
|
71 | - * @return string file extension |
|
72 | - */ |
|
73 | - public function getExtension(); |
|
68 | + /** |
|
69 | + * Get the selected file extension |
|
70 | + * |
|
71 | + * @return string file extension |
|
72 | + */ |
|
73 | + public function getExtension(); |
|
74 | 74 | |
75 | - /** |
|
76 | - * Encode the given data in the format |
|
77 | - * |
|
78 | - * @param array $data |
|
79 | - * resulting data that needs to |
|
80 | - * be encoded in the given format |
|
81 | - * @param boolean $humanReadable |
|
82 | - * set to TRUE when restler |
|
83 | - * is not running in production mode. Formatter has to |
|
84 | - * make the encoded output more human readable |
|
85 | - * @return string encoded string |
|
86 | - */ |
|
87 | - public function encode($data, $humanReadable = false); |
|
75 | + /** |
|
76 | + * Encode the given data in the format |
|
77 | + * |
|
78 | + * @param array $data |
|
79 | + * resulting data that needs to |
|
80 | + * be encoded in the given format |
|
81 | + * @param boolean $humanReadable |
|
82 | + * set to TRUE when restler |
|
83 | + * is not running in production mode. Formatter has to |
|
84 | + * make the encoded output more human readable |
|
85 | + * @return string encoded string |
|
86 | + */ |
|
87 | + public function encode($data, $humanReadable = false); |
|
88 | 88 | |
89 | - /** |
|
90 | - * Decode the given data from the format |
|
91 | - * |
|
92 | - * @param string $data |
|
93 | - * data sent from client to |
|
94 | - * the api in the given format. |
|
95 | - * @return array associative array of the parsed data |
|
96 | - */ |
|
97 | - public function decode($data); |
|
89 | + /** |
|
90 | + * Decode the given data from the format |
|
91 | + * |
|
92 | + * @param string $data |
|
93 | + * data sent from client to |
|
94 | + * the api in the given format. |
|
95 | + * @return array associative array of the parsed data |
|
96 | + */ |
|
97 | + public function decode($data); |
|
98 | 98 | |
99 | - /** |
|
100 | - * @return boolean is parsing the request supported? |
|
101 | - */ |
|
102 | - public function isReadable(); |
|
99 | + /** |
|
100 | + * @return boolean is parsing the request supported? |
|
101 | + */ |
|
102 | + public function isReadable(); |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return boolean is composing response supported? |
|
106 | - */ |
|
107 | - public function isWritable(); |
|
104 | + /** |
|
105 | + * @return boolean is composing response supported? |
|
106 | + */ |
|
107 | + public function isWritable(); |
|
108 | 108 | } |
109 | 109 |
@@ -13,132 +13,132 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class MultiFormat implements iFormat |
15 | 15 | { |
16 | - /** |
|
17 | - * override in the extending class |
|
18 | - */ |
|
19 | - const MIME = 'text/plain,text/html'; |
|
20 | - /** |
|
21 | - * override in the extending class |
|
22 | - */ |
|
23 | - const EXTENSION = 'txt,html'; |
|
16 | + /** |
|
17 | + * override in the extending class |
|
18 | + */ |
|
19 | + const MIME = 'text/plain,text/html'; |
|
20 | + /** |
|
21 | + * override in the extending class |
|
22 | + */ |
|
23 | + const EXTENSION = 'txt,html'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var string charset encoding defaults to UTF8 |
|
27 | - */ |
|
28 | - protected $charset='utf-8'; |
|
25 | + /** |
|
26 | + * @var string charset encoding defaults to UTF8 |
|
27 | + */ |
|
28 | + protected $charset='utf-8'; |
|
29 | 29 | |
30 | - public static $mime; |
|
31 | - public static $extension; |
|
30 | + public static $mime; |
|
31 | + public static $extension; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Injected at runtime |
|
35 | - * |
|
36 | - * @var \Luracast\Restler\Restler |
|
37 | - */ |
|
38 | - public $restler; |
|
33 | + /** |
|
34 | + * Injected at runtime |
|
35 | + * |
|
36 | + * @var \Luracast\Restler\Restler |
|
37 | + */ |
|
38 | + public $restler; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get MIME type => Extension mappings as an associative array |
|
42 | - * |
|
43 | - * @return array list of mime strings for the format |
|
44 | - * @example array('application/json'=>'json'); |
|
45 | - */ |
|
46 | - public function getMIMEMap() |
|
47 | - { |
|
48 | - $extensions = explode(',',static::EXTENSION); |
|
49 | - $mimes = explode(',',static::MIME); |
|
50 | - $count = max(count($extensions), count($mimes)); |
|
51 | - $extensions += array_fill(0, $count, end($extensions)); |
|
52 | - $mimes += array_fill(0, $count, end($mimes)); |
|
53 | - return array_combine($mimes,$extensions); |
|
54 | - } |
|
40 | + /** |
|
41 | + * Get MIME type => Extension mappings as an associative array |
|
42 | + * |
|
43 | + * @return array list of mime strings for the format |
|
44 | + * @example array('application/json'=>'json'); |
|
45 | + */ |
|
46 | + public function getMIMEMap() |
|
47 | + { |
|
48 | + $extensions = explode(',',static::EXTENSION); |
|
49 | + $mimes = explode(',',static::MIME); |
|
50 | + $count = max(count($extensions), count($mimes)); |
|
51 | + $extensions += array_fill(0, $count, end($extensions)); |
|
52 | + $mimes += array_fill(0, $count, end($mimes)); |
|
53 | + return array_combine($mimes,$extensions); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Set the selected MIME type |
|
58 | - * |
|
59 | - * @param string $mime |
|
60 | - * MIME type |
|
61 | - */ |
|
62 | - public function setMIME($mime) |
|
63 | - { |
|
64 | - static::$mime = $mime; |
|
65 | - } |
|
56 | + /** |
|
57 | + * Set the selected MIME type |
|
58 | + * |
|
59 | + * @param string $mime |
|
60 | + * MIME type |
|
61 | + */ |
|
62 | + public function setMIME($mime) |
|
63 | + { |
|
64 | + static::$mime = $mime; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Content-Type field of the HTTP header can send a charset |
|
69 | - * parameter in the HTTP header to specify the character |
|
70 | - * encoding of the document. |
|
71 | - * This information is passed |
|
72 | - * here so that Format class can encode data accordingly |
|
73 | - * Format class may choose to ignore this and use its |
|
74 | - * default character set. |
|
75 | - * |
|
76 | - * @param string $charset |
|
77 | - * Example utf-8 |
|
78 | - */ |
|
79 | - public function setCharset($charset) |
|
80 | - { |
|
81 | - $this->charset = $charset; |
|
82 | - } |
|
67 | + /** |
|
68 | + * Content-Type field of the HTTP header can send a charset |
|
69 | + * parameter in the HTTP header to specify the character |
|
70 | + * encoding of the document. |
|
71 | + * This information is passed |
|
72 | + * here so that Format class can encode data accordingly |
|
73 | + * Format class may choose to ignore this and use its |
|
74 | + * default character set. |
|
75 | + * |
|
76 | + * @param string $charset |
|
77 | + * Example utf-8 |
|
78 | + */ |
|
79 | + public function setCharset($charset) |
|
80 | + { |
|
81 | + $this->charset = $charset; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Content-Type accepted by the Format class |
|
86 | - * |
|
87 | - * @return string $charset Example utf-8 |
|
88 | - */ |
|
89 | - public function getCharset() |
|
90 | - { |
|
91 | - return $this->charset; |
|
92 | - } |
|
84 | + /** |
|
85 | + * Content-Type accepted by the Format class |
|
86 | + * |
|
87 | + * @return string $charset Example utf-8 |
|
88 | + */ |
|
89 | + public function getCharset() |
|
90 | + { |
|
91 | + return $this->charset; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get selected MIME type |
|
96 | - */ |
|
97 | - public function getMIME() |
|
98 | - { |
|
99 | - return static::$mime; |
|
100 | - } |
|
94 | + /** |
|
95 | + * Get selected MIME type |
|
96 | + */ |
|
97 | + public function getMIME() |
|
98 | + { |
|
99 | + return static::$mime; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Set the selected file extension |
|
104 | - * |
|
105 | - * @param string $extension |
|
106 | - * file extension |
|
107 | - */ |
|
108 | - public function setExtension($extension) |
|
109 | - { |
|
110 | - static::$extension = $extension; |
|
111 | - } |
|
102 | + /** |
|
103 | + * Set the selected file extension |
|
104 | + * |
|
105 | + * @param string $extension |
|
106 | + * file extension |
|
107 | + */ |
|
108 | + public function setExtension($extension) |
|
109 | + { |
|
110 | + static::$extension = $extension; |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Get the selected file extension |
|
115 | - * |
|
116 | - * @return string file extension |
|
117 | - */ |
|
118 | - public function getExtension() |
|
119 | - { |
|
120 | - return static::$extension; |
|
121 | - } |
|
113 | + /** |
|
114 | + * Get the selected file extension |
|
115 | + * |
|
116 | + * @return string file extension |
|
117 | + */ |
|
118 | + public function getExtension() |
|
119 | + { |
|
120 | + return static::$extension; |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @return boolean is parsing the request supported? |
|
125 | - */ |
|
126 | - public function isReadable() |
|
127 | - { |
|
128 | - return true; |
|
129 | - } |
|
123 | + /** |
|
124 | + * @return boolean is parsing the request supported? |
|
125 | + */ |
|
126 | + public function isReadable() |
|
127 | + { |
|
128 | + return true; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @return boolean is composing response supported? |
|
133 | - */ |
|
134 | - public function isWritable() |
|
135 | - { |
|
136 | - return true; |
|
137 | - } |
|
131 | + /** |
|
132 | + * @return boolean is composing response supported? |
|
133 | + */ |
|
134 | + public function isWritable() |
|
135 | + { |
|
136 | + return true; |
|
137 | + } |
|
138 | 138 | |
139 | - public function __toString() |
|
140 | - { |
|
141 | - return $this->getExtension(); |
|
142 | - } |
|
139 | + public function __toString() |
|
140 | + { |
|
141 | + return $this->getExtension(); |
|
142 | + } |
|
143 | 143 | } |
144 | 144 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | /** |
26 | 26 | * @var string charset encoding defaults to UTF8 |
27 | 27 | */ |
28 | - protected $charset='utf-8'; |
|
28 | + protected $charset = 'utf-8'; |
|
29 | 29 | |
30 | 30 | public static $mime; |
31 | 31 | public static $extension; |
@@ -45,12 +45,12 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function getMIMEMap() |
47 | 47 | { |
48 | - $extensions = explode(',',static::EXTENSION); |
|
49 | - $mimes = explode(',',static::MIME); |
|
48 | + $extensions = explode(',', static::EXTENSION); |
|
49 | + $mimes = explode(',', static::MIME); |
|
50 | 50 | $count = max(count($extensions), count($mimes)); |
51 | 51 | $extensions += array_fill(0, $count, end($extensions)); |
52 | 52 | $mimes += array_fill(0, $count, end($mimes)); |
53 | - return array_combine($mimes,$extensions); |
|
53 | + return array_combine($mimes, $extensions); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -6,51 +6,51 @@ |
||
6 | 6 | |
7 | 7 | abstract class DependentFormat extends Format |
8 | 8 | { |
9 | - /** |
|
10 | - * override in the extending class |
|
11 | - * |
|
12 | - * @example symfony/yaml:* |
|
13 | - */ |
|
14 | - const PACKAGE_NAME = 'vendor/project:version'; |
|
15 | - |
|
16 | - /** |
|
17 | - * override in the extending class |
|
18 | - * |
|
19 | - * fully qualified class name |
|
20 | - */ |
|
21 | - const EXTERNAL_CLASS = 'Namespace\\ClassName'; |
|
22 | - |
|
23 | - /** |
|
24 | - * Get external class => packagist package name as an associative array |
|
25 | - * |
|
26 | - * @return array list of dependencies for the format |
|
27 | - */ |
|
28 | - public function getDependencyMap() |
|
29 | - { |
|
30 | - return array( |
|
31 | - static::EXTERNAL_CLASS => static::PACKAGE_NAME |
|
32 | - ); |
|
33 | - } |
|
34 | - |
|
35 | - protected function checkDependency($class = null) |
|
36 | - { |
|
37 | - if (empty($class)) { |
|
38 | - $class = key($this->getDependencyMap()); |
|
39 | - } |
|
40 | - if (!class_exists($class, true)) { |
|
41 | - $map = $this->getDependencyMap(); |
|
42 | - $package = $map[$class]; |
|
43 | - throw new RestException( |
|
44 | - 500, |
|
45 | - get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | - $package . '` from the project root. Read https://getcomposer.org for more info' |
|
47 | - ); |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - public function __construct() |
|
52 | - { |
|
53 | - $this->checkDependency(); |
|
54 | - } |
|
9 | + /** |
|
10 | + * override in the extending class |
|
11 | + * |
|
12 | + * @example symfony/yaml:* |
|
13 | + */ |
|
14 | + const PACKAGE_NAME = 'vendor/project:version'; |
|
15 | + |
|
16 | + /** |
|
17 | + * override in the extending class |
|
18 | + * |
|
19 | + * fully qualified class name |
|
20 | + */ |
|
21 | + const EXTERNAL_CLASS = 'Namespace\\ClassName'; |
|
22 | + |
|
23 | + /** |
|
24 | + * Get external class => packagist package name as an associative array |
|
25 | + * |
|
26 | + * @return array list of dependencies for the format |
|
27 | + */ |
|
28 | + public function getDependencyMap() |
|
29 | + { |
|
30 | + return array( |
|
31 | + static::EXTERNAL_CLASS => static::PACKAGE_NAME |
|
32 | + ); |
|
33 | + } |
|
34 | + |
|
35 | + protected function checkDependency($class = null) |
|
36 | + { |
|
37 | + if (empty($class)) { |
|
38 | + $class = key($this->getDependencyMap()); |
|
39 | + } |
|
40 | + if (!class_exists($class, true)) { |
|
41 | + $map = $this->getDependencyMap(); |
|
42 | + $package = $map[$class]; |
|
43 | + throw new RestException( |
|
44 | + 500, |
|
45 | + get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | + $package . '` from the project root. Read https://getcomposer.org for more info' |
|
47 | + ); |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + public function __construct() |
|
52 | + { |
|
53 | + $this->checkDependency(); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | } |
57 | 57 | \ No newline at end of file |
@@ -42,8 +42,8 @@ |
||
42 | 42 | $package = $map[$class]; |
43 | 43 | throw new RestException( |
44 | 44 | 500, |
45 | - get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | - $package . '` from the project root. Read https://getcomposer.org for more info' |
|
45 | + get_called_class().' has external dependency. Please run `composer require '. |
|
46 | + $package.'` from the project root. Read https://getcomposer.org for more info' |
|
47 | 47 | ); |
48 | 48 | } |
49 | 49 | } |
@@ -15,34 +15,34 @@ |
||
15 | 15 | */ |
16 | 16 | class JsFormat extends JsonFormat |
17 | 17 | { |
18 | - const MIME = 'text/javascript'; |
|
19 | - const EXTENSION = 'js'; |
|
18 | + const MIME = 'text/javascript'; |
|
19 | + const EXTENSION = 'js'; |
|
20 | 20 | |
21 | - public static $callbackMethodName = 'parseResponse'; |
|
22 | - public static $callbackOverrideQueryString = 'callback'; |
|
23 | - public static $includeHeaders = true; |
|
21 | + public static $callbackMethodName = 'parseResponse'; |
|
22 | + public static $callbackOverrideQueryString = 'callback'; |
|
23 | + public static $includeHeaders = true; |
|
24 | 24 | |
25 | - public function encode($data, $human_readable = false) |
|
26 | - { |
|
27 | - $r = array(); |
|
28 | - if (static::$includeHeaders) { |
|
29 | - $r['meta'] = array(); |
|
30 | - foreach (headers_list() as $header) { |
|
31 | - list($h, $v) = explode(': ', $header, 2); |
|
32 | - $r['meta'][$h] = $v; |
|
33 | - } |
|
34 | - } |
|
35 | - $r['data'] = $data; |
|
36 | - if (isset($_GET[static::$callbackOverrideQueryString])) { |
|
37 | - static::$callbackMethodName |
|
38 | - = (string) $_GET[static::$callbackOverrideQueryString]; |
|
39 | - } |
|
40 | - return static::$callbackMethodName . '(' |
|
41 | - . parent::encode($r, $human_readable) . ');'; |
|
42 | - } |
|
25 | + public function encode($data, $human_readable = false) |
|
26 | + { |
|
27 | + $r = array(); |
|
28 | + if (static::$includeHeaders) { |
|
29 | + $r['meta'] = array(); |
|
30 | + foreach (headers_list() as $header) { |
|
31 | + list($h, $v) = explode(': ', $header, 2); |
|
32 | + $r['meta'][$h] = $v; |
|
33 | + } |
|
34 | + } |
|
35 | + $r['data'] = $data; |
|
36 | + if (isset($_GET[static::$callbackOverrideQueryString])) { |
|
37 | + static::$callbackMethodName |
|
38 | + = (string) $_GET[static::$callbackOverrideQueryString]; |
|
39 | + } |
|
40 | + return static::$callbackMethodName . '(' |
|
41 | + . parent::encode($r, $human_readable) . ');'; |
|
42 | + } |
|
43 | 43 | |
44 | - public function isReadable() |
|
45 | - { |
|
46 | - return false; |
|
47 | - } |
|
44 | + public function isReadable() |
|
45 | + { |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | } |
@@ -37,8 +37,8 @@ |
||
37 | 37 | static::$callbackMethodName |
38 | 38 | = (string) $_GET[static::$callbackOverrideQueryString]; |
39 | 39 | } |
40 | - return static::$callbackMethodName . '(' |
|
41 | - . parent::encode($r, $human_readable) . ');'; |
|
40 | + return static::$callbackMethodName.'(' |
|
41 | + . parent::encode($r, $human_readable).');'; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function isReadable() |