| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentSupport\Framework\View\View; |
| 6 |
use FluentSupport\Framework\Http\Router; |
| 7 |
use FluentSupport\Framework\Request\Request; |
| 8 |
use FluentSupport\Framework\Response\Response; |
| 9 |
use FluentSupport\Framework\Database\Orm\Model; |
| 10 |
use FluentSupport\Framework\Validator\Validator; |
| 11 |
use FluentSupport\Framework\Foundation\Dispatcher; |
| 12 |
use FluentSupport\Framework\Foundation\RequestGuard; |
| 13 |
use FluentSupport\Framework\Pagination\AbstractPaginator; |
| 14 |
use FluentSupport\Framework\Database\ConnectionResolver; |
| 15 |
use FluentSupport\Framework\Database\Query\WPDBConnection; |
| 16 |
use FluentSupport\Framework\Foundation\UnAuthorizedException; |
| 17 |
|
| 18 |
class ComponentBinder |
| 19 |
{ |
| 20 |
protected $app = null; |
| 21 |
|
| 22 |
protected $bindables = [ |
| 23 |
'Request', |
| 24 |
'Response', |
| 25 |
'Validator', |
| 26 |
'View', |
| 27 |
'Events', |
| 28 |
'DataBase', |
| 29 |
'Router', |
| 30 |
'Paginator' |
| 31 |
]; |
| 32 |
|
| 33 |
public function __construct($app) |
| 34 |
{ |
| 35 |
$this->app = $app; |
| 36 |
} |
| 37 |
|
| 38 |
public function bindComponents() |
| 39 |
{ |
| 40 |
foreach ($this->bindables as $value) { |
| 41 |
$method = "bind{$value}"; |
| 42 |
$this->{$method}(); |
| 43 |
} |
| 44 |
|
| 45 |
$this->extendBindings($this); |
| 46 |
} |
| 47 |
|
| 48 |
protected function bindRequest() |
| 49 |
{ |
| 50 |
$this->app->singleton(Request::class, function ($app) { |
| 51 |
return new Request($app, $_GET, $_POST, $_FILES); |
| 52 |
}); |
| 53 |
|
| 54 |
$this->app->alias(Request::class, 'request'); |
| 55 |
} |
| 56 |
|
| 57 |
protected function bindResponse() |
| 58 |
{ |
| 59 |
$this->app->singleton(Response::class, function($app) { |
| 60 |
return new Response($app); |
| 61 |
}); |
| 62 |
|
| 63 |
$this->app->alias(Response::class, 'response'); |
| 64 |
} |
| 65 |
|
| 66 |
protected function bindValidator() |
| 67 |
{ |
| 68 |
$this->app->bind(Validator::class, function($app) { |
| 69 |
return new Validator; |
| 70 |
}); |
| 71 |
|
| 72 |
$this->app->alias(Validator::class, 'validator'); |
| 73 |
} |
| 74 |
|
| 75 |
protected function bindView() |
| 76 |
{ |
| 77 |
$this->app->bind(View::class, function($app) { |
| 78 |
return new View($app); |
| 79 |
}); |
| 80 |
|
| 81 |
$this->app->alias(View::class, 'view'); |
| 82 |
} |
| 83 |
|
| 84 |
protected function bindEvents() |
| 85 |
{ |
| 86 |
$this->app->singleton(Dispatcher::class, function($app) { |
| 87 |
return new Dispatcher($app); |
| 88 |
}); |
| 89 |
|
| 90 |
$this->app->alias(Dispatcher::class, 'events'); |
| 91 |
} |
| 92 |
|
| 93 |
protected function bindDataBase() |
| 94 |
{ |
| 95 |
$this->app->bindShared('db', function($app) { |
| 96 |
return new WPDBConnection( |
| 97 |
$GLOBALS['wpdb'], $app->config->get('database') |
| 98 |
); |
| 99 |
}); |
| 100 |
|
| 101 |
Model::setEventDispatcher($this->app['events']); |
| 102 |
|
| 103 |
Model::setConnectionResolver(new ConnectionResolver); |
| 104 |
} |
| 105 |
|
| 106 |
protected function bindRouter() |
| 107 |
{ |
| 108 |
$this->app->singleton('router', function($app) { |
| 109 |
return new Router($app); |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
protected function bindPaginator() |
| 114 |
{ |
| 115 |
AbstractPaginator::currentPathResolver(function () { |
| 116 |
return $this->app['request']->url(); |
| 117 |
}); |
| 118 |
|
| 119 |
AbstractPaginator::currentPageResolver(function ($pageName = 'page') { |
| 120 |
$page = $this->app['request']->get($pageName); |
| 121 |
|
| 122 |
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { |
| 123 |
return $page; |
| 124 |
} |
| 125 |
|
| 126 |
return 1; |
| 127 |
}); |
| 128 |
} |
| 129 |
|
| 130 |
protected function extendBindings($app) |
| 131 |
{ |
| 132 |
$bindings = $this->app['path'] . 'boot/bindings.php'; |
| 133 |
|
| 134 |
if (is_readable($bindings)) { |
| 135 |
require_once $bindings; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|