← All changes
|
vendor/wpfluent/framework/src/WPFluent/Foundation/ComponentBinder.php
+129
-81
1.7.1
→
2.5.0
View file →
| @@ -5,11 +5,12 @@ | ||
| 5 | 5 | use FluentBooking\Framework\Support\Arr; |
| 6 | 6 | use FluentBooking\Framework\View\View; |
| 7 | 7 | use FluentBooking\Framework\Cache\Cache; |
| 8 | 8 | use FluentBooking\Framework\Http\URL; |
| 9 | +use FluentBooking\Framework\Http\UrlGenerator; | |
| 10 | +use FluentBooking\Framework\Support\Mail; | |
| 11 | +use FluentBooking\Framework\Support\Pipeline; | |
| 9 | 12 | use FluentBooking\Framework\Http\Router; |
| 10 | -use FluentBooking\Framework\Support\Facade; | |
| 11 | -use FluentBooking\Framework\Support\Pipeline; | |
| 12 | 13 | use FluentBooking\Framework\Http\Request\Request; |
| 13 | 14 | use FluentBooking\Framework\Http\Response\Response; |
| 14 | 15 | use FluentBooking\Framework\Events\Dispatcher; |
| 15 | 16 | use FluentBooking\Framework\Encryption\Encrypter; |
| @@ -15,8 +16,11 @@ | ||
| 15 | 16 | use FluentBooking\Framework\Encryption\Encrypter; |
| 16 | 17 | use FluentBooking\Framework\Database\Orm\Model; |
| 17 | 18 | use FluentBooking\Framework\Validator\Validator; |
| 18 | 19 | use FluentBooking\Framework\Foundation\RequestGuard; |
| 20 | +use FluentBooking\Framework\Foundation\Exceptions\ExceptionHandler; | |
| 21 | +use FluentBooking\Framework\Database\DatabaseManager; | |
| 22 | +use FluentBooking\Framework\Database\DatabaseTransactionsManager; | |
| 19 | 23 | use FluentBooking\Framework\Database\ConnectionResolver; |
| 20 | 24 | use FluentBooking\Framework\Database\Query\WPDBConnection; |
| 21 | 25 | use FluentBooking\Framework\Pagination\AbstractCursorPaginator; |
| 22 | 26 | use FluentBooking\Framework\Pagination\AbstractPaginator; |
| @@ -25,8 +29,10 @@ | ||
| 25 | 29 | use WpOrg\Requests\Exception\Http\Status401; |
| 26 | 30 | |
| 27 | 31 | class ComponentBinder |
| 28 | 32 | { |
| 33 | + use Concerns\DynamicFacadeTrait; | |
| 34 | + | |
| 29 | 35 | /** |
| 30 | 36 | * The application instance |
| 31 | 37 | * @var \FluentBooking\Framework\Foundation\Application |
| 32 | 38 | */ |
| @@ -46,10 +52,12 @@ | ||
| 46 | 52 | 'Encrypter', |
| 47 | 53 | 'DB', |
| 48 | 54 | 'URL', |
| 49 | 55 | 'Router', |
| 56 | + 'Mail', | |
| 50 | 57 | 'Paginator', |
| 51 | 58 | 'Pipeline', |
| 59 | + 'ExceptionHandler', | |
| 52 | 60 | ]; |
| 53 | 61 | |
| 54 | 62 | /** |
| 55 | 63 | * Construct the binder |
| @@ -79,8 +87,19 @@ | ||
| 79 | 87 | |
| 80 | 88 | $this->registerResolvingEvent($this->app); |
| 81 | 89 | } |
| 82 | 90 | |
| 91 | + public function resolveDatabaseTransactionsManager() | |
| 92 | + { | |
| 93 | + if (!$this->app->bound('db.transactions')) { | |
| 94 | + $this->app->singleton('db.transactions', function ($app) { | |
| 95 | + return new DatabaseTransactionsManager; | |
| 96 | + }); | |
| 97 | + } | |
| 98 | + | |
| 99 | + return $this->app->make('db.transactions'); | |
| 100 | + } | |
| 101 | + | |
| 83 | 102 | /** |
| 84 | 103 | * Register resolving event into the container. |
| 85 | 104 | * @param \FluentBooking\Framework\Foundation\Application $app |
| 86 | 105 | * @return null |
| @@ -87,86 +106,24 @@ | ||
| 87 | 106 | */ |
| 88 | 107 | protected function registerResolvingEvent($app) |
| 89 | 108 | { |
| 90 | 109 | $app->resolving(RequestGuard::class, function($request) use ($app) { |
| 110 | + | |
| 111 | + $request->setRequestInstance($app->request); | |
| 91 | 112 | |
| 92 | 113 | if (method_exists($request, 'authorize')) { |
| 93 | 114 | if(!$request->authorize()) throw new Status401; |
| 94 | 115 | } |
| 95 | 116 | |
| 96 | - $request->merge($request->beforeValidation()); | |
| 117 | + $request->merge((array) $request->beforeValidation()); | |
| 97 | 118 | $request->validate(); |
| 98 | 119 | $request->merge((array) $request->afterValidation( |
| 99 | - $app->make('validator') | |
| 120 | + $request->getValidator() | |
| 100 | 121 | )); |
| 101 | 122 | }); |
| 102 | 123 | } |
| 103 | 124 | |
| 104 | 125 | /** |
| 105 | - * Register the dynamic facade resolver. | |
| 106 | - * @param \FluentBooking\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 \FluentBooking\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 \FluentBooking\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 | 126 | * Bind the cache instance into the container. |
| 170 | 127 | * @return null |
| 171 | 128 | */ |
| 172 | 129 | protected function bindCache() |
| @@ -186,9 +143,9 @@ | ||
| 186 | 143 | { |
| 187 | 144 | $method = $this->getBindingMethod('singleton'); |
| 188 | 145 | |
| 189 | 146 | $this->app->$method(Request::class, function ($app) { |
| 190 | - return new Request($app, $_GET, $_POST, $_FILES); | |
| 147 | + return $this->resolveRequest($app); | |
| 191 | 148 | }); |
| 192 | 149 | |
| 193 | 150 | $this->app->alias(Request::class, 'request'); |
| 194 | 151 | |
| @@ -259,9 +216,11 @@ | ||
| 259 | 216 | */ |
| 260 | 217 | protected function bindEvents() |
| 261 | 218 | { |
| 262 | 219 | $this->app->singleton(Dispatcher::class, function($app) { |
| 263 | - return new Dispatcher($app); | |
| 220 | + return (new Dispatcher($app))->setTransactionManagerResolver( | |
| 221 | + fn () => $this->resolveDatabaseTransactionsManager() | |
| 222 | + ); | |
| 264 | 223 | }); |
| 265 | 224 | |
| 266 | 225 | $this->app->alias(Dispatcher::class, 'events'); |
| 267 | 226 | } |
| @@ -285,27 +244,38 @@ | ||
| 285 | 244 | * @return null |
| 286 | 245 | */ |
| 287 | 246 | protected function bindDB() |
| 288 | 247 | { |
| 289 | - $this->app->singleton('db', function($app) { | |
| 290 | - return new WPDBConnection( | |
| 291 | - $GLOBALS['wpdb'], $app->config->get('database') | |
| 292 | - ); | |
| 293 | - }); | |
| 248 | + $connection = new WPDBConnection($GLOBALS['wpdb']); | |
| 294 | 249 | |
| 250 | + $resolver = new ConnectionResolver([ | |
| 251 | + 'mysql' => $connection, | |
| 252 | + 'sqlite' => $connection, | |
| 253 | + ]); | |
| 254 | + | |
| 255 | + $resolver->setDefaultConnection('mysql'); | |
| 256 | + | |
| 257 | + $resolver->connection()->setTransactionManager( | |
| 258 | + $this->resolveDatabaseTransactionsManager() | |
| 259 | + ); | |
| 260 | + | |
| 261 | + Model::setConnectionResolver($resolver); | |
| 262 | + | |
| 295 | 263 | Model::setEventDispatcher($this->app['events']); |
| 296 | - | |
| 297 | - Model::setConnectionResolver(new ConnectionResolver); | |
| 264 | + | |
| 265 | + $this->app->singletonIf('db', function($app) use ($resolver) { | |
| 266 | + return new DatabaseManager($resolver); | |
| 267 | + }); | |
| 298 | 268 | } |
| 299 | 269 | |
| 300 | 270 | /** |
| 301 | - * Bind the URL instance into the container. | |
| 271 | + * Bind the Url instance into the container. | |
| 302 | 272 | * @return null |
| 303 | 273 | */ |
| 304 | - protected function bindURL() | |
| 274 | + protected function bindUrl() | |
| 305 | 275 | { |
| 306 | 276 | $this->app->bind(URL::class, function($app) { |
| 307 | - return new URL($app->make(Encrypter::class)); | |
| 277 | + return new URL('', new UrlGenerator($app)); | |
| 308 | 278 | }); |
| 309 | 279 | |
| 310 | 280 | $this->app->alias(URL::class, 'url'); |
| 311 | 281 | } |
| @@ -323,8 +293,21 @@ | ||
| 323 | 293 | $this->app->alias(Router::class, 'router'); |
| 324 | 294 | } |
| 325 | 295 | |
| 326 | 296 | /** |
| 297 | + * Bind the mail instance into the container. | |
| 298 | + * @return null | |
| 299 | + */ | |
| 300 | + protected function bindMail() | |
| 301 | + { | |
| 302 | + $this->app->bind(Mail::class, function($app) { | |
| 303 | + return new Mail(); | |
| 304 | + }); | |
| 305 | + | |
| 306 | + $this->app->alias(Mail::class, 'mail'); | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 327 | 310 | * Bind the paginator instance into the container. |
| 328 | 311 | * @return null |
| 329 | 312 | */ |
| 330 | 313 | protected function bindPaginator() |
| @@ -361,12 +344,31 @@ | ||
| 361 | 344 | $this->app->bind(Pipeline::class, function ($app) { |
| 362 | 345 | return new Pipeline($app); |
| 363 | 346 | }); |
| 364 | 347 | |
| 365 | - $this->app->alias(Pipeline::class, 'pipeline'); | |
| 348 | + $this->app->alias(Pipeline::class, 'pipeline'); | |
| 366 | 349 | } |
| 367 | 350 | |
| 368 | 351 | /** |
| 352 | + * Bind the exception-handler registry into the container. | |
| 353 | + * | |
| 354 | + * Default is the bare `Foundation\Exceptions\ExceptionHandler` (no | |
| 355 | + * renderables registered). Plugins override by re-binding their own | |
| 356 | + * subclass to the same key from `boot/bindings.php` BEFORE the first | |
| 357 | + * request hits Route. | |
| 358 | + * | |
| 359 | + * @return null | |
| 360 | + */ | |
| 361 | + protected function bindExceptionHandler() | |
| 362 | + { | |
| 363 | + $this->app->singleton(ExceptionHandler::class, function ($app) { | |
| 364 | + return new ExceptionHandler(); | |
| 365 | + }); | |
| 366 | + | |
| 367 | + $this->app->alias(ExceptionHandler::class, 'exception.handler'); | |
| 368 | + } | |
| 369 | + | |
| 370 | + /** | |
| 369 | 371 | * Load other bindings the developers might |
| 370 | 372 | * have added in the application level. |
| 371 | 373 | * |
| 372 | 374 | * @param \FluentBooking\Framework\Foundation\Application $app |
| @@ -426,6 +428,52 @@ | ||
| 426 | 428 | unset($pieces[$index]); |
| 427 | 429 | } |
| 428 | 430 | |
| 429 | 431 | return implode('\\', $pieces); |
| 432 | + } | |
| 433 | + | |
| 434 | + /** | |
| 435 | + * Resolve the appropriate request instance. | |
| 436 | + * | |
| 437 | + * @param $app | |
| 438 | + * @return Request|object (Anonymous Class) | |
| 439 | + */ | |
| 440 | + protected function resolveRequest($app) | |
| 441 | + { | |
| 442 | + return new Request($app, $_GET, $_POST); | |
| 443 | + } | |
| 444 | + | |
| 445 | + /** | |
| 446 | + * Check if the request is of the plugin. | |
| 447 | + * | |
| 448 | + * @return boolean | |
| 449 | + */ | |
| 450 | + protected function isRequestOfPlugin() | |
| 451 | + { | |
| 452 | + if (str_starts_with($this->app->env(), 'testing')) { | |
| 453 | + return true; | |
| 454 | + } | |
| 455 | + | |
| 456 | + $slug = $this->app->config->get('app.slug'); | |
| 457 | + | |
| 458 | + if (get_option('permalink_structure')) { | |
| 459 | + $route = $_SERVER['REQUEST_URI'] ?? ''; | |
| 460 | + } else { | |
| 461 | + $route = $_GET['rest_route'] ?? ''; | |
| 462 | + } | |
| 463 | + | |
| 464 | + $parsedUrl = parse_url($route); | |
| 465 | + | |
| 466 | + $path = $parsedUrl['path'] ?? ''; | |
| 467 | + | |
| 468 | + $path = str_replace('/wp-json', '', $path); | |
| 469 | + | |
| 470 | + if (is_admin()) { | |
| 471 | + $page = $_GET['page'] ?? ''; | |
| 472 | + if ($slug === $page) { | |
| 473 | + $path = $page; | |
| 474 | + } | |
| 475 | + } | |
| 476 | + | |
| 477 | + return str_starts_with(ltrim($path, '/'), $slug); | |
| 430 | 478 | } |
| 431 | 479 | } |