forked from tastyigniter/ti-ext-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.php
executable file
·175 lines (153 loc) · 6.08 KB
/
Extension.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
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
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Igniter\Api;
use Admin\Models\Customers_model;
use Admin\Models\Users_model;
use Event;
use Igniter\Api\Exception\ExceptionHandler;
use Igniter\Api\Models\Token;
use Igniter\Flame\Database\Model;
use Illuminate\Contracts\Http\Kernel;
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
use Laravel\Sanctum\Sanctum;
use System\Classes\BaseExtension;
/**
* Api Extension Information File
*/
class Extension extends BaseExtension
{
public function register()
{
$this->mergeConfigFrom(__DIR__.'/config/api.php', 'api');
$this->mergeConfigFrom(__DIR__.'/config/sanctum.php', 'sanctum');
Sanctum::usePersonalAccessTokenModel(Token::class);
$this->registerResponseFactory();
$this->registerConsoleCommand('create.apiresource', \Igniter\Api\Console\CreateApiResource::class);
$this->registerConsoleCommand('issue.apitoken', \Igniter\Api\Console\IssueApiToken::class);
$this->registerExceptionHandler();
}
public function boot()
{
$this->sanctumConfigureAuthModels();
$this->sanctumConfigureMiddleware();
}
public function registerNavigation()
{
return [
'tools' => [
'child' => [
'resources' => [
'priority' => 2,
'class' => 'api-resources',
'href' => admin_url('igniter/api/resources'),
'title' => 'APIs',
'permission' => 'Igniter.Api',
],
],
],
];
}
public function registerPermissions()
{
return [
'Igniter.Api.Manage' => [
'description' => 'Create, modify and delete api resources',
'group' => 'module',
],
];
}
public function registerApiResources()
{
return [
'categories' => [
'name' => 'Categories',
'description' => 'An API resource for categories',
'model' => \Admin\Models\Categories_model::class,
'controller' => \Igniter\Api\ApiResources\Categories::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\CategoryTransformer::class,
],
'customers' => [
'name' => 'Customers',
'description' => 'An API resource for customers',
'model' => \Admin\Models\Customers_model::class,
'controller' => \Igniter\Api\ApiResources\Customers::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\CustomerTransformer::class,
],
'locations' => [
'name' => 'Locations',
'description' => 'An API resource for locations',
'model' => \Admin\Models\Locations_model::class,
'controller' => \Igniter\Api\ApiResources\Locations::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\LocationTransformer::class,
],
'menus' => [
'name' => 'Menus',
'description' => 'An API resource for menus',
'model' => \Admin\Models\Menus_model::class,
'controller' => \Igniter\Api\ApiResources\Menus::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\MenuTransformer::class,
],
'orders' => [
'name' => 'Orders',
'description' => 'An API resource for orders',
'model' => \Admin\Models\Orders_model::class,
'controller' => \Igniter\Api\ApiResources\Orders::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\OrderTransformer::class,
],
'reservations' => [
'name' => 'Reservations',
'description' => 'An API resource for reservations',
'model' => \Admin\Models\Reservations_model::class,
'controller' => \Igniter\Api\ApiResources\Reservations::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\ReservationTransformer::class,
],
'reviews' => [
'name' => 'Reviews',
'description' => 'An API resource for reviews',
'model' => \Admin\Models\Reviews_model::class,
'controller' => \Igniter\Api\ApiResources\Reviews::class,
'transformer' => \Igniter\Api\ApiResources\Transformers\ReviewTransformer::class,
],
];
}
/**
* Register the response factory.
*
* @return void
*/
protected function registerResponseFactory()
{
$this->app->alias('api.response', \Igniter\Api\Classes\ResponseFactory::class);
$this->app->singleton('api.response', function ($app) {
return new \Igniter\Api\Classes\ResponseFactory();
});
}
protected function registerExceptionHandler()
{
Event::listen('exception.beforeRender', function ($exception, $httpCode, $request) {
if (!$request->is('api/*'))
return;
$format = $this->app['config']->get('api.errorFormat');
$handler = new ExceptionHandler($format);
return $handler->handleException($exception);
});
}
/**
* Configure the Sanctum middleware and priority.
*
* @return void
*/
protected function sanctumConfigureMiddleware()
{
$kernel = $this->app->make(Kernel::class);
$kernel->prependToMiddlewarePriority(EnsureFrontendRequestsAreStateful::class);
}
protected function sanctumConfigureAuthModels()
{
Users_model::extend(function (Model $model) {
$model->relation['morphMany']['tokens'] = [Sanctum::$personalAccessTokenModel, 'name' => 'tokenable', 'delete' => TRUE];
});
Customers_model::extend(function (Model $model) {
$model->relation['morphMany']['tokens'] = [Sanctum::$personalAccessTokenModel, 'name' => 'tokenable', 'delete' => TRUE];
});
}
}