1
1
<?php
2
2
3
- require_once __DIR__ . '/../../ vendor/autoload.php ' ;
3
+ require_once __DIR__ . '/vendor/autoload.php ' ;
4
4
5
5
use Battis \SharedLogs \Database \Bindings \DevicesBinding ;
6
6
use Battis \SharedLogs \Database \Bindings \EntriesBinding ;
37
37
};
38
38
39
39
/* placeholders as separate arguments */
40
- $ container ['foundHandler ' ] = function () {
40
+ $ container ['foundHandler ' ] = function () {
41
41
return new RequestResponseArgs ();
42
42
};
43
43
55
55
return new UsersBinding ($ c ->pdo );
56
56
};
57
57
58
+ $ container ['cors ' ] = function ($ c ) {
59
+ return [
60
+ 'allow-origin ' => (empty ($ c ['settings ' ]['cors ' ]['allow-origin ' ])
61
+ ? ($ _SERVER ['HTTPS ' ] ? 'https:// ' : 'http:// ' ) . $ _SERVER ['SERVER_NAME ' ]
62
+ : $ c ['settings ' ]['cors ' ]['allow-origin ' ]
63
+ ),
64
+ 'allow-headers ' => (empty ($ c ['settings ' ]['cors ' ]['allow-headers ' ])
65
+ ? 'X-Requested-With, Content-Type, Accept, Origin, Authorization '
66
+ : $ c ['settings ' ]['cors ' ]['allow-headers ' ]
67
+ ),
68
+ 'allow-methods ' => (empty ($ c ['settings ' ]['cors ' ]['allow-methods ' ])
69
+ ? 'GET, POST, PUT, DELETE, OPTIONS '
70
+ : $ c ['settings ' ]['cors ' ]['allow-headers ' ]
71
+ )
72
+ ];
73
+ };
74
+
75
+ $ apiPrefix = $ container ['settings ' ]['api ' ]['prefix ' ];
76
+
58
77
/* "lazy CORS" */
59
- $ app ->options ('/{routes:.+} ' , function ($ request , $ response , $ args ) {
78
+ $ app ->options ($ apiPrefix . '/{routes:.+} ' , function ($ request , $ response , $ args ) {
60
79
return $ response ;
61
80
});
62
81
63
82
$ app ->add (function (Request $ req , Response $ res , callable $ next ) {
64
83
$ response = $ next ($ req , $ res );
65
84
return $ response
66
- ->withHeader ('Access-Control-Allow-Origin ' , ( $ _SERVER [ ' HTTPS ' ] ? ' https:// ' : ' http:// ' ) . $ _SERVER [ ' SERVER_NAME ' ])
67
- ->withHeader ('Access-Control-Allow-Headers ' , ' X-Requested-With, Content-Type, Accept, Origin, Authorization ' )
68
- ->withHeader ('Access-Control-Allow-Methods ' , ' GET, POST, PUT, DELETE, OPTIONS ' );
85
+ ->withHeader ('Access-Control-Allow-Origin ' , $ this -> cors [ ' allow-origin ' ])
86
+ ->withHeader ('Access-Control-Allow-Headers ' , $ this -> cors [ ' allow-headers ' ] )
87
+ ->withHeader ('Access-Control-Allow-Methods ' , $ this -> cors [ ' allow-methods ' ] );
69
88
});
70
89
71
- function callWithNonEmptyParams (callable $ method , ...$ params ) {
90
+ function callWithNonEmptyParams (callable $ method , ...$ params )
91
+ {
72
92
return $ method (...array_filter ($ params , function ($ param ) {
73
93
return !empty ($ param );
74
94
}));
@@ -77,7 +97,7 @@ function callWithNonEmptyParams(callable $method, ...$params) {
77
97
/*
78
98
* define routes
79
99
*/
80
- $ app ->group ('/devices ' , function () {
100
+ $ app ->group ($ apiPrefix . '/devices ' , function () {
81
101
$ this ->post ('' , function (Request $ request , Response $ response ) {
82
102
return $ response ->withJson (callWithNonEmptyParams ([$ this ->devices , 'create ' ], $ request ->getParsedBody (), $ request ->getParams ()));
83
103
});
@@ -97,7 +117,7 @@ function callWithNonEmptyParams(callable $method, ...$params) {
97
117
return $ response ->withJson (callWithNonEmptyParams ([$ this ->logs , 'listByDevice ' ], $ id , $ request ->getParams ()));
98
118
});
99
119
});
100
- $ app ->group ('/logs ' , function () {
120
+ $ app ->group ($ apiPrefix . '/logs ' , function () {
101
121
$ this ->post ('' , function (Request $ request , Response $ response ) {
102
122
return $ response ->withJson (callWithNonEmptyParams ([$ this ->logs , 'create ' ], $ request ->getParsedBody (), $ request ->getParams ()));
103
123
});
@@ -117,8 +137,8 @@ function callWithNonEmptyParams(callable $method, ...$params) {
117
137
return $ response ->withJson (callWithNonEmptyParams ([$ this ->entries , 'listByLog ' ], $ id , $ request ->getParams ()));
118
138
});
119
139
});
120
- $ app ->group ('/entries ' , function () {
121
- $ this ->post ('' , function (Request $ request , Response $ response ){
140
+ $ app ->group ($ apiPrefix . '/entries ' , function () {
141
+ $ this ->post ('' , function (Request $ request , Response $ response ) {
122
142
return $ response ->withJson (callWithNonEmptyParams ([$ this ->entries , 'create ' ], $ request ->getParsedBody (), $ request ->getParams ()));
123
143
});
124
144
$ this ->get (id_PATTERN, function (Request $ request , Response $ response , $ id ) {
@@ -131,7 +151,7 @@ function callWithNonEmptyParams(callable $method, ...$params) {
131
151
return $ response ->withJson (callWithNonEmptyParams ([$ this ->entries , 'delete ' ], $ id , $ request ->getParams ()));
132
152
});
133
153
});
134
- $ app ->group ('/users ' , function () {
154
+ $ app ->group ($ apiPrefix . '/users ' , function () {
135
155
$ this ->post ('' , function (Request $ request , Response $ response ) {
136
156
return $ response ->withJson (callWithNonEmptyParams ([$ this ->users , 'create ' ], $ request ->getParsedBody (), $ request ->getParams ()));
137
157
});
@@ -142,7 +162,7 @@ function callWithNonEmptyParams(callable $method, ...$params) {
142
162
return $ response ->withJson (callWithNonEmptyParams ([$ this ->users , 'get ' ], $ id , $ request ->getParams ()));
143
163
});
144
164
$ this ->get ('/{screen_name:\w{ ' . User::SCREEN_NAME_MINIMUM_LENGTH . ',}} ' , function (Request $ request , Response $ response , $ screen_name ) {
145
- return $ response ->withJson (callWithNonEmptyParams ([$ this ->users , 'lookupByScreenName ' ], $ screen_name , $ request ->getParams ()));
165
+ return $ response ->withJson (callWithNonEmptyParams ([$ this ->users , 'lookupByScreenName ' ], $ screen_name , $ request ->getParams ()));
146
166
});
147
167
$ this ->put (id_PATTERN, function (Request $ request , Response $ response , $ id ) {
148
168
return $ response ->withJson (callWithNonEmptyParams ([$ this ->users , 'update ' ], $ id , $ request ->getParams ()));
@@ -153,7 +173,7 @@ function callWithNonEmptyParams(callable $method, ...$params) {
153
173
});
154
174
155
175
/* finish lazy CORS */
156
- $ app ->map (['GET ' , 'POST ' , 'PUT ' , 'DELETE ' ], '/{routes:.+} ' , function ($ req , $ res ) {
176
+ $ app ->map (['GET ' , 'POST ' , 'PUT ' , 'DELETE ' ], $ apiPrefix . '/{routes:.+} ' , function ($ req , $ res ) {
157
177
$ handler = $ this ->notFoundHandler ;
158
178
return $ handler ($ req , $ res );
159
179
});
0 commit comments