-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCrudPlugin.php
More file actions
executable file
·163 lines (145 loc) · 4.61 KB
/
CrudPlugin.php
File metadata and controls
executable file
·163 lines (145 loc) · 4.61 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace MixerApi\Crud;
use Cake\Core\BasePlugin;
use Cake\Core\ContainerInterface;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\Http\Exception\MethodNotAllowedException;
/**
* Plugin for Crud
*
* @experimental
*/
class CrudPlugin extends BasePlugin
{
protected ?string $name = 'MixerApi/Crud';
/**
* Console middleware
*
* @var bool
*/
protected bool $consoleEnabled = false;
/**
* Enable middleware
*
* @var bool
*/
protected bool $middlewareEnabled = false;
/**
* Load routes or not
*
* @var bool
*/
protected bool $routesEnabled = false;
/**
* Enforce request->allowMethod() for the follow action/http-method pair.
*
* To alter: Set $options['allowedMethods'] to the mapping of your choice
* To disable: Set $options['allowedMethods'] to an empty array to turn this functionality off.
*
* @var array
*/
private array $allowedMethods = [
'add' => ['post'],
'index' => ['get'],
'view' => ['get'],
'edit' => ['patch','put','patch'],
'delete' => ['delete'],
];
/**
* Should viewVars be serialized automatically? Defaults to true, set to false to disable.
*
* @var bool
*/
private bool $doSerialize = true;
/**
* See this class for allowed $options
*
* @param array $options options
*/
public function __construct(array $options = [])
{
parent::__construct($options);
$this->allowedMethods = $options['allowedMethods'] ?? $this->allowedMethods;
$this->doSerialize = $options['doSerialize'] ?? $this->doSerialize;
}
/**
* Load all the plugin configuration and bootstrap logic.
*
* The host application is provided as an argument. This allows you to load
* additional plugin dependencies, or attach events.
*
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
$this->allowedMethodsEvent();
$this->serializeEvent();
}
/**
* Register application container services.
*
* @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
*/
public function services(ContainerInterface $container): void
{
/** @var \League\Container\Container $container */
$container->addServiceProvider(new CrudServiceProvider());
}
/**
* Registers listener to enforce allowed methods
*
* @return void
*/
private function allowedMethodsEvent(): void
{
if (empty($this->allowedMethods)) {
return;
}
EventManager::instance()->on('Controller.initialize', function (Event $event) {
/** @var \Cake\Controller\Controller $controller */
$controller = $event->getSubject();
$action = $controller->getRequest()->getParam('action');
if (is_array($this->allowedMethods) && isset($this->allowedMethods[$action])) {
try {
$controller->getRequest()->allowMethod($this->allowedMethods[$action]);
} catch (MethodNotAllowedException $e) {
throw new MethodNotAllowedException(
'Method Not Allowed. Must be one of: ' . implode($this->allowedMethods[$action])
);
}
}
});
}
/**
* Register listener for automatic serialization on all responses with a status code in the 200-299 range.
*
* @return void
*/
private function serializeEvent(): void
{
if (!$this->doSerialize) {
return;
}
EventManager::instance()->on('Controller.beforeRender', function (Event $event) {
/** @var \Cake\Controller\Controller $controller */
$controller = $event->getSubject();
$action = $controller->getRequest()->getParam('action');
if (!in_array($action, ['index', 'view', 'add', 'edit', 'delete'])) {
return;
}
if ($controller->getResponse()->getStatusCode() >= 300) {
return;
}
$keys = array_keys($controller->viewBuilder()->getVars());
if (!empty($keys)) {
$controller->viewBuilder()->setOption('serialize', reset($keys));
}
});
}
}