PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.3
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.3
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Foundation / ComponentBinder.php

ComponentBinder.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.3, at vendor/wpfluent/framework/src/WPFluent/Foundation/ComponentBinder.php

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