-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWireframeHooks.module.php
105 lines (88 loc) · 2.88 KB
/
WireframeHooks.module.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
<?php
namespace ProcessWire;
/**
* Wireframe Hooks
*
* This is an autoloaded companion module for Wireframe and Wireframe API.
*
* @version 0.2.0
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
class WireframeHooks extends WireData implements Module {
/**
* Wireframe API instance
*
* @var null|WireframeAPI
*/
protected $api;
/**
* Is multibyte support enabled?
*
* @var bool
*/
protected $is_mb = false;
/**
* Init method
*/
protected function init() {
// hook into page not found event to provide endpoint for API
if ($this->modules->isInstalled('WireframeAPI') && $this->user->isSuperuser()) {
$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'interceptAPIRequest');
}
}
/**
* Intercept API requests
*
* @param HookEvent $event
*
* @throws WireException if required config::http404PageID doesn't exist
*/
protected function interceptAPIRequest(HookEvent $event) {
// params from event
$page = $event->arguments[0];
$url = $event->arguments[1];
// get Wireframe API module and API root
$this->api = $this->modules->get('WireframeAPI');
$api_root = $this->api->getAPIRoot();
if (empty($api_root)) return;
// check if multibyte encoding is enabled
$this->is_mb = function_exists('mb_strpos');
// compare API root with current request
if ($this->is_mb && mb_strpos($url, $api_root) !== 0 || !$this->is_mb && strpos($url, $api_root) !== 0) {
return;
}
// set page
if (($page === null || !$page->id) && $this->config->http404PageID) {
$page = $this->pages->get($this->config->http404PageID);
if (!$page->id) {
throw new WireException("config::http404PageID does not exist - please check your config");
}
}
$this->wire('page', $page);
// set system ready state
$event->object->ready();
// handle API request
$event->return = $this->renderAPIResponse($url);
$event->replace = true;
}
/**
* Render API response
*
* @param string $url
* @return string
*/
protected function renderAPIResponse(string $url): string {
// params for API query
$args = $this->input->get('args') ? json_decode($this->input->get('args'), true) : [];
if ($args === null) {
$args = [];
}
$root = $this->api->getAPIRoot();
$path = $this->is_mb ? mb_substr($url, mb_strlen($root)) : substr($url, strlen($root));
// init API, render and return API response
$this->api->init($path, $args);
$this->api->sendHeaders();
return $this->api->render();
}
}