├── README.md
├── changelog.md
├── config.php
├── inc
├── codeSamples.php
├── markdown.php
├── raml.php
├── ramlDataObject.php
├── ramlPathObject.php
└── spyc.php
├── index.php
├── raml
├── github.raml
├── githubrepo.raml
├── songs.raml
└── twitter.raml
└── templates
├── grey
├── _404.phtml
├── _action.phtml
├── _comments.phtml
├── _menu.phtml
├── _resources.phtml
├── _verbs.phtml
└── index.phtml
└── orange
├── _404.phtml
├── _action.phtml
├── _comments.phtml
├── _menu.phtml
├── _resources.phtml
├── _verbs.phtml
└── index.phtml
/README.md:
--------------------------------------------------------------------------------
1 | # RAML 2 HTML for PHP
2 |
3 | RAML 2 HTML for PHP is a simple application that makes use of multiple templates to allow you to build and customize your API Docs using RAML.
4 |
5 | 
6 |
7 | #### What all does RAML 2 HTML for PHP do?
8 | RAML 2 HTML for PHP builds out a multi-page documentation site for you based on your RAML spec. This lets users explore and understand how your API works, while letting you style the documentation (within the RAML spec) using HTML or markdown, and provide your users with automatically generated code samples while also letting you include community and commenting functionality within your documentation via Disqus.
9 |
10 | Of course, being templated, you can add additional features/ functionality to your site quickly and easily by setting up your own template (or copying the grey template) and modifying the HTML/CSS/PHP code.
11 |
12 | #### What version of PHP does RAML 2 HTML require?
13 | RAML 2 HTML for PHP versions 1.0 or greater require PHP 5.3+
14 |
15 | If you are running an older version of PHP, it is highly recommend you upgrade, but if you are unable to do so, you can use RAML version 0.2 which supports PHP 5+. However, this version is extremely limited and is not being maintained or supported.
16 |
17 | No other external libraries, databases, or extensions are required to run RAML to HTML for PHP, although if APC is installed the script will take advantage of it for caching purposes.
18 |
19 | #### How do I set it up?
20 | Important setup information is stored in config.php. You can read setup instructions [here](http://www.mikestowe.com/2014/05/raml-2-html.php).
21 |
22 | #### Is there a Demo?
23 | Yes! You can find the latest stable demo and the latest development version demos [here](http://www.mikestowe.com/2014/05/raml-2-html.php).
24 |
25 | #### Does it support all RAML features?
26 | Not yet, although version 1.0 was a complete rewrite supports base, path variables, multi-level includes, resourceTypes, traits, and more. Other features will be added down the road!
27 |
28 | #### How Can I Help?
29 | Easy! Download and use RAML 2 HTML for PHP, tell your friends, if you find issues report them, or even better - feel free to contribute by forking and making pull requests!
30 |
31 | #### License
32 | RAML is covered under the GPL2 license. However, the included class Spyc falls under the MIT license.
33 |
--------------------------------------------------------------------------------
/changelog.md:
--------------------------------------------------------------------------------
1 | #RAML2HTML for PHP
2 | ###version 1.3 changelog
3 | - Added in support for resourceTypes
4 | - Fixed isArray() bug
5 |
6 |
7 | ###version 1.2 changelog
8 |
9 | - Upgraded Spyc to fix spacing issues
10 | - Added in support for basic markdown
11 | - headers
12 | - bold and italic text
13 | - lists (ordered/unordered)
14 | - links and images
15 | - code blocks
16 | - Added in support for alerts
17 | - info
18 | - warning
19 | - alert
20 | - Added in Auto-generated Code Samples
21 | - JavaScript
22 | - PHP via Curl
23 | - Ruby on Rails
24 | - Added in comments via Disqus (optional)
25 | - Added in code and response formatting
26 | - Added "required" and "example" fields to Parameter boxes
27 |
--------------------------------------------------------------------------------
/config.php:
--------------------------------------------------------------------------------
1 |
25 |
--------------------------------------------------------------------------------
/inc/codeSamples.php:
--------------------------------------------------------------------------------
1 |
6 | * @link https://github.com/mikestowe/php-raml2html
7 | * @link http://www.mikestowe.com/2014/05/raml-2-html.php
8 | * @license http://www.gnu.org/licenses/gpl-2.0.html GPL v2
9 | */
10 |
11 | namespace RAML2HTML;
12 |
13 | /**
14 | * Code Sample Generator
15 | * @package RAML2HTML
16 | */
17 | class codeSamples
18 | {
19 | private $RAML = false;
20 |
21 | /**
22 | * Setup Class
23 | * @param RAML
24 | * @return void
25 | */
26 | function __construct($object) {
27 | $this->RAML = $object;
28 | }
29 |
30 | /**
31 | * Create a Path for API Calls
32 | * @return string
33 | */
34 | private function path() {
35 | $path = $this->RAML->baseUri . $this->RAML->getCurrentPath();
36 | if ($this->RAML->getCurrentAction() == 'GET') {
37 | $path .= '?';
38 | $opt = true;
39 |
40 | foreach ($this->RAML->action()->get('queryParameters')->toArray() as $k => $v) {
41 | if (isset($v['required']) && $v['required'] == 1) {
42 | $path .= $k . '=';
43 | if (isset($v['example'])) {
44 | $path .= urlencode($v['example']) . '&';
45 | } else {
46 | $path .= '%' . $k . '%&';
47 | }
48 | } elseif (isset($v['required']) && $opt && isset($v['example'])) {
49 | $path .= $k . '=' . urlencode($v['example']) . '&';
50 | $opt = false;
51 | }
52 | }
53 | return substr($path, 0,-1);
54 | }
55 |
56 | return $path;
57 | }
58 |
59 |
60 | /**
61 | * Produce Code Sample for PHP
62 | * @return string
63 | */
64 | public function php() {
65 | $template = '$ch = curl_init();' . "\n";
66 | $template .= 'curl_setopt($ch, CURLOPT_URL, "'. $this->path() .'");' . "\n";
67 | $template .= 'curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);' . "\n";
68 | $template .= 'curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); ' . "\n";
69 | $template .= '' . "\n";
70 |
71 | $template .= '// Make sure you set the nessary headers as a $headers array' . "\n";
72 | $template .= 'curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);' . "\n";
73 |
74 | $template .= '' . "\n";
75 |
76 | if ($this->RAML->getCurrentAction() != 'GET') {
77 | $template .= 'curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "'.strtolower($this->RAML->getCurrentAction()).'")' . "\n";
78 | }
79 |
80 | if (in_array($this->RAML->getCurrentAction(), array('POST', 'PUT', 'PATCH'))) {
81 | if($this->RAML->get('body')->get('application/json')->get('example')) {
82 | $template .= '$body = \'' . str_replace("'", "\\'", $this->RAML->get('body')->get('application/json')->get('example')) . '\';' . "\n";
83 | } else {
84 | $template .= '// $body is your JSON/ XML/ Text/ Form Query/ etc' . "\n";
85 | }
86 |
87 | $template .= 'curl_setopt($ch, CURLOPT_POSTFIELDS, $body);' . "\n";
88 | $template .= '' . "\n";
89 | }
90 |
91 | $template .= '$response = curl_exec($ch);' . "\n";
92 | $template .= '$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);' . "\n";
93 | $template .= 'curl_close($ch);' . "\n";
94 |
95 | return $template;
96 | }
97 |
98 |
99 |
100 | /**
101 | * Produce Code Sample for Rails
102 | * @return string
103 | */
104 | public function rails() {
105 | $template = 'uri = URI.parse("'.$this->path().'")' . "\n";
106 | $template .= 'http = Net::HTTP.new(uri.host, uri.port)' . "\n";
107 | $template .= 'request = Net::HTTP::'.ucfirst(strtolower($this->RAML->getCurrentAction())).'.new(uri.request_uri)' . "\n\n";
108 | $template .= '# Make sure you set the appropriate headers' . "\n";
109 | $template .= 'request["header"] = "header value"' . "\n\n";
110 |
111 | if ($this->RAML->getCurrentAction() != 'GET') {
112 | $template .= '# body is your JSON/ XML/ Text/ Form Query/ etc' . "\n";
113 | $template .= 'request.set_form_data(body)' . "\n\n";
114 | }
115 |
116 | $template .= 'response = http.request(request)' . "\n";
117 |
118 | return $template;
119 | }
120 |
121 |
122 |
123 | /**
124 | * Produce Code Sample for JavaScript
125 | * @return string
126 | */
127 | public function javascript() {
128 | $template = 'var xmlHttp = new XMLHttpRequest();' . "\n";
129 | $template .= 'xmlHttp.open("'.strtoupper($this->RAML->getCurrentAction()).'", "'.$this->path().'", false);' . "\n";
130 | $template .= "\n";
131 | $template .= "// Make sure you set the appropriate headers\n";
132 | $template .= 'xmlHttp.setRequestHeader("Header Key", "Header Value");' . "\n\n";
133 | $send = 'null';
134 |
135 | if ($this->RAML->getCurrentAction() != 'GET') {
136 | $template .= 'var data = "# body is your JSON/ XML/ Text/ Form Query/ etc"' . "\n";
137 | $send = 'data';
138 | }
139 |
140 | $template .= 'xmlHttp.send('.$send.');' . "\n\n";
141 | $template .= 'var response = xmlHttp.responseText;' . "\n";
142 |
143 | return $template;
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/inc/markdown.php:
--------------------------------------------------------------------------------
1 |
6 | * @link https://github.com/mikestowe/php-raml2html
7 | * @link http://www.mikestowe.com/2014/05/raml-2-html.php
8 | * @license http://www.gnu.org/licenses/gpl-2.0.html GPL v2
9 | */
10 |
11 | namespace RAML2HTML;
12 |
13 | /**
14 | * Simple Markdown Parser
15 | * @package RAML2HTML
16 | */
17 | class markdown
18 | {
19 |
20 | static $patterns = array(
21 | '\n(\s)*-' => '
• ',
22 | '\n(\s)*([0-9]+)\.?' => '
$1. ',
23 | '\[(info|warning|alert)\]([^\]]+)\[\/(info|warning|alert)\]' => '
'.htmlentities($matches[1]).'
';
45 | }, $input);
46 |
47 | // Handle line breaks
48 | return str_replace("\n", "
58 | * $Spyc = new Spyc;
59 | * $array = $Spyc->load($file);
60 | *
61 | * or:
62 | *
63 | * $array = Spyc::YAMLLoad($file);
64 | *
65 | * or:
66 | *
67 | * $array = spyc_load_file($file);
68 | *
69 | * @package Spyc
70 | */
71 | class Spyc {
72 |
73 | // SETTINGS
74 |
75 | const REMPTY = "\0\0\0\0\0";
76 |
77 | /**
78 | * Setting this to true will force YAMLDump to enclose any string value in
79 | * quotes. False by default.
80 | *
81 | * @var bool
82 | */
83 | public $setting_dump_force_quotes = false;
84 |
85 | /**
86 | * Setting this to true will forse YAMLLoad to use syck_load function when
87 | * possible. False by default.
88 | * @var bool
89 | */
90 | public $setting_use_syck_is_possible = false;
91 |
92 | /**#@+
93 | * @access private
94 | * @var mixed
95 | */
96 | private $_dumpIndent;
97 | private $_dumpWordWrap;
98 | private $_containsGroupAnchor = false;
99 | private $_containsGroupAlias = false;
100 | private $path;
101 | private $result;
102 | private $LiteralPlaceHolder = '___YAML_Literal_Block___';
103 | private $SavedGroups = array();
104 | private $indent;
105 | /**
106 | * Path modifier that should be applied after adding current element.
107 | * @var array
108 | */
109 | private $delayedPath = array();
110 |
111 | /**#@+
112 | * @access public
113 | * @var mixed
114 | */
115 | public $_nodeId;
116 |
117 | /**
118 | * Load a valid YAML string to Spyc.
119 | * @param string $input
120 | * @return array
121 | */
122 | public function load($input) {
123 | return $this -> __loadString($input);
124 | }
125 |
126 | /**
127 | * Load a valid YAML file to Spyc.
128 | * @param string $file
129 | * @return array
130 | */
131 | public function loadFile($file) {
132 | return $this -> __load($file);
133 | }
134 |
135 | /**
136 | * Load YAML into a PHP array statically
137 | *
138 | * The load method, when supplied with a YAML stream (string or file),
139 | * will do its best to convert YAML in a file into a PHP array. Pretty
140 | * simple.
141 | * Usage:
142 | *
143 | * $array = Spyc::YAMLLoad('lucky.yaml');
144 | * print_r($array);
145 | *
146 | * @access public
147 | * @return array
148 | * @param string $input Path of YAML file or string containing YAML
149 | */
150 | public static function YAMLLoad($input) {
151 | $Spyc = new Spyc;
152 | return $Spyc -> __load($input);
153 | }
154 |
155 | /**
156 | * Load a string of YAML into a PHP array statically
157 | *
158 | * The load method, when supplied with a YAML string, will do its best
159 | * to convert YAML in a string into a PHP array. Pretty simple.
160 | *
161 | * Note: use this function if you don't want files from the file system
162 | * loaded and processed as YAML. This is of interest to people concerned
163 | * about security whose input is from a string.
164 | *
165 | * Usage:
166 | *
167 | * $array = Spyc::YAMLLoadString("---\n0: hello world\n");
168 | * print_r($array);
169 | *
170 | * @access public
171 | * @return array
172 | * @param string $input String containing YAML
173 | */
174 | public static function YAMLLoadString($input) {
175 | $Spyc = new Spyc;
176 | return $Spyc -> __loadString($input);
177 | }
178 |
179 | /**
180 | * Dump YAML from PHP array statically
181 | *
182 | * The dump method, when supplied with an array, will do its best
183 | * to convert the array into friendly YAML. Pretty simple. Feel free to
184 | * save the returned string as nothing.yaml and pass it around.
185 | *
186 | * Oh, and you can decide how big the indent is and what the wordwrap
187 | * for folding is. Pretty cool -- just pass in 'false' for either if
188 | * you want to use the default.
189 | *
190 | * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
191 | * you can turn off wordwrap by passing in 0.
192 | *
193 | * @access public
194 | * @return string
195 | * @param array $array PHP array
196 | * @param int $indent Pass in false to use the default, which is 2
197 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
198 | * @param int $no_opening_dashes Do not start YAML file with "---\n"
199 | */
200 | public static function YAMLDump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) {
201 | $spyc = new Spyc;
202 | return $spyc -> dump($array, $indent, $wordwrap, $no_opening_dashes);
203 | }
204 |
205 | /**
206 | * Dump PHP array to YAML
207 | *
208 | * The dump method, when supplied with an array, will do its best
209 | * to convert the array into friendly YAML. Pretty simple. Feel free to
210 | * save the returned string as tasteful.yaml and pass it around.
211 | *
212 | * Oh, and you can decide how big the indent is and what the wordwrap
213 | * for folding is. Pretty cool -- just pass in 'false' for either if
214 | * you want to use the default.
215 | *
216 | * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
217 | * you can turn off wordwrap by passing in 0.
218 | *
219 | * @access public
220 | * @return string
221 | * @param array $array PHP array
222 | * @param int $indent Pass in false to use the default, which is 2
223 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
224 | */
225 | public function dump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) {
226 | // Dumps to some very clean YAML. We'll have to add some more features
227 | // and options soon. And better support for folding.
228 |
229 | // New features and options.
230 | if ($indent === false or !is_numeric($indent)) {
231 | $this -> _dumpIndent = 2;
232 | } else {
233 | $this -> _dumpIndent = $indent;
234 | }
235 |
236 | if ($wordwrap === false or !is_numeric($wordwrap)) {
237 | $this -> _dumpWordWrap = 40;
238 | } else {
239 | $this -> _dumpWordWrap = $wordwrap;
240 | }
241 |
242 | // New YAML document
243 | $string = "";
244 | if (!$no_opening_dashes)
245 | $string = "---\n";
246 |
247 | // Start at the base of the array and move through it.
248 | if ($array) {
249 | $array = (array)$array;
250 | $previous_key = -1;
251 | foreach ($array as $key => $value) {
252 | if (!isset($first_key))
253 | $first_key = $key;
254 | $string .= $this -> _yamlize($key, $value, 0, $previous_key, $first_key, $array);
255 | $previous_key = $key;
256 | }
257 | }
258 | return $string;
259 | }
260 |
261 | /**
262 | * Attempts to convert a key / value array item to YAML
263 | * @access private
264 | * @return string
265 | * @param $key The name of the key
266 | * @param $value The value of the item
267 | * @param $indent The indent of the current node
268 | */
269 | private function _yamlize($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
270 | if (is_array($value)) {
271 | if (empty($value))
272 | return $this -> _dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
273 | // It has children. What to do?
274 | // Make it the right kind of item
275 | $string = $this -> _dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
276 | // Add the indent
277 | $indent += $this -> _dumpIndent;
278 | // Yamlize the array
279 | $string .= $this -> _yamlizeArray($value, $indent);
280 | } elseif (!is_array($value)) {
281 | // It doesn't have children. Yip.
282 | $string = $this -> _dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
283 | }
284 | return $string;
285 | }
286 |
287 | /**
288 | * Attempts to convert an array to YAML
289 | * @access private
290 | * @return string
291 | * @param $array The array you want to convert
292 | * @param $indent The indent of the current level
293 | */
294 | private function _yamlizeArray($array, $indent) {
295 | if (is_array($array)) {
296 | $string = '';
297 | $previous_key = -1;
298 | foreach ($array as $key => $value) {
299 | if (!isset($first_key))
300 | $first_key = $key;
301 | $string .= $this -> _yamlize($key, $value, $indent, $previous_key, $first_key, $array);
302 | $previous_key = $key;
303 | }
304 | return $string;
305 | } else {
306 | return false;
307 | }
308 | }
309 |
310 | /**
311 | * Returns YAML from a key and a value
312 | * @access private
313 | * @return string
314 | * @param $key The name of the key
315 | * @param $value The value of the item
316 | * @param $indent The indent of the current node
317 | */
318 | private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
319 | // do some folding here, for blocks
320 | if (is_string($value) && ((strpos($value, "\n") !== false || strpos($value, ": ") !== false || strpos($value, "- ") !== false || strpos($value, "*") !== false || strpos($value, "#") !== false || strpos($value, "<") !== false || strpos($value, ">") !== false || strpos($value, '%') !== false || strpos($value, ' ') !== false || strpos($value, "[") !== false || strpos($value, "]") !== false || strpos($value, "{") !== false || strpos($value, "}") !== false) || strpos($value, "&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 || substr($value, -1, 1) == ':')) {
321 | $value = $this -> _doLiteralBlock($value, $indent);
322 | } else {
323 | $value = $this -> _doFolding($value, $indent);
324 | }
325 |
326 | if ($value === array())
327 | $value = '[ ]';
328 | if ($value === "")
329 | $value = '""';
330 | if (self::isTranslationWord($value)) {
331 | $value = $this -> _doLiteralBlock($value, $indent);
332 | }
333 | if (trim($value) != $value)
334 | $value = $this -> _doLiteralBlock($value, $indent);
335 |
336 | if (is_bool($value)) {
337 | $value = $value ? "true" : "false";
338 | }
339 |
340 | if ($value === null)
341 | $value = 'null';
342 | if ($value === "'" . self::REMPTY . "'")
343 | $value = null;
344 |
345 | $spaces = str_repeat(' ', $indent);
346 |
347 | //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
348 | if (is_array($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) {
349 | // It's a sequence
350 | $string = $spaces . '- ' . $value . "\n";
351 | } else {
352 | // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
353 | // It's mapped
354 | if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"';
355 | }
356 | $string = rtrim($spaces . $key . ': ' . $value) . "\n";
357 | }
358 | return $string;
359 | }
360 |
361 | /**
362 | * Creates a literal block for dumping
363 | * @access private
364 | * @return string
365 | * @param $value
366 | * @param $indent int The value of the indent
367 | */
368 | private function _doLiteralBlock($value, $indent) {
369 | if ($value === "\n")
370 | return '\n';
371 | if (strpos($value, "\n") === false && strpos($value, "'") === false) {
372 | return sprintf("'%s'", $value);
373 | }
374 | if (strpos($value, "\n") === false && strpos($value, '"') === false) {
375 | return sprintf('"%s"', $value);
376 | }
377 | $exploded = explode("\n", $value);
378 | $newValue = '|';
379 | if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) {
380 | $newValue = $exploded[0];
381 | unset($exploded[0]);
382 | }
383 | $indent += $this -> _dumpIndent;
384 | $spaces = str_repeat(' ', $indent);
385 | foreach ($exploded as $line) {
386 | $line = trim($line);
387 | if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line) - 1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line) - 1)) {
388 | $line = substr($line, 1, -1);
389 | }
390 | $newValue .= "\n" . $spaces . ($line);
391 | }
392 | return $newValue;
393 | }
394 |
395 | /**
396 | * Folds a string of text, if necessary
397 | * @access private
398 | * @return string
399 | * @param $value The string you wish to fold
400 | */
401 | private function _doFolding($value, $indent) {
402 | // Don't do anything if wordwrap is set to 0
403 |
404 | if ($this -> _dumpWordWrap !== 0 && is_string($value) && strlen($value) > $this -> _dumpWordWrap) {
405 | $indent += $this -> _dumpIndent;
406 | $indent = str_repeat(' ', $indent);
407 | $wrapped = wordwrap($value, $this -> _dumpWordWrap, "\n$indent");
408 | $value = ">\n" . $indent . $wrapped;
409 | } else {
410 | if ($this -> setting_dump_force_quotes && is_string($value) && $value !== self::REMPTY)
411 | $value = '"' . $value . '"';
412 | if (is_numeric($value) && is_string($value))
413 | $value = '"' . $value . '"';
414 | }
415 |
416 | return $value;
417 | }
418 |
419 | private function isTrueWord($value) {
420 | $words = self::getTranslations(array('true', 'on', 'yes', 'y'));
421 | return in_array($value, $words, true);
422 | }
423 |
424 | private function isFalseWord($value) {
425 | $words = self::getTranslations(array('false', 'off', 'no', 'n'));
426 | return in_array($value, $words, true);
427 | }
428 |
429 | private function isNullWord($value) {
430 | $words = self::getTranslations(array('null', '~'));
431 | return in_array($value, $words, true);
432 | }
433 |
434 | private function isTranslationWord($value) {
435 | return (self::isTrueWord($value) || self::isFalseWord($value) || self::isNullWord($value));
436 | }
437 |
438 | /**
439 | * Coerce a string into a native type
440 | * Reference: http://yaml.org/type/bool.html
441 | * TODO: Use only words from the YAML spec.
442 | * @access private
443 | * @param $value The value to coerce
444 | */
445 | private function coerceValue(&$value) {
446 | if (self::isTrueWord($value)) {
447 | $value = true;
448 | } else if (self::isFalseWord($value)) {
449 | $value = false;
450 | } else if (self::isNullWord($value)) {
451 | $value = null;
452 | }
453 | }
454 |
455 | /**
456 | * Given a set of words, perform the appropriate translations on them to
457 | * match the YAML 1.1 specification for type coercing.
458 | * @param $words The words to translate
459 | * @access private
460 | */
461 | private static function getTranslations(array $words) {
462 | $result = array();
463 | foreach ($words as $i) {
464 | $result = array_merge($result, array(ucfirst($i), strtoupper($i), strtolower($i)));
465 | }
466 | return $result;
467 | }
468 |
469 | // LOADING FUNCTIONS
470 |
471 | private function __load($input) {
472 | $Source = $this -> loadFromSource($input);
473 | return $this -> loadWithSource($Source);
474 | }
475 |
476 | private function __loadString($input) {
477 | $Source = $this -> loadFromString($input);
478 | return $this -> loadWithSource($Source);
479 | }
480 |
481 | private function loadWithSource($Source) {
482 | if (empty($Source))
483 | return array();
484 | if ($this -> setting_use_syck_is_possible && function_exists('syck_load')) {
485 | $array = syck_load(implode("\n", $Source));
486 | return is_array($array) ? $array : array();
487 | }
488 |
489 | $this -> path = array();
490 | $this -> result = array();
491 |
492 | $cnt = count($Source);
493 | for ($i = 0; $i < $cnt; $i++) {
494 | $line = $Source[$i];
495 |
496 | $this -> indent = strlen($line) - strlen(ltrim($line));
497 | $tempPath = $this -> getParentPathByIndent($this -> indent);
498 | $line = self::stripIndent($line, $this -> indent);
499 | if (self::isComment($line))
500 | continue;
501 | if (self::isEmpty($line))
502 | continue;
503 | $this -> path = $tempPath;
504 |
505 | $literalBlockStyle = self::startsLiteralBlock($line);
506 | if ($literalBlockStyle) {
507 | $line = rtrim($line, $literalBlockStyle . " \n");
508 | $literalBlock = '';
509 | $line .= ' ' . $this -> LiteralPlaceHolder;
510 | $literal_block_indent = strlen($Source[$i + 1]) - strlen(ltrim($Source[$i + 1]));
511 | while (++$i < $cnt && $this -> literalBlockContinues($Source[$i], $this -> indent)) {
512 | $literalBlock = $this -> addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent);
513 | }
514 | $i--;
515 | }
516 |
517 | // Strip out comments
518 | if (strpos($line, '#')) {
519 | $line = preg_replace('/\s*#([^"\']+)$/', '', $line);
520 | }
521 |
522 | while (++$i < $cnt && self::greedilyNeedNextLine($line)) {
523 | $line = rtrim($line, " \n\t\r") . ' ' . ltrim($Source[$i], " \t");
524 | }
525 | $i--;
526 |
527 | $lineArray = $this -> _parseLine($line);
528 |
529 | if ($literalBlockStyle)
530 | $lineArray = $this -> revertLiteralPlaceHolder($lineArray, $literalBlock);
531 |
532 | $this -> addArray($lineArray, $this -> indent);
533 |
534 | foreach ($this->delayedPath as $indent => $delayedPath)
535 | $this -> path[$indent] = $delayedPath;
536 |
537 | $this -> delayedPath = array();
538 |
539 | }
540 | return $this -> result;
541 | }
542 |
543 | private function loadFromSource($input) {
544 | if (!empty($input) && strpos($input, "\n") === false && file_exists($input))
545 | $input = file_get_contents($input);
546 |
547 | return $this -> loadFromString($input);
548 | }
549 |
550 | private function loadFromString($input) {
551 | $lines = explode("\n", $input);
552 | foreach ($lines as $k => $_) {
553 | $lines[$k] = rtrim($_, "\r");
554 | }
555 | return $lines;
556 | }
557 |
558 | /**
559 | * Parses YAML code and returns an array for a node
560 | * @access private
561 | * @return array
562 | * @param string $line A line from the YAML file
563 | */
564 | private function _parseLine($line) {
565 | if (!$line)
566 | return array();
567 | $line = trim($line);
568 | if (!$line)
569 | return array();
570 |
571 | $array = array();
572 |
573 | $group = $this -> nodeContainsGroup($line);
574 | if ($group) {
575 | $this -> addGroup($line, $group);
576 | $line = $this -> stripGroup($line, $group);
577 | }
578 |
579 | if ($this -> startsMappedSequence($line))
580 | return $this -> returnMappedSequence($line);
581 |
582 | if ($this -> startsMappedValue($line))
583 | return $this -> returnMappedValue($line);
584 |
585 | if ($this -> isArrayElement($line))
586 | return $this -> returnArrayElement($line);
587 |
588 | if ($this -> isPlainArray($line))
589 | return $this -> returnPlainArray($line);
590 |
591 | return $this -> returnKeyValuePair($line);
592 |
593 | }
594 |
595 | /**
596 | * Finds the type of the passed value, returns the value as the new type.
597 | * @access private
598 | * @param string $value
599 | * @return mixed
600 | */
601 | private function _toType($value) {
602 | if ($value === '')
603 | return "";
604 | $first_character = $value[0];
605 | $last_character = substr($value, -1, 1);
606 |
607 | $is_quoted = false;
608 | do {
609 | if (!$value)
610 | break;
611 | if ($first_character != '"' && $first_character != "'")
612 | break;
613 | if ($last_character != '"' && $last_character != "'")
614 | break;
615 | $is_quoted = true;
616 | } while (0);
617 |
618 | if ($is_quoted) {
619 | $value = str_replace('\n', "\n", $value);
620 | return strtr(substr($value, 1, -1), array('\\"' => '"', '\'\'' => '\'', '\\\'' => '\''));
621 | }
622 |
623 | if (strpos($value, ' #') !== false && !$is_quoted)
624 | $value = preg_replace('/\s+#(.+)$/', '', $value);
625 |
626 | if ($first_character == '[' && $last_character == ']') {
627 | // Take out strings sequences and mappings
628 | $innerValue = trim(substr($value, 1, -1));
629 | if ($innerValue === '')
630 | return array();
631 | $explode = $this -> _inlineEscape($innerValue);
632 | // Propagate value array
633 | $value = array();
634 | foreach ($explode as $v) {
635 | $value[] = $this -> _toType($v);
636 | }
637 | return $value;
638 | }
639 |
640 | if (strpos($value, ': ') !== false && $first_character != '{') {
641 | $array = explode(': ', $value);
642 | $key = trim($array[0]);
643 | array_shift($array);
644 | $value = trim(implode(': ', $array));
645 | $value = $this -> _toType($value);
646 | return array($key => $value);
647 | }
648 |
649 | if ($first_character == '{' && $last_character == '}') {
650 | $innerValue = trim(substr($value, 1, -1));
651 | if ($innerValue === '')
652 | return array();
653 | // Inline Mapping
654 | // Take out strings sequences and mappings
655 | $explode = $this -> _inlineEscape($innerValue);
656 | // Propagate value array
657 | $array = array();
658 | foreach ($explode as $v) {
659 | $SubArr = $this -> _toType($v);
660 | if (empty($SubArr))
661 | continue;
662 | if (is_array($SubArr)) {
663 | $array[key($SubArr)] = $SubArr[key($SubArr)];
664 | continue;
665 | }
666 | $array[] = $SubArr;
667 | }
668 | return $array;
669 | }
670 |
671 | if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') {
672 | return null;
673 | }
674 |
675 | if (is_numeric($value) && preg_match('/^(-|)[1-9]+[0-9]*$/', $value)) {
676 | $intvalue = (int)$value;
677 | if ($intvalue != PHP_INT_MAX)
678 | $value = $intvalue;
679 | return $value;
680 | }
681 |
682 | if (is_numeric($value) && preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) {
683 | // Hexadecimal value.
684 | return hexdec($value);
685 | }
686 |
687 | $this -> coerceValue($value);
688 |
689 | if (is_numeric($value)) {
690 | if ($value === '0')
691 | return 0;
692 | if (rtrim($value, 0) === $value)
693 | $value = (float)$value;
694 | return $value;
695 | }
696 |
697 | return $value;
698 | }
699 |
700 | /**
701 | * Used in inlines to check for more inlines or quoted strings
702 | * @access private
703 | * @return array
704 | */
705 | private function _inlineEscape($inline) {
706 | // There's gotta be a cleaner way to do this...
707 | // While pure sequences seem to be nesting just fine,
708 | // pure mappings and mappings with sequences inside can't go very
709 | // deep. This needs to be fixed.
710 |
711 | $seqs = array();
712 | $maps = array();
713 | $saved_strings = array();
714 | $saved_empties = array();
715 |
716 | // Check for empty strings
717 | $regex = '/("")|(\'\')/';
718 | if (preg_match_all($regex, $inline, $strings)) {
719 | $saved_empties = $strings[0];
720 | $inline = preg_replace($regex, 'YAMLEmpty', $inline);
721 | }
722 | unset($regex);
723 |
724 | // Check for strings
725 | $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
726 | if (preg_match_all($regex, $inline, $strings)) {
727 | $saved_strings = $strings[0];
728 | $inline = preg_replace($regex, 'YAMLString', $inline);
729 | }
730 | unset($regex);
731 |
732 | // echo $inline;
733 |
734 | $i = 0;
735 | do {
736 |
737 | // Check for sequences
738 | while (preg_match('/\[([^{}\[\]]+)\]/U', $inline, $matchseqs)) {
739 | $seqs[] = $matchseqs[0];
740 | $inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1);
741 | }
742 |
743 | // Check for mappings
744 | while (preg_match('/{([^\[\]{}]+)}/U', $inline, $matchmaps)) {
745 | $maps[] = $matchmaps[0];
746 | $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1);
747 | }
748 |
749 | if ($i++ >= 10)
750 | break;
751 |
752 | } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false);
753 |
754 | $explode = explode(',', $inline);
755 | $explode = array_map('trim', $explode);
756 | $stringi = 0;
757 | $i = 0;
758 |
759 | while (1) {
760 |
761 | // Re-add the sequences
762 | if (!empty($seqs)) {
763 | foreach ($explode as $key => $value) {
764 | if (strpos($value, 'YAMLSeq') !== false) {
765 | foreach ($seqs as $seqk => $seq) {
766 | $explode[$key] = str_replace(('YAMLSeq' . $seqk . 's'), $seq, $value);
767 | $value = $explode[$key];
768 | }
769 | }
770 | }
771 | }
772 |
773 | // Re-add the mappings
774 | if (!empty($maps)) {
775 | foreach ($explode as $key => $value) {
776 | if (strpos($value, 'YAMLMap') !== false) {
777 | foreach ($maps as $mapk => $map) {
778 | $explode[$key] = str_replace(('YAMLMap' . $mapk . 's'), $map, $value);
779 | $value = $explode[$key];
780 | }
781 | }
782 | }
783 | }
784 |
785 | // Re-add the strings
786 | if (!empty($saved_strings)) {
787 | foreach ($explode as $key => $value) {
788 | while (strpos($value, 'YAMLString') !== false) {
789 | $explode[$key] = preg_replace('/YAMLString/', $saved_strings[$stringi], $value, 1);
790 | unset($saved_strings[$stringi]);
791 | ++$stringi;
792 | $value = $explode[$key];
793 | }
794 | }
795 | }
796 |
797 | // Re-add the empties
798 | if (!empty($saved_empties)) {
799 | foreach ($explode as $key => $value) {
800 | while (strpos($value, 'YAMLEmpty') !== false) {
801 | $explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1);
802 | $value = $explode[$key];
803 | }
804 | }
805 | }
806 |
807 | $finished = true;
808 | foreach ($explode as $key => $value) {
809 | if (strpos($value, 'YAMLSeq') !== false) {
810 | $finished = false;
811 | break;
812 | }
813 | if (strpos($value, 'YAMLMap') !== false) {
814 | $finished = false;
815 | break;
816 | }
817 | if (strpos($value, 'YAMLString') !== false) {
818 | $finished = false;
819 | break;
820 | }
821 | if (strpos($value, 'YAMLEmpty') !== false) {
822 | $finished = false;
823 | break;
824 | }
825 | }
826 | if ($finished)
827 | break;
828 |
829 | $i++;
830 | if ($i > 10)
831 | break;
832 | // Prevent infinite loops.
833 | }
834 |
835 | return $explode;
836 | }
837 |
838 | private function literalBlockContinues($line, $lineIndent) {
839 | if (!trim($line))
840 | return true;
841 | if (strlen($line) - strlen(ltrim($line)) > $lineIndent)
842 | return true;
843 | return false;
844 | }
845 |
846 | private function referenceContentsByAlias($alias) {
847 | do {
848 | if (!isset($this -> SavedGroups[$alias])) { echo "Bad group name: $alias.";
849 | break;
850 | }
851 | $groupPath = $this -> SavedGroups[$alias];
852 | $value = $this -> result;
853 | foreach ($groupPath as $k) {
854 | $value = $value[$k];
855 | }
856 | } while (false);
857 | return $value;
858 | }
859 |
860 | private function addArrayInline($array, $indent) {
861 | $CommonGroupPath = $this -> path;
862 | if (empty($array))
863 | return false;
864 |
865 | foreach ($array as $k => $_) {
866 | $this -> addArray(array($k => $_), $indent);
867 | $this -> path = $CommonGroupPath;
868 | }
869 | return true;
870 | }
871 |
872 | private function addArray($incoming_data, $incoming_indent) {
873 |
874 | // print_r ($incoming_data);
875 |
876 | if (count($incoming_data) > 1)
877 | return $this -> addArrayInline($incoming_data, $incoming_indent);
878 |
879 | $key = key($incoming_data);
880 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null;
881 | if ($key === '__!YAMLZero')
882 | $key = '0';
883 |
884 | if ($incoming_indent == 0 && !$this -> _containsGroupAlias && !$this -> _containsGroupAnchor) {// Shortcut for root-level values.
885 | if ($key || $key === '' || $key === '0') {
886 | $this -> result[$key] = $value;
887 | } else {
888 | $this -> result[] = $value;
889 | end($this -> result);
890 | $key = key($this -> result);
891 | }
892 | $this -> path[$incoming_indent] = $key;
893 | return;
894 | }
895 |
896 | $history = array();
897 | // Unfolding inner array tree.
898 | $history[] = $_arr = $this -> result;
899 | foreach ($this->path as $k) {
900 | $history[] = $_arr = $_arr[$k];
901 | }
902 |
903 | if ($this -> _containsGroupAlias) {
904 | $value = $this -> referenceContentsByAlias($this -> _containsGroupAlias);
905 | $this -> _containsGroupAlias = false;
906 | }
907 |
908 | // Adding string or numeric key to the innermost level or $this->arr.
909 | if (is_string($key) && $key == '<<') {
910 | if (!is_array($_arr)) { $_arr = array();
911 | }
912 |
913 | $_arr = array_merge($_arr, $value);
914 | } else if ($key || $key === '' || $key === '0') {
915 | if (!is_array($_arr))
916 | $_arr = array($key => $value);
917 | else
918 | $_arr[$key] = $value;
919 | } else {
920 | if (!is_array($_arr)) { $_arr = array($value);
921 | $key = 0;
922 | } else { $_arr[] = $value;
923 | end($_arr);
924 | $key = key($_arr);
925 | }
926 | }
927 |
928 | $reverse_path = array_reverse($this -> path);
929 | $reverse_history = array_reverse($history);
930 | $reverse_history[0] = $_arr;
931 | $cnt = count($reverse_history) - 1;
932 | for ($i = 0; $i < $cnt; $i++) {
933 | $reverse_history[$i + 1][$reverse_path[$i]] = $reverse_history[$i];
934 | }
935 | $this -> result = $reverse_history[$cnt];
936 |
937 | $this -> path[$incoming_indent] = $key;
938 |
939 | if ($this -> _containsGroupAnchor) {
940 | $this -> SavedGroups[$this -> _containsGroupAnchor] = $this -> path;
941 | if (is_array($value)) {
942 | $k = key($value);
943 | if (!is_int($k)) {
944 | $this -> SavedGroups[$this -> _containsGroupAnchor][$incoming_indent + 2] = $k;
945 | }
946 | }
947 | $this -> _containsGroupAnchor = false;
948 | }
949 |
950 | }
951 |
952 | private static function startsLiteralBlock($line) {
953 | $lastChar = substr(trim($line), -1);
954 | if ($lastChar != '>' && $lastChar != '|')
955 | return false;
956 | if ($lastChar == '|')
957 | return $lastChar;
958 | // HTML tags should not be counted as literal blocks.
959 | if (preg_match('#<.*?>$#', $line))
960 | return false;
961 | return $lastChar;
962 | }
963 |
964 | private static function greedilyNeedNextLine($line) {
965 | $line = trim($line);
966 | if (!strlen($line))
967 | return false;
968 | if (substr($line, -1, 1) == ']')
969 | return false;
970 | if ($line[0] == '[')
971 | return true;
972 | if (preg_match('#^[^:]+?:\s*\[#', $line))
973 | return true;
974 | return false;
975 | }
976 |
977 | private function addLiteralLine($literalBlock, $line, $literalBlockStyle, $indent = -1) {
978 | $line = self::stripIndent($line, $indent);
979 | if ($literalBlockStyle !== '|') {
980 | $line = self::stripIndent($line);
981 | }
982 | $line = rtrim($line, "\r\n\t ") . "\n";
983 | if ($literalBlockStyle == '|') {
984 | return $literalBlock . $line;
985 | }
986 | if (strlen($line) == 0)
987 | return rtrim($literalBlock, ' ') . "\n";
988 | if ($line == "\n" && $literalBlockStyle == '>') {
989 | return rtrim($literalBlock, " \t") . "\n";
990 | }
991 | if ($line != "\n")
992 | $line = trim($line, "\r\n ") . " ";
993 | return $literalBlock . $line;
994 | }
995 |
996 | function revertLiteralPlaceHolder($lineArray, $literalBlock) {
997 | foreach ($lineArray as $k => $_) {
998 | if (is_array($_))
999 | $lineArray[$k] = $this -> revertLiteralPlaceHolder($_, $literalBlock);
1000 | else if (substr($_, -1 * strlen($this -> LiteralPlaceHolder)) == $this -> LiteralPlaceHolder)
1001 | $lineArray[$k] = rtrim($literalBlock, " \r\n");
1002 | }
1003 | return $lineArray;
1004 | }
1005 |
1006 | private static function stripIndent($line, $indent = -1) {
1007 | if ($indent == -1)
1008 | $indent = strlen($line) - strlen(ltrim($line));
1009 | return substr($line, $indent);
1010 | }
1011 |
1012 | private function getParentPathByIndent($indent) {
1013 | if ($indent == 0)
1014 | return array();
1015 | $linePath = $this -> path;
1016 | do {
1017 | end($linePath);
1018 | $lastIndentInParentPath = key($linePath);
1019 | if ($indent <= $lastIndentInParentPath)
1020 | array_pop($linePath);
1021 | } while ($indent <= $lastIndentInParentPath);
1022 | return $linePath;
1023 | }
1024 |
1025 | private function clearBiggerPathValues($indent) {
1026 |
1027 | if ($indent == 0)
1028 | $this -> path = array();
1029 | if (empty($this -> path))
1030 | return true;
1031 |
1032 | foreach ($this->path as $k => $_) {
1033 | if ($k > $indent)
1034 | unset($this -> path[$k]);
1035 | }
1036 |
1037 | return true;
1038 | }
1039 |
1040 | private static function isComment($line) {
1041 | if (!$line)
1042 | return false;
1043 | if ($line[0] == '#')
1044 | return true;
1045 | if (trim($line, " \r\n\t") == '---')
1046 | return true;
1047 | return false;
1048 | }
1049 |
1050 | private static function isEmpty($line) {
1051 | return (trim($line) === '');
1052 | }
1053 |
1054 | private function isArrayElement($line) {
1055 | if (!$line || !is_scalar($line))
1056 | return false;
1057 | if (substr($line, 0, 2) != '- ')
1058 | return false;
1059 | if (strlen($line) > 3)
1060 | if (substr($line, 0, 3) == '---')
1061 | return false;
1062 |
1063 | return true;
1064 | }
1065 |
1066 | private function isHashElement($line) {
1067 | return strpos($line, ':');
1068 | }
1069 |
1070 | private function isLiteral($line) {
1071 | if ($this -> isArrayElement($line))
1072 | return false;
1073 | if ($this -> isHashElement($line))
1074 | return false;
1075 | return true;
1076 | }
1077 |
1078 | private static function unquote($value) {
1079 | if (!$value)
1080 | return $value;
1081 | if (!is_string($value))
1082 | return $value;
1083 | if ($value[0] == '\'')
1084 | return trim($value, '\'');
1085 | if ($value[0] == '"')
1086 | return trim($value, '"');
1087 | return $value;
1088 | }
1089 |
1090 | private function startsMappedSequence($line) {
1091 | return (substr($line, 0, 2) == '- ' && substr($line, -1, 1) == ':');
1092 | }
1093 |
1094 | private function returnMappedSequence($line) {
1095 | $array = array();
1096 | $key = self::unquote(trim(substr($line, 1, -1)));
1097 | $array[$key] = array();
1098 | $this -> delayedPath = array(strpos($line, $key) + $this -> indent => $key);
1099 | return array($array);
1100 | }
1101 |
1102 | private function checkKeysInValue($value) {
1103 | if (strchr('[{"\'', $value[0]) === false) {
1104 | if (strchr($value, ': ') !== false) {
1105 | throw new Exception('Too many keys: ' . $value);
1106 | }
1107 | }
1108 | }
1109 |
1110 | private function returnMappedValue($line) {
1111 | $this -> checkKeysInValue($line);
1112 | $array = array();
1113 | $key = self::unquote(trim(substr($line, 0, -1)));
1114 | $array[$key] = '';
1115 | return $array;
1116 | }
1117 |
1118 | private function startsMappedValue($line) {
1119 | return (substr($line, -1, 1) == ':');
1120 | }
1121 |
1122 | private function isPlainArray($line) {
1123 | return ($line[0] == '[' && substr($line, -1, 1) == ']');
1124 | }
1125 |
1126 | private function returnPlainArray($line) {
1127 | return $this -> _toType($line);
1128 | }
1129 |
1130 | private function returnKeyValuePair($line) {
1131 | $array = array();
1132 | $key = '';
1133 | if (strpos($line, ': ')) {
1134 | // It's a key/value pair most likely
1135 | // If the key is in double quotes pull it out
1136 | if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/', $line, $matches)) {
1137 | $value = trim(str_replace($matches[1], '', $line));
1138 | $key = $matches[2];
1139 | } else {
1140 | // Do some guesswork as to the key and the value
1141 | $explode = explode(': ', $line);
1142 | $key = trim(array_shift($explode));
1143 | $value = trim(implode(': ', $explode));
1144 | $this -> checkKeysInValue($value);
1145 | }
1146 | // Set the type of the value. Int, string, etc
1147 | $value = $this -> _toType($value);
1148 | if ($key === '0')
1149 | $key = '__!YAMLZero';
1150 | $array[$key] = $value;
1151 | } else {
1152 | $array = array($line);
1153 | }
1154 | return $array;
1155 |
1156 | }
1157 |
1158 | private function returnArrayElement($line) {
1159 | if (strlen($line) <= 1)
1160 | return array( array());
1161 | // Weird %)
1162 | $array = array();
1163 | $value = trim(substr($line, 1));
1164 | $value = $this -> _toType($value);
1165 | if ($this -> isArrayElement($value)) {
1166 | $value = $this -> returnArrayElement($value);
1167 | }
1168 | $array[] = $value;
1169 | return $array;
1170 | }
1171 |
1172 | private function nodeContainsGroup($line) {
1173 | $symbolsForReference = 'A-z0-9_\-';
1174 | if (strpos($line, '&') === false && strpos($line, '*') === false)
1175 | return false;
1176 | // Please die fast ;-)
1177 | if ($line[0] == '&' && preg_match('/^(&[' . $symbolsForReference . ']+)/', $line, $matches))
1178 | return $matches[1];
1179 | if ($line[0] == '*' && preg_match('/^(\*[' . $symbolsForReference . ']+)/', $line, $matches))
1180 | return $matches[1];
1181 | if (preg_match('/(&[' . $symbolsForReference . ']+)$/', $line, $matches))
1182 | return $matches[1];
1183 | if (preg_match('/(\*[' . $symbolsForReference . ']+$)/', $line, $matches))
1184 | return $matches[1];
1185 | if (preg_match('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches))
1186 | return $matches[1];
1187 | return false;
1188 |
1189 | }
1190 |
1191 | private function addGroup($line, $group) {
1192 | if ($group[0] == '&')
1193 | $this -> _containsGroupAnchor = substr($group, 1);
1194 | if ($group[0] == '*')
1195 | $this -> _containsGroupAlias = substr($group, 1);
1196 | //print_r ($this->path);
1197 | }
1198 |
1199 | private function stripGroup($line, $group) {
1200 | $line = trim(str_replace($group, '', $line));
1201 | return $line;
1202 | }
1203 |
1204 | }
1205 |
1206 | // Enable use of Spyc from command line
1207 | // The syntax is the following: php Spyc.php spyc.yaml
1208 |
1209 | do {
1210 | if (PHP_SAPI != 'cli')
1211 | break;
1212 | if (empty($_SERVER['argc']) || $_SERVER['argc'] < 2)
1213 | break;
1214 | if (empty($_SERVER['PHP_SELF']) || FALSE === strpos($_SERVER['PHP_SELF'], 'Spyc.php'))
1215 | break;
1216 | $file = $argv[1];
1217 | echo json_encode(spyc_load_file($file));
1218 | } while (0);
1219 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
6 | * @link https://github.com/mikestowe/php-raml2html
7 | * @link http://www.mikestowe.com/2014/05/raml-2-html.php
8 | * @license http://www.gnu.org/licenses/gpl-2.0.html GPL v2
9 | */
10 |
11 | require_once('inc/spyc.php');
12 | require_once('inc/ramlDataObject.php');
13 | require_once('inc/raml.php');
14 | require_once('inc/ramlPathObject.php');
15 | require_once('inc/markdown.php');
16 | require_once('inc/codeSamples.php');
17 | require_once('config.php');
18 |
19 |
20 | // Dangling Function
21 | function formatResponse($text) {
22 | return str_replace(array(" ", "\n"), array(" ", "2 | The page you are trying to access does not exist. Please select a parent endpoint from the menu to your right. 3 |
4 | -------------------------------------------------------------------------------- /templates/grey/_action.phtml: -------------------------------------------------------------------------------- 1 | 2 |action()->get('description')); ?>
10 | 11 | 12 | action()->get('queryParameters')->isArray()): ?> 13 | 14 | Try It 15 | 16 |Parameter | Type | Required | Description | 21 |
Example: ' . $details['example'] . '' : ''); ?> | 30 |
Parameter | Type | Required | Description | 44 |
Example: ' . $details['example'] . '' : ''); ?> | 53 |
Parameter | Type | Required | Description | 67 |
Example: ' . $details['example'] . '' : ''); ?> | 81 |
", $codeSamples->javascript()); ?>
94 | ", $codeSamples->php()); ?>
95 | ", $codeSamples->rails()); ?>
96 |
97 |
109 |
110 | 130 | 133 | 134 | 135 | 136 | path()->getVerbs()) > 1): ?> 137 |
12 | 13 |
version get('version'); ?> get('baseUri'); ?>
269 |2 | The page you are trying to access does not exist. Please select a parent endpoint from the menu to your right. 3 |
4 | -------------------------------------------------------------------------------- /templates/orange/_action.phtml: -------------------------------------------------------------------------------- 1 | 2 |action()->get('description')); ?>
10 | 11 | 12 | action()->get('queryParameters')->isArray()): ?> 13 | 14 | Try It 15 | 16 |Parameter | Type | Required | Description | 21 |
Example: ' . $details['example'] . '' : ''); ?> | 30 |
Parameter | Type | Required | Description | 44 |
Example: ' . $details['example'] . '' : ''); ?> | 53 |
Parameter | Type | Required | Description | 67 |
Example: ' . $details['example'] . '' : ''); ?> | 81 |
", $codeSamples->javascript()); ?>
95 | ", $codeSamples->php()); ?>
96 | ", $codeSamples->rails()); ?>
97 |
98 |
110 |
111 | 132 | 135 | 136 | 137 | 138 | path()->getVerbs()) > 1): ?> 139 |