This repository has been archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
RestApi.module
91 lines (71 loc) · 2.97 KB
/
RestApi.module
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
<?php namespace ProcessWire;
require_once __DIR__ . "/Router.php";
// set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
// $message = "Error: $errstr. File: $errfile:$errline";
// \TD::fireLog('EXCEPTION: ' . $message);
// // if(wire('config')->debug === true) throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
// // else {
// // wire('log')->save('api-error', $message);
// // throw new \Exception('Error. If you are a system administrator, please check logs', 500);
// // }
// });
// \set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
// echo "error";
// exit();
// });
class RestApi extends WireData implements Module {
// public function __construct() {
// echo "construct";
// set_error_handler(array($this, 'handleErrorCallback'));
// set_exception_handler(array($this, 'handleErrorCallback'));
// }
public function init() {
$this->addHookBefore('ProcessPageView::execute', $this, 'checkIfApiRequest');
// Let endpoint fall back to 'api' if not set:
if(!$this->endpoint) $this->endpoint = 'api';
}
public function ready () {
}
public function checkIfApiRequest(HookEvent $event) {
$url = $this->sanitizer->url($this->input->url);
// support / in endpoint url:
$endpoint = str_replace("/", "\/", $this->endpoint);
$regex = '/^\/'.$endpoint.'\/?.*/m';
preg_match($regex, $url, $matches);
if($matches) {
Router::go();
$event->replace = true;
}
}
public function ___install() {
$apiPath = "{$this->config->paths->site}api";
$routesPath = "{$this->config->paths->site}api/Routes.php";
$examplesPath = "{$this->config->paths->site}api/Example.php";
if (!file_exists($apiPath)) {
$this->files->mkdir("{$this->config->paths->site}api");
$this->message("$this->className: Created api directory: $apiPath");
}
if (!file_exists($routesPath)) {
$this->files->copy(__DIR__ . "/apiTemplate/Routes.php", $routesPath);
$this->message("$this->className: Created Routes.php in: $routesPath");
}
if (!file_exists($examplesPath)) {
$this->files->copy(__DIR__ . "/apiTemplate/Example.php", $examplesPath);
$this->message("$this->className: Created Example.php in: $examplesPath");
}
}
public function ___uninstall() {
$this->message("$this->className: You need to remove the site/api folder yourself if you're not planning on using it anymore");
}
public function ___upgrade($fromVersion, $toVersion) {
// set authMethod to jwt if it was used before on upgrade to 0.0.3:
if(version_compare($fromVersion, "0.0.3") === -1) {
if($this->useJwtAuth) {
$data = $this->modules->getConfig($this->className);
$data['authMethod'] = 'jwt';
$this->modules->saveConfig($this->className, $data);
$this->message("$this->className: Automatically set Auth Method to 'JWT' since you used JWT Auth before");
}
}
}
}