-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiExamplePlugin.php
113 lines (98 loc) · 3.63 KB
/
ApiExamplePlugin.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
111
112
113
<?php
/**
* @file plugins/generic/apiExample/ApiExamplePlugin.php
*
* Copyright (c) 2023 Simon Fraser University
* Copyright (c) 2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ApiExamplePlugin
*
* @ingroup plugins_generic_apiExample
*
* @brief A simple example plugin to demonstrate how to implement API controller
* or extends an existing api endpoint at plugin level so that plugins
* can have own api endpoints to tap into the existing collection of
* endpoints.
*/
namespace APP\plugins\generic\apiExample;
use APP\plugins\generic\apiExample\api\v1\users\PKPOverriddenUserController;
use Illuminate\Http\Request as IlluminateRequest;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use PKP\core\PKPBaseController;
use PKP\handler\APIHandler;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\security\Role;
class ApiExamplePlugin extends GenericPlugin
{
public function __construct()
{
parent::__construct();
}
/**
* Load and initialize the plug-in and register plugin hooks.
*
* @param string $category Name of category plugin was registered to
* @param string $path The path the plugin was found in
* @param int $mainContextId To identify if the plugin is enabled
*
* @return bool True/False value by which it's determined if plugin will be registered or not
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if (!$success || !$this->getEnabled()) {
return $success;
}
// add/inject new routes/endpoints to an existing collection/list of api end points
$this->addRoute();
return $success;
}
/**
* Add/override new api endpoints to existing list of api endpoints
*/
public function addRoute(): void
{
Hook::add('APIHandler::endpoints::users', function(string $hookName, PKPBaseController &$apiController, APIHandler $apiHandler): bool {
// This allow to add a route on fly without defining a api controller
// Through this allow quick add/modify routes, it's better to use
// controller based appraoch which is more structured and understandable
$apiHandler->addRoute(
'GET',
'testing/routes/add/onfly',
function (IlluminateRequest $request): JsonResponse {
return response()->json([
'message' => 'A new route added successfully on fly',
], Response::HTTP_OK);
},
'test.onfly',
[
Role::ROLE_ID_SITE_ADMIN,
Role::ROLE_ID_MANAGER,
Role::ROLE_ID_SUB_EDITOR,
]
);
// This allow to update the api controller directly with an overrided controller
// that extends a core controller where one or more routes can be added or
// multiple existing routes can be modified
$apiController = new PKPOverriddenUserController();
return false;
});
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.apiExample.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.apiExample.description');
}
}