| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
use FluentBooking\Framework\View\View; |
| 7 |
use FluentBooking\Framework\Cache\Cache; |
| 8 |
use FluentBooking\Framework\Http\URL; |
| 9 |
use FluentBooking\Framework\Http\UrlGenerator; |
| 10 |
use FluentBooking\Framework\Support\Mail; |
| 11 |
use FluentBooking\Framework\Support\Pipeline; |
| 12 |
use FluentBooking\Framework\Http\Router; |
| 13 |
use FluentBooking\Framework\Http\Request\Request; |
| 14 |
use FluentBooking\Framework\Http\Response\Response; |
| 15 |
use FluentBooking\Framework\Events\Dispatcher; |
| 16 |
use FluentBooking\Framework\Encryption\Encrypter; |
| 17 |
use FluentBooking\Framework\Database\Orm\Model; |
| 18 |
use FluentBooking\Framework\Validator\Validator; |
| 19 |
use FluentBooking\Framework\Foundation\RequestGuard; |
| 20 |
use FluentBooking\Framework\Database\DatabaseManager; |
| 21 |
use FluentBooking\Framework\Database\DatabaseTransactionsManager; |
| 22 |
use FluentBooking\Framework\Database\ConnectionResolver; |
| 23 |
use FluentBooking\Framework\Database\Query\WPDBConnection; |
| 24 |
use FluentBooking\Framework\Pagination\AbstractCursorPaginator; |
| 25 |
use FluentBooking\Framework\Pagination\AbstractPaginator; |
| 26 |
use FluentBooking\Framework\Pagination\CursorPaginator; |
| 27 |
use FluentBooking\Framework\Pagination\Cursor; |
| 28 |
use WpOrg\Requests\Exception\Http\Status401; |
| 29 |
|
| 30 |
class ComponentBinder |
| 31 |
{ |
| 32 |
use Concerns\DynamicFacadeTrait; |
| 33 |
|
| 34 |
/** |
| 35 |
* The application instance |
| 36 |
* @var \FluentBooking\Framework\Foundation\Application |
| 37 |
*/ |
| 38 |
protected $app = null; |
| 39 |
|
| 40 |
/** |
| 41 |
* List of bindings |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
protected $bindables = [ |
| 45 |
'Cache', |
| 46 |
'Request', |
| 47 |
'Response', |
| 48 |
'Validator', |
| 49 |
'View', |
| 50 |
'Events', |
| 51 |
'Encrypter', |
| 52 |
'DB', |
| 53 |
'URL', |
| 54 |
'Router', |
| 55 |
'Mail', |
| 56 |
'Paginator', |
| 57 |
'Pipeline', |
| 58 |
]; |
| 59 |
|
| 60 |
/** |
| 61 |
* Construct the binder |
| 62 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 63 |
*/ |
| 64 |
public function __construct($app) |
| 65 |
{ |
| 66 |
$this->registerFacadeResolver( |
| 67 |
$this->app = $app |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Bind all the components in to the container. |
| 73 |
* @return null |
| 74 |
*/ |
| 75 |
public function bindComponents() |
| 76 |
{ |
| 77 |
foreach ($this->bindables as $value) { |
| 78 |
$method = "bind{$value}"; |
| 79 |
$this->{$method}(); |
| 80 |
} |
| 81 |
|
| 82 |
$this->extendBindings($this->app); |
| 83 |
|
| 84 |
$this->loadGlobalFunctions($this->app); |
| 85 |
|
| 86 |
$this->registerResolvingEvent($this->app); |
| 87 |
} |
| 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 |
|
| 100 |
/** |
| 101 |
* Register resolving event into the container. |
| 102 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 103 |
* @return null |
| 104 |
*/ |
| 105 |
protected function registerResolvingEvent($app) |
| 106 |
{ |
| 107 |
$app->resolving(RequestGuard::class, function($request) use ($app) { |
| 108 |
|
| 109 |
$request->setRequestInstance($app->request); |
| 110 |
|
| 111 |
if (method_exists($request, 'authorize')) { |
| 112 |
if(!$request->authorize()) throw new Status401; |
| 113 |
} |
| 114 |
|
| 115 |
$request->merge((array) $request->beforeValidation()); |
| 116 |
$request->validate(); |
| 117 |
$request->merge((array) $request->afterValidation( |
| 118 |
$request->getValidator() |
| 119 |
)); |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Bind the cache instance into the container. |
| 125 |
* @return null |
| 126 |
*/ |
| 127 |
protected function bindCache() |
| 128 |
{ |
| 129 |
$this->app->singleton(Cache::class, function ($app) { |
| 130 |
return Cache::init(); |
| 131 |
}); |
| 132 |
|
| 133 |
$this->app->alias(Cache::class, 'cache'); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Bind the request instance into the container. |
| 138 |
* @return null |
| 139 |
*/ |
| 140 |
protected function bindRequest() |
| 141 |
{ |
| 142 |
$method = $this->getBindingMethod('singleton'); |
| 143 |
|
| 144 |
$this->app->$method(Request::class, function ($app) { |
| 145 |
return $this->resolveRequest($app); |
| 146 |
}); |
| 147 |
|
| 148 |
$this->app->alias(Request::class, 'request'); |
| 149 |
|
| 150 |
$this->addBackwardCompatibleAlias(Request::class); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Bind the reesponse instance into the container. |
| 155 |
* @return null |
| 156 |
*/ |
| 157 |
protected function bindResponse() |
| 158 |
{ |
| 159 |
$method = $this->getBindingMethod('singleton'); |
| 160 |
|
| 161 |
$this->app->$method(Response::class, function($app) { |
| 162 |
return new Response($app); |
| 163 |
}); |
| 164 |
|
| 165 |
$this->app->alias(Response::class, 'response'); |
| 166 |
|
| 167 |
$this->addBackwardCompatibleAlias(Response::class); |
| 168 |
} |
| 169 |
|
| 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 |
/** |
| 186 |
* Bind the request validator into the container. |
| 187 |
* @return null |
| 188 |
*/ |
| 189 |
protected function bindValidator() |
| 190 |
{ |
| 191 |
$this->app->bind(Validator::class, function($app) { |
| 192 |
return new Validator; |
| 193 |
}); |
| 194 |
|
| 195 |
$this->app->alias(Validator::class, 'validator'); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Bind the view instance into the container. |
| 200 |
* @return null |
| 201 |
*/ |
| 202 |
protected function bindView() |
| 203 |
{ |
| 204 |
$this->app->bind(View::class, function($app) { |
| 205 |
return new View($app); |
| 206 |
}); |
| 207 |
|
| 208 |
$this->app->alias(View::class, 'view'); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Bind the event dispatcher instance into the container. |
| 213 |
* @return null |
| 214 |
*/ |
| 215 |
protected function bindEvents() |
| 216 |
{ |
| 217 |
$this->app->singleton(Dispatcher::class, function($app) { |
| 218 |
return (new Dispatcher($app))->setTransactionManagerResolver( |
| 219 |
fn () => $this->resolveDatabaseTransactionsManager() |
| 220 |
); |
| 221 |
}); |
| 222 |
|
| 223 |
$this->app->alias(Dispatcher::class, 'events'); |
| 224 |
} |
| 225 |
|
| 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 |
/** |
| 241 |
* Bind the db (query builder) instance into the container. |
| 242 |
* @return null |
| 243 |
*/ |
| 244 |
protected function bindDB() |
| 245 |
{ |
| 246 |
$connection = new WPDBConnection($GLOBALS['wpdb']); |
| 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 |
|
| 261 |
Model::setEventDispatcher($this->app['events']); |
| 262 |
|
| 263 |
$this->app->singletonIf('db', function($app) use ($resolver) { |
| 264 |
return new DatabaseManager($resolver); |
| 265 |
}); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Bind the Url instance into the container. |
| 270 |
* @return null |
| 271 |
*/ |
| 272 |
protected function bindUrl() |
| 273 |
{ |
| 274 |
$this->app->bind(URL::class, function($app) { |
| 275 |
return new URL('', new UrlGenerator($app)); |
| 276 |
}); |
| 277 |
|
| 278 |
$this->app->alias(URL::class, 'url'); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Bind the router instance into the container. |
| 283 |
* @return null |
| 284 |
*/ |
| 285 |
protected function bindRouter() |
| 286 |
{ |
| 287 |
$this->app->singleton(Router::class, function($app) { |
| 288 |
return new Router($app); |
| 289 |
}); |
| 290 |
|
| 291 |
$this->app->alias(Router::class, 'router'); |
| 292 |
} |
| 293 |
|
| 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 |
/** |
| 308 |
* Bind the paginator instance into the container. |
| 309 |
* @return null |
| 310 |
*/ |
| 311 |
protected function bindPaginator() |
| 312 |
{ |
| 313 |
AbstractPaginator::currentPathResolver(function () { |
| 314 |
return $this->app['request']->url(); |
| 315 |
}); |
| 316 |
|
| 317 |
AbstractPaginator::currentPageResolver(function ($pageName = 'page') { |
| 318 |
$page = $this->app['request']->get($pageName); |
| 319 |
|
| 320 |
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { |
| 321 |
return $page; |
| 322 |
} |
| 323 |
|
| 324 |
return 1; |
| 325 |
}); |
| 326 |
|
| 327 |
AbstractPaginator::queryStringResolver(function () { |
| 328 |
return $this->app['request']->query(); |
| 329 |
}); |
| 330 |
|
| 331 |
AbstractCursorPaginator::currentCursorResolver(function ($cursorName = 'cursor') { |
| 332 |
return Cursor::fromEncoded($this->app['request']->get($cursorName)); |
| 333 |
}); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Bind the pipeline instance into the container. |
| 338 |
* @return null |
| 339 |
*/ |
| 340 |
protected function bindPipeline() |
| 341 |
{ |
| 342 |
$this->app->bind(Pipeline::class, function ($app) { |
| 343 |
return new Pipeline($app); |
| 344 |
}); |
| 345 |
|
| 346 |
$this->app->alias(Pipeline::class, 'pipeline'); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Load other bindings the developers might |
| 351 |
* have added in the application level. |
| 352 |
* |
| 353 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 354 |
* @return null |
| 355 |
*/ |
| 356 |
protected function extendBindings($app) |
| 357 |
{ |
| 358 |
$bindings = $app['path'] . 'boot/bindings.php'; |
| 359 |
|
| 360 |
if (is_readable($bindings)) { |
| 361 |
require_once $bindings; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Load the plugin's global functions |
| 367 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 368 |
* @return null |
| 369 |
*/ |
| 370 |
protected function loadGlobalFunctions($app) |
| 371 |
{ |
| 372 |
$globals = $app['path'] . 'boot/globals.php'; |
| 373 |
|
| 374 |
if (is_readable($globals)) { |
| 375 |
require_once $globals; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Adds new alias to maintain the backward compatibility. |
| 381 |
* |
| 382 |
* @param string $class |
| 383 |
* @return void |
| 384 |
*/ |
| 385 |
protected function addBackwardCompatibleAlias($class) |
| 386 |
{ |
| 387 |
$this->app->alias( |
| 388 |
$class, $alias = $this->getAlias($class) |
| 389 |
); |
| 390 |
|
| 391 |
if (!class_exists($alias)) { |
| 392 |
class_alias($class, $alias); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Resolves the backward compatible alias. |
| 398 |
* |
| 399 |
* @param string $class |
| 400 |
* @return string New alias |
| 401 |
*/ |
| 402 |
protected function getAlias($class) |
| 403 |
{ |
| 404 |
$pieces = explode('\\', $class); |
| 405 |
|
| 406 |
if ($index = Arr::findPath($pieces, 'Http')) { |
| 407 |
unset($pieces[$index]); |
| 408 |
} |
| 409 |
|
| 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); |
| 457 |
} |
| 458 |
} |
| 459 |
|