-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.php
110 lines (94 loc) · 2.76 KB
/
api.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
107
108
109
110
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
use GravApi\Config\Constants;
/**
* Class ApiPlugin
* @package Grav\Plugin
*/
class ApiPlugin extends Plugin
{
protected $defaultBaseRoute = 'api';
protected $api;
// This will enable the plugin to extend the user/account blueprint
public $features = [
'blueprints' => 1000,
];
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
require_once __DIR__ . '/vendor/autoload.php';
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onAdminRegisterPermissions' => ['onAdminRegisterPermissions', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
// Enable the main event we are interested in
$this->enable([
'onPagesInitialized' => ['onPagesInitialized', 0]
]);
}
/**
* @param Event $e
*/
public function onPagesInitialized(Event $e)
{
$paths = $this->grav['uri']->paths();
// Check if the requested page is an intended API call, return if not
$apiRoute = trim($this->config->get('plugins.api.route'), '/');
if (!$paths || $paths[0] !== $apiRoute) {
return;
}
// We only run the API if enabled in the config
if ($this->config->get('plugins.api.enabled')) {
$this->loadApi()->run();
// We don't need Grav to do any more
exit();
}
}
/**
* Register custom API role permissions
*
* @param Event $e
*/
public function onAdminRegisterPermissions(Event $e)
{
if (isset($e['admin'])) {
$permissions = [];
foreach (Constants::ROLES as $role) {
$permissions[$role] = 'boolean';
}
$e['admin']->addPermissions($permissions);
}
}
/**
* Loads the GravApi dependencies and returns a new instance the Api app
*
* @return GravApi\Api $api
*/
public function loadApi()
{
// Load app dependencies once we know the request is for the API
require_once __DIR__ . '/src/Api.php';
return new \GravApi\Api();
}
}