PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.41
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.41
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Foundation / ComponentBinder.php

ComponentBinder.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.41, at vendor/wpfluent/framework/src/WPFluent/Foundation/ComponentBinder.php

432 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Foundation;
4
5 use FluentBoards\Framework\Support\Arr;
6 use FluentBoards\Framework\View\View;
7 use FluentBoards\Framework\Cache\Cache;
8 use FluentBoards\Framework\Http\URL;
9 use FluentBoards\Framework\Http\Router;
10 use FluentBoards\Framework\Support\Facade;
11 use FluentBoards\Framework\Support\Pipeline;
12 use FluentBoards\Framework\Http\Request\Request;
13 use FluentBoards\Framework\Http\Response\Response;
14 use FluentBoards\Framework\Events\Dispatcher;
15 use FluentBoards\Framework\Encryption\Encrypter;
16 use FluentBoards\Framework\Database\Orm\Model;
17 use FluentBoards\Framework\Validator\Validator;
18 use FluentBoards\Framework\Foundation\RequestGuard;
19 use FluentBoards\Framework\Database\ConnectionResolver;
20 use FluentBoards\Framework\Database\Query\WPDBConnection;
21 use FluentBoards\Framework\Pagination\AbstractCursorPaginator;
22 use FluentBoards\Framework\Pagination\AbstractPaginator;
23 use FluentBoards\Framework\Pagination\CursorPaginator;
24 use FluentBoards\Framework\Pagination\Cursor;
25 use WpOrg\Requests\Exception\Http\Status401;
26
27 class ComponentBinder
28 {
29 /**
30 * The application instance
31 * @var \FluentBoards\Framework\Foundation\Application
32 */
33 protected $app = null;
34
35 /**
36 * List of bindings
37 * @var array
38 */
39 protected $bindables = [
40 'Cache',
41 'Request',
42 'Response',
43 'Validator',
44 'View',
45 'Events',
46 'Encrypter',
47 'DB',
48 'URL',
49 'Router',
50 'Paginator',
51 'Pipeline',
52 ];
53
54 /**
55 * Construct the binder
56 * @param \FluentBoards\Framework\Foundation\Application $app
57 */
58 public function __construct($app)
59 {
60 $this->registerFacadeResolver(
61 $this->app = $app
62 );
63 }
64
65 /**
66 * Bind all the components in to the container.
67 * @return null
68 */
69 public function bindComponents()
70 {
71 foreach ($this->bindables as $value) {
72 $method = "bind{$value}";
73 $this->{$method}();
74 }
75
76 $this->extendBindings($this->app);
77
78 $this->loadGlobalFunctions($this->app);
79
80 $this->registerResolvingEvent($this->app);
81 }
82
83 /**
84 * Register resolving event into the container.
85 * @param \FluentBoards\Framework\Foundation\Application $app
86 * @return null
87 */
88 protected function registerResolvingEvent($app)
89 {
90 $app->resolving(RequestGuard::class, function($request) use ($app) {
91
92 if (method_exists($request, 'authorize')) {
93 if(!$request->authorize()) throw new Status401;
94 }
95
96 $request->merge($request->beforeValidation());
97 $request->validate();
98 $request->merge((array) $request->afterValidation(
99 $app->make('validator')
100 ));
101 });
102 }
103
104 /**
105 * Register the dynamic facade resolver.
106 * @param \FluentBoards\Framework\Foundation\Application $app
107 * @return null
108 */
109 protected function registerFacadeResolver($app)
110 {
111 Facade::setFacadeApplication($app);
112
113 spl_autoload_register(function($class) use ($app) {
114
115 $ns = substr(($fqn = __NAMESPACE__), 0, strpos($fqn, '\\'));
116
117 if (str_contains($class, ($facade = $ns.'\Facade'))) {
118 $this->createFacadeFor($facade, $class, $app);
119 }
120 }, true, true);
121 }
122
123 /**
124 * Create a facade resolver class dynamically
125 * @param string $facade
126 * @param string $class
127 * @param \FluentBoards\Framework\Foundation\Application $app
128 * @return null
129 */
130 protected function createFacadeFor($facade, $class, $app)
131 {
132 $facadeAccessor = $this->resolveFacadeAccessor($facade, $class, $app);
133
134 $anonymousClass = new class($facadeAccessor) extends Facade {
135
136 protected static $facadeAccessor;
137
138 public function __construct($facadeAccessor) {
139 static::$facadeAccessor = $facadeAccessor;
140 }
141
142 protected static function getFacadeAccessor() {
143 return static::$facadeAccessor;
144 }
145 };
146
147 class_alias(get_class($anonymousClass), $class, true);
148 }
149
150 /**
151 * Resolve the binding name.
152 * @param string $facade
153 * @param string $class
154 * @param \FluentBoards\Framework\Foundation\Application $app
155 * @return string
156 */
157 protected function resolveFacadeAccessor($facade, $class,$app)
158 {
159 $name = strtolower(trim(str_replace($facade, '', $class), '\\'));
160
161 if ($name == 'route') $name = 'router';
162
163 if ($app->bound($name)) {
164 return $name;
165 }
166 }
167
168 /**
169 * Bind the cache instance into the container.
170 * @return null
171 */
172 protected function bindCache()
173 {
174 $this->app->singleton(Cache::class, function ($app) {
175 return Cache::init();
176 });
177
178 $this->app->alias(Cache::class, 'cache');
179 }
180
181 /**
182 * Bind the request instance into the container.
183 * @return null
184 */
185 protected function bindRequest()
186 {
187 $method = $this->getBindingMethod('singleton');
188
189 $this->app->$method(Request::class, function ($app) {
190 return new Request($app, $_GET, $_POST, $_FILES);
191 });
192
193 $this->app->alias(Request::class, 'request');
194
195 $this->addBackwardCompatibleAlias(Request::class);
196 }
197
198 /**
199 * Bind the reesponse instance into the container.
200 * @return null
201 */
202 protected function bindResponse()
203 {
204 $method = $this->getBindingMethod('singleton');
205
206 $this->app->$method(Response::class, function($app) {
207 return new Response($app);
208 });
209
210 $this->app->alias(Response::class, 'response');
211
212 $this->addBackwardCompatibleAlias(Response::class);
213 }
214
215 /**
216 * Get the binding method to bind a component.
217 * In the testing environment, we use bind
218 * method instead of singleton method.
219 *
220 * @param string $original
221 * @return string
222 */
223 protected function getBindingMethod($original)
224 {
225 return str_starts_with(
226 $this->app->env(), 'testing'
227 ) ? 'bind' : $original;
228 }
229
230 /**
231 * Bind the request validator into the container.
232 * @return null
233 */
234 protected function bindValidator()
235 {
236 $this->app->bind(Validator::class, function($app) {
237 return new Validator;
238 });
239
240 $this->app->alias(Validator::class, 'validator');
241 }
242
243 /**
244 * Bind the view instance into the container.
245 * @return null
246 */
247 protected function bindView()
248 {
249 $this->app->bind(View::class, function($app) {
250 return new View($app);
251 });
252
253 $this->app->alias(View::class, 'view');
254 }
255
256 /**
257 * Bind the event dispatcher instance into the container.
258 * @return null
259 */
260 protected function bindEvents()
261 {
262 $this->app->singleton(Dispatcher::class, function($app) {
263 return new Dispatcher($app);
264 });
265
266 $this->app->alias(Dispatcher::class, 'events');
267 }
268
269 /**
270 * Bind the encrypter instance into the container.
271 * @return null
272 */
273 protected function bindEncrypter()
274 {
275 $this->app->singleton(Encrypter::class, function($app) {
276 return new Encrypter($app->config->get('app.key'));
277 });
278
279 $this->app->alias(Encrypter::class, 'encrypter');
280 $this->app->alias(Encrypter::class, 'crypt');
281 }
282
283 /**
284 * Bind the db (query builder) instance into the container.
285 * @return null
286 */
287 protected function bindDB()
288 {
289 $this->app->singleton('db', function($app) {
290 return new WPDBConnection(
291 $GLOBALS['wpdb'], $app->config->get('database')
292 );
293 });
294
295 Model::setEventDispatcher($this->app['events']);
296
297 Model::setConnectionResolver(new ConnectionResolver);
298 }
299
300 /**
301 * Bind the URL instance into the container.
302 * @return null
303 */
304 protected function bindURL()
305 {
306 $this->app->bind(URL::class, function($app) {
307 return new URL($app->make(Encrypter::class));
308 });
309
310 $this->app->alias(URL::class, 'url');
311 }
312
313 /**
314 * Bind the router instance into the container.
315 * @return null
316 */
317 protected function bindRouter()
318 {
319 $this->app->singleton(Router::class, function($app) {
320 return new Router($app);
321 });
322
323 $this->app->alias(Router::class, 'router');
324 }
325
326 /**
327 * Bind the paginator instance into the container.
328 * @return null
329 */
330 protected function bindPaginator()
331 {
332 AbstractPaginator::currentPathResolver(function () {
333 return $this->app['request']->url();
334 });
335
336 AbstractPaginator::currentPageResolver(function ($pageName = 'page') {
337 $page = $this->app['request']->get($pageName);
338
339 if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) {
340 return $page;
341 }
342
343 return 1;
344 });
345
346 AbstractPaginator::queryStringResolver(function () {
347 return $this->app['request']->query();
348 });
349
350 AbstractCursorPaginator::currentCursorResolver(function ($cursorName = 'cursor') {
351 return Cursor::fromEncoded($this->app['request']->get($cursorName));
352 });
353 }
354
355 /**
356 * Bind the pipeline instance into the container.
357 * @return null
358 */
359 protected function bindPipeline()
360 {
361 $this->app->bind(Pipeline::class, function ($app) {
362 return new Pipeline($app);
363 });
364
365 $this->app->alias(Pipeline::class, 'pipeline');
366 }
367
368 /**
369 * Load other bindings the developers might
370 * have added in the application level.
371 *
372 * @param \FluentBoards\Framework\Foundation\Application $app
373 * @return null
374 */
375 protected function extendBindings($app)
376 {
377 $bindings = $app['path'] . 'boot/bindings.php';
378
379 if (is_readable($bindings)) {
380 require_once $bindings;
381 }
382 }
383
384 /**
385 * Load the plugin's global functions
386 * @param \FluentBoards\Framework\Foundation\Application $app
387 * @return null
388 */
389 protected function loadGlobalFunctions($app)
390 {
391 $globals = $app['path'] . 'boot/globals.php';
392
393 if (is_readable($globals)) {
394 require_once $globals;
395 }
396 }
397
398 /**
399 * Adds new alias to maintain the backward compatibility.
400 *
401 * @param string $class
402 * @return void
403 */
404 protected function addBackwardCompatibleAlias($class)
405 {
406 $this->app->alias(
407 $class, $alias = $this->getAlias($class)
408 );
409
410 if (!class_exists($alias)) {
411 class_alias($class, $alias);
412 }
413 }
414
415 /**
416 * Resolves the backward compatible alias.
417 *
418 * @param string $class
419 * @return string New alias
420 */
421 protected function getAlias($class)
422 {
423 $pieces = explode('\\', $class);
424
425 if ($index = Arr::findPath($pieces, 'Http')) {
426 unset($pieces[$index]);
427 }
428
429 return implode('\\', $pieces);
430 }
431 }
432