PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.22
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.22
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.22, at vendor/wpfluent/framework/src/WPFluent/Foundation/ComponentBinder.php

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