forked from btopro/page-as-data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
page-as-data.php
106 lines (101 loc) · 3.04 KB
/
page-as-data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
class PageAsDataPlugin extends Plugin {
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => [
['onPluginsInitialized', 0]
],
];
}
public function onPluginsInitialized() {
// Hijack output so we can deliver as a different format
// if this value isset
if (isset($_GET['return-as']) && in_array($_GET['return-as'], array('json', 'xml', 'yaml'))) {
$this->enable([
'onPageInitialized' => ['deliverFormatAs', 0]
]);
}
}
/**
* Delivers the page as a different format.
*/
public function deliverFormatAs() {
/**
* @var \Grav\Common\Page\Page $page
*/
$format = $_GET['return-as'];
$page = $this->grav['page'];
$collection = $page->collection('children');
$pageArray = $page->toArray();
$children = array();
foreach ($collection as $item) {
$children[] = $item->toArray();
}
$pageArray['children'] = $children;
switch ($format) {
case 'json':
header("Content-Type: application/json");
echo json_encode($pageArray);
break;
case 'yaml':
header("Content-Type: application/yaml");
echo $page->toYaml();
break;
case 'xml':
header("Content-Type: application/xml");
$array2XmlConverter = new PageAsDataXmlDomConstructor('1.0', 'utf-8');
$array2XmlConverter->xmlStandalone = true;
$array2XmlConverter->formatOutput = true;
try {
$array2XmlConverter->fromMixed(array('page' => $pageArray));
$array2XmlConverter->normalizeDocument();
$xml = $array2XmlConverter->saveXML();
print $xml;
}
catch(Exception $ex) {
return $ex;
}
break;
}
exit();
}
}
/**
* Converts an array to XML
* http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/
*/
/**
* Extends the DOMDocument to implement personal (utility) methods.
* - From: http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/
* - parent:: See http://www.php.net/manual/en/class.domdocument.php
*
* @throws DOMException http://www.php.net/manual/en/class.domexception.php
*
* @author Toni Van de Voorde
*/
class PageAsDataXmlDomConstructor extends \DOMDocument {
public function fromMixed($mixed, \DOMElement $domElement = null) {
$domElement = is_null($domElement) ? $this : $domElement;
if (is_array($mixed)) {
foreach ($mixed as $index => $mixedElement) {
if (is_int($index)) {
if ($index == 0) {
$node = $domElement;
} else {
$node = $this->createElement($domElement->tagName);
$domElement->parentNode->appendChild($node);
}
} else {
$node = $this->createElement($index);
$domElement->appendChild($node);
}
$this->fromMixed($mixedElement, $node);
}
} else {
$domElement->appendChild($this->createTextNode($mixed));
}
}
}