PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.2.1
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.2.1
2.2.1 2.2.0 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 All 44 releases
← All changes | vendor/wpfluent/framework/src/WPFluent/Foundation/ComponentBinder.php +159 -80 1.21 → 2.2.1 View file →
@@ -2,20 +2,24 @@
2 2
3 3 namespace FluentBoards\Framework\Foundation;
4 4
5 5 use FluentBoards\Framework\Support\Arr;
6 -use FluentBoards\Framework\Support\Str;
7 6 use FluentBoards\Framework\View\View;
7 +use FluentBoards\Framework\Cache\Cache;
8 8 use FluentBoards\Framework\Http\URL;
9 +use FluentBoards\Framework\Http\UrlGenerator;
10 +use FluentBoards\Framework\Support\Mail;
11 +use FluentBoards\Framework\Support\Pipeline;
9 12 use FluentBoards\Framework\Http\Router;
10 -use FluentBoards\Framework\Support\Facade;
11 -use FluentBoards\Framework\Support\Pipeline;
12 13 use FluentBoards\Framework\Http\Request\Request;
13 14 use FluentBoards\Framework\Http\Response\Response;
14 15 use FluentBoards\Framework\Events\Dispatcher;
16 +use FluentBoards\Framework\Encryption\Encrypter;
15 17 use FluentBoards\Framework\Database\Orm\Model;
16 18 use FluentBoards\Framework\Validator\Validator;
17 19 use FluentBoards\Framework\Foundation\RequestGuard;
20 +use FluentBoards\Framework\Database\DatabaseManager;
21 +use FluentBoards\Framework\Database\DatabaseTransactionsManager;
18 22 use FluentBoards\Framework\Database\ConnectionResolver;
19 23 use FluentBoards\Framework\Database\Query\WPDBConnection;
20 24 use FluentBoards\Framework\Pagination\AbstractCursorPaginator;
21 25 use FluentBoards\Framework\Pagination\AbstractPaginator;
@@ -24,8 +28,10 @@
24 28 use WpOrg\Requests\Exception\Http\Status401;
25 29
26 30 class ComponentBinder
27 31 {
32 + use Concerns\DynamicFacadeTrait;
33 +
28 34 /**
29 35 * The application instance
30 36 * @var \FluentBoards\Framework\Foundation\Application
31 37 */
@@ -35,16 +41,19 @@
35 41 * List of bindings
36 42 * @var array
37 43 */
38 44 protected $bindables = [
45 + 'Cache',
39 46 'Request',
40 47 'Response',
41 48 'Validator',
42 49 'View',
43 50 'Events',
51 + 'Encrypter',
44 52 'DB',
45 53 'URL',
46 54 'Router',
55 + 'Mail',
47 56 'Paginator',
48 57 'Pipeline',
49 58 ];
50 59
@@ -76,8 +85,19 @@
76 85
77 86 $this->registerResolvingEvent($this->app);
78 87 }
79 88
89 + public function resolveDatabaseTransactionsManager()
90 + {
91 + if (!$this->app->bound('db.transactions')) {
92 + $this->app->singleton('db.transactions', function ($app) {
93 + return new DatabaseTransactionsManager;
94 + });
95 + }
96 +
97 + return $this->app->make('db.transactions');
98 + }
99 +
80 100 /**
81 101 * Register resolving event into the container.
82 102 * @param \FluentBoards\Framework\Foundation\Application $app
83 103 * @return null
@@ -84,94 +104,46 @@
84 104 */
85 105 protected function registerResolvingEvent($app)
86 106 {
87 107 $app->resolving(RequestGuard::class, function($request) use ($app) {
108 +
109 + $request->setRequestInstance($app->request);
88 110
89 111 if (method_exists($request, 'authorize')) {
90 112 if(!$request->authorize()) throw new Status401;
91 113 }
92 114
93 - $request->merge($request->beforeValidation());
115 + $request->merge((array) $request->beforeValidation());
94 116 $request->validate();
95 117 $request->merge((array) $request->afterValidation(
96 - $app->make('validator')
118 + $request->getValidator()
97 119 ));
98 120 });
99 121 }
100 122
101 123 /**
102 - * Register the dynamic facade resolver.
103 - * @param \FluentBoards\Framework\Foundation\Application $app
124 + * Bind the cache instance into the container.
104 125 * @return null
105 126 */
106 - protected function registerFacadeResolver($app)
127 + protected function bindCache()
107 128 {
108 - Facade::setFacadeApplication($app);
109 -
110 - spl_autoload_register(function($class) use ($app) {
111 -
112 - $ns = substr(($fqn = __NAMESPACE__), 0, strpos($fqn, '\\'));
113 -
114 - if (Str::contains($class, ($facade = $ns.'\Facade'))) {
115 -
116 - $this->createFacadeFor($facade, $class, $app);
117 - }
129 + $this->app->singleton(Cache::class, function ($app) {
130 + return Cache::init();
118 131 });
119 - }
120 132
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);
133 + $this->app->alias(Cache::class, 'cache');
146 134 }
147 135
148 136 /**
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 137 * Bind the request instance into the container.
168 138 * @return null
169 139 */
170 140 protected function bindRequest()
171 141 {
172 - $this->app->singleton(Request::class, function ($app) {
173 - return new Request($app, $_GET, $_POST, $_FILES);
142 + $method = $this->getBindingMethod('singleton');
143 +
144 + $this->app->$method(Request::class, function ($app) {
145 + return $this->resolveRequest($app);
174 146 });
175 147
176 148 $this->app->alias(Request::class, 'request');
177 149
@@ -183,9 +155,11 @@
183 155 * @return null
184 156 */
185 157 protected function bindResponse()
186 158 {
187 - $this->app->singleton(Response::class, function($app) {
159 + $method = $this->getBindingMethod('singleton');
160 +
161 + $this->app->$method(Response::class, function($app) {
188 162 return new Response($app);
189 163 });
190 164
191 165 $this->app->alias(Response::class, 'response');
@@ -193,8 +167,23 @@
193 167 $this->addBackwardCompatibleAlias(Response::class);
194 168 }
195 169
196 170 /**
171 + * Get the binding method to bind a component.
172 + * In the testing environment, we use bind
173 + * method instead of singleton method.
174 + *
175 + * @param string $original
176 + * @return string
177 + */
178 + protected function getBindingMethod($original)
179 + {
180 + return str_starts_with(
181 + $this->app->env(), 'testing'
182 + ) ? 'bind' : $original;
183 + }
184 +
185 + /**
197 186 * Bind the request validator into the container.
198 187 * @return null
199 188 */
200 189 protected function bindValidator()
@@ -225,9 +214,11 @@
225 214 */
226 215 protected function bindEvents()
227 216 {
228 217 $this->app->singleton(Dispatcher::class, function($app) {
229 - return new Dispatcher($app);
218 + return (new Dispatcher($app))->setTransactionManagerResolver(
219 + fn () => $this->resolveDatabaseTransactionsManager()
220 + );
230 221 });
231 222
232 223 $this->app->alias(Dispatcher::class, 'events');
233 224 }
@@ -232,33 +223,60 @@
232 223 $this->app->alias(Dispatcher::class, 'events');
233 224 }
234 225
235 226 /**
227 + * Bind the encrypter instance into the container.
228 + * @return null
229 + */
230 + protected function bindEncrypter()
231 + {
232 + $this->app->singleton(Encrypter::class, function($app) {
233 + return new Encrypter($app->config->get('app.key'));
234 + });
235 +
236 + $this->app->alias(Encrypter::class, 'encrypter');
237 + $this->app->alias(Encrypter::class, 'crypt');
238 + }
239 +
240 + /**
236 241 * Bind the db (query builder) instance into the container.
237 242 * @return null
238 243 */
239 244 protected function bindDB()
240 245 {
241 - $this->app->singleton('db', function($app) {
242 - return new WPDBConnection(
243 - $GLOBALS['wpdb'], $app->config->get('database')
244 - );
245 - });
246 + $connection = new WPDBConnection($GLOBALS['wpdb']);
246 247
248 + $resolver = new ConnectionResolver([
249 + 'mysql' => $connection,
250 + 'sqlite' => $connection,
251 + ]);
252 +
253 + $resolver->setDefaultConnection('mysql');
254 +
255 + $resolver->connection()->setTransactionManager(
256 + $this->resolveDatabaseTransactionsManager()
257 + );
258 +
259 + Model::setConnectionResolver($resolver);
260 +
247 261 Model::setEventDispatcher($this->app['events']);
248 -
249 - Model::setConnectionResolver(new ConnectionResolver);
262 +
263 + $this->app->singletonIf('db', function($app) use ($resolver) {
264 + return new DatabaseManager($resolver);
265 + });
250 266 }
251 267
252 268 /**
253 - * Bind the URL instance into the container.
269 + * Bind the Url instance into the container.
254 270 * @return null
255 271 */
256 - protected function bindURL()
272 + protected function bindUrl()
257 273 {
258 - $this->app->bind('url', function($app) {
259 - return new URL;
274 + $this->app->bind(URL::class, function($app) {
275 + return new URL('', new UrlGenerator($app));
260 276 });
277 +
278 + $this->app->alias(URL::class, 'url');
261 279 }
262 280
263 281 /**
264 282 * Bind the router instance into the container.
@@ -265,14 +283,29 @@
265 283 * @return null
266 284 */
267 285 protected function bindRouter()
268 286 {
269 - $this->app->singleton('router', function($app) {
287 + $this->app->singleton(Router::class, function($app) {
270 288 return new Router($app);
271 289 });
290 +
291 + $this->app->alias(Router::class, 'router');
272 292 }
273 293
274 294 /**
295 + * Bind the mail instance into the container.
296 + * @return null
297 + */
298 + protected function bindMail()
299 + {
300 + $this->app->bind(Mail::class, function($app) {
301 + return new Mail();
302 + });
303 +
304 + $this->app->alias(Mail::class, 'mail');
305 + }
306 +
307 + /**
275 308 * Bind the paginator instance into the container.
276 309 * @return null
277 310 */
278 311 protected function bindPaginator()
@@ -309,9 +342,9 @@
309 342 $this->app->bind(Pipeline::class, function ($app) {
310 343 return new Pipeline($app);
311 344 });
312 345
313 - $this->app->alias(Pipeline::class, 'pipeline');
346 + $this->app->alias(Pipeline::class, 'pipeline');
314 347 }
315 348
316 349 /**
317 350 * Load other bindings the developers might
@@ -343,9 +376,9 @@
343 376 }
344 377 }
345 378
346 379 /**
347 - * Adds new alias to maintain ther backward compatibility.
380 + * Adds new alias to maintain the backward compatibility.
348 381 *
349 382 * @param string $class
350 383 * @return void
351 384 */
@@ -374,6 +407,52 @@
374 407 unset($pieces[$index]);
375 408 }
376 409
377 410 return implode('\\', $pieces);
411 + }
412 +
413 + /**
414 + * Resolve the appropriate request instance.
415 + *
416 + * @param $app
417 + * @return Request|object (Anonymous Class)
418 + */
419 + protected function resolveRequest($app)
420 + {
421 + return new Request($app, $_GET, $_POST);
422 + }
423 +
424 + /**
425 + * Check if the request is of the plugin.
426 + *
427 + * @return boolean
428 + */
429 + protected function isRequestOfPlugin()
430 + {
431 + if (str_starts_with($this->app->env(), 'testing')) {
432 + return true;
433 + }
434 +
435 + $slug = $this->app->config->get('app.slug');
436 +
437 + if (get_option('permalink_structure')) {
438 + $route = $_SERVER['REQUEST_URI'] ?? '';
439 + } else {
440 + $route = $_GET['rest_route'] ?? '';
441 + }
442 +
443 + $parsedUrl = parse_url($route);
444 +
445 + $path = $parsedUrl['path'] ?? '';
446 +
447 + $path = str_replace('/wp-json', '', $path);
448 +
449 + if (is_admin()) {
450 + $page = $_GET['page'] ?? '';
451 + if ($slug === $page) {
452 + $path = $page;
453 + }
454 + }
455 +
456 + return str_starts_with(ltrim($path, '/'), $slug);
378 457 }
379 458 }