| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
use FluentBooking\Framework\Support\Str; |
| 7 |
use FluentBooking\Framework\View\View; |
| 8 |
use FluentBooking\Framework\Http\URL; |
| 9 |
use FluentBooking\Framework\Http\Router; |
| 10 |
use FluentBooking\Framework\Support\Facade; |
| 11 |
use FluentBooking\Framework\Support\Pipeline; |
| 12 |
use FluentBooking\Framework\Http\Request\Request; |
| 13 |
use FluentBooking\Framework\Http\Response\Response; |
| 14 |
use FluentBooking\Framework\Events\Dispatcher; |
| 15 |
use FluentBooking\Framework\Database\Orm\Model; |
| 16 |
use FluentBooking\Framework\Validator\Validator; |
| 17 |
use FluentBooking\Framework\Foundation\RequestGuard; |
| 18 |
use FluentBooking\Framework\Database\ConnectionResolver; |
| 19 |
use FluentBooking\Framework\Database\Query\WPDBConnection; |
| 20 |
use FluentBooking\Framework\Pagination\AbstractCursorPaginator; |
| 21 |
use FluentBooking\Framework\Pagination\AbstractPaginator; |
| 22 |
use FluentBooking\Framework\Pagination\CursorPaginator; |
| 23 |
use FluentBooking\Framework\Pagination\Cursor; |
| 24 |
use WpOrg\Requests\Exception\Http\Status401; |
| 25 |
|
| 26 |
class ComponentBinder |
| 27 |
{ |
| 28 |
/** |
| 29 |
* The application instance |
| 30 |
* @var \FluentBooking\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 |
'DB', |
| 45 |
'URL', |
| 46 |
'Router', |
| 47 |
'Paginator', |
| 48 |
'Pipeline', |
| 49 |
]; |
| 50 |
|
| 51 |
/** |
| 52 |
* Construct the binder |
| 53 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 54 |
*/ |
| 55 |
public function __construct($app) |
| 56 |
{ |
| 57 |
$this->registerFacadeResolver( |
| 58 |
$this->app = $app |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Bind all the components in to the container. |
| 64 |
* @return null |
| 65 |
*/ |
| 66 |
public function bindComponents() |
| 67 |
{ |
| 68 |
foreach ($this->bindables as $value) { |
| 69 |
$method = "bind{$value}"; |
| 70 |
$this->{$method}(); |
| 71 |
} |
| 72 |
|
| 73 |
$this->extendBindings($this->app); |
| 74 |
|
| 75 |
$this->loadGlobalFunctions($this->app); |
| 76 |
|
| 77 |
$this->registerResolvingEvent($this->app); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Register resolving event into the container. |
| 82 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 83 |
* @return null |
| 84 |
*/ |
| 85 |
protected function registerResolvingEvent($app) |
| 86 |
{ |
| 87 |
$app->resolving(RequestGuard::class, function($request) use ($app) { |
| 88 |
|
| 89 |
if (method_exists($request, 'authorize')) { |
| 90 |
if(!$request->authorize()) throw new Status401; |
| 91 |
} |
| 92 |
|
| 93 |
$request->merge($request->beforeValidation()); |
| 94 |
$request->validate(); |
| 95 |
$request->merge((array) $request->afterValidation( |
| 96 |
$app->make('validator') |
| 97 |
)); |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register the dynamic facade resolver. |
| 103 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 104 |
* @return null |
| 105 |
*/ |
| 106 |
protected function registerFacadeResolver($app) |
| 107 |
{ |
| 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 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Create a facade resolver class dynamically |
| 123 |
* @param string $facade |
| 124 |
* @param string $class |
| 125 |
* @param \FluentBooking\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 \FluentBooking\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 db (query builder) instance into the container. |
| 237 |
* @return null |
| 238 |
*/ |
| 239 |
protected function bindDB() |
| 240 |
{ |
| 241 |
$this->app->singleton('db', function($app) { |
| 242 |
return new WPDBConnection( |
| 243 |
$GLOBALS['wpdb'], $app->config->get('database') |
| 244 |
); |
| 245 |
}); |
| 246 |
|
| 247 |
Model::setEventDispatcher($this->app['events']); |
| 248 |
|
| 249 |
Model::setConnectionResolver(new ConnectionResolver); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Bind the URL instance into the container. |
| 254 |
* @return null |
| 255 |
*/ |
| 256 |
protected function bindURL() |
| 257 |
{ |
| 258 |
$this->app->bind('url', function($app) { |
| 259 |
return new URL; |
| 260 |
}); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Bind the router instance into the container. |
| 265 |
* @return null |
| 266 |
*/ |
| 267 |
protected function bindRouter() |
| 268 |
{ |
| 269 |
$this->app->singleton('router', function($app) { |
| 270 |
return new Router($app); |
| 271 |
}); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Bind the paginator instance into the container. |
| 276 |
* @return null |
| 277 |
*/ |
| 278 |
protected function bindPaginator() |
| 279 |
{ |
| 280 |
AbstractPaginator::currentPathResolver(function () { |
| 281 |
return $this->app['request']->url(); |
| 282 |
}); |
| 283 |
|
| 284 |
AbstractPaginator::currentPageResolver(function ($pageName = 'page') { |
| 285 |
$page = $this->app['request']->get($pageName); |
| 286 |
|
| 287 |
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { |
| 288 |
return $page; |
| 289 |
} |
| 290 |
|
| 291 |
return 1; |
| 292 |
}); |
| 293 |
|
| 294 |
AbstractPaginator::queryStringResolver(function () { |
| 295 |
return $this->app['request']->query(); |
| 296 |
}); |
| 297 |
|
| 298 |
AbstractCursorPaginator::currentCursorResolver(function ($cursorName = 'cursor') { |
| 299 |
return Cursor::fromEncoded($this->app['request']->get($cursorName)); |
| 300 |
}); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Bind the pipeline instance into the container. |
| 305 |
* @return null |
| 306 |
*/ |
| 307 |
protected function bindPipeline() |
| 308 |
{ |
| 309 |
$this->app->bind(Pipeline::class, function ($app) { |
| 310 |
return new Pipeline($app); |
| 311 |
}); |
| 312 |
|
| 313 |
$this->app->alias(Pipeline::class, 'pipeline'); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Load other bindings the developers might |
| 318 |
* have added in the application level. |
| 319 |
* |
| 320 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 321 |
* @return null |
| 322 |
*/ |
| 323 |
protected function extendBindings($app) |
| 324 |
{ |
| 325 |
$bindings = $app['path'] . 'boot/bindings.php'; |
| 326 |
|
| 327 |
if (is_readable($bindings)) { |
| 328 |
require_once $bindings; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Load the plugin's global functions |
| 334 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 335 |
* @return null |
| 336 |
*/ |
| 337 |
protected function loadGlobalFunctions($app) |
| 338 |
{ |
| 339 |
$globals = $app['path'] . 'boot/globals.php'; |
| 340 |
|
| 341 |
if (is_readable($globals)) { |
| 342 |
require_once $globals; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Adds new alias to maintain ther backward compatibility. |
| 348 |
* |
| 349 |
* @param string $class |
| 350 |
* @return void |
| 351 |
*/ |
| 352 |
protected function addBackwardCompatibleAlias($class) |
| 353 |
{ |
| 354 |
$this->app->alias( |
| 355 |
$class, $alias = $this->getAlias($class) |
| 356 |
); |
| 357 |
|
| 358 |
if (!class_exists($alias)) { |
| 359 |
class_alias($class, $alias); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Resolves the backward compatible alias. |
| 365 |
* |
| 366 |
* @param string $class |
| 367 |
* @return string New alias |
| 368 |
*/ |
| 369 |
protected function getAlias($class) |
| 370 |
{ |
| 371 |
$pieces = explode('\\', $class); |
| 372 |
|
| 373 |
if ($index = Arr::findPath($pieces, 'Http')) { |
| 374 |
unset($pieces[$index]); |
| 375 |
} |
| 376 |
|
| 377 |
return implode('\\', $pieces); |
| 378 |
} |
| 379 |
} |
| 380 |
|