| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Glue WordPress plugin framework. |
| 5 |
* |
| 6 |
* @package Glue |
| 7 |
* @link https://github.com/wpglue |
| 8 |
* @author Sheikh Heera <heera.sheikh77@gmail.com> |
| 9 |
* @license https://github.com/wpglue/framework/blob/master/LICENSE |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FluentForm\Framework\Foundation; |
| 13 |
|
| 14 |
use FluentForm\Framework\Exception\ExceptionHandler; |
| 15 |
|
| 16 |
class Application extends Container |
| 17 |
{ |
| 18 |
use PathsAndUrlsTrait, SetGetAttributesTrait, FacadeLoaderTrait, HelpersTrait; |
| 19 |
|
| 20 |
/** |
| 21 |
* Framework Version |
| 22 |
*/ |
| 23 |
const VERSION = '1.0.0'; |
| 24 |
|
| 25 |
/** |
| 26 |
* $baseFile root plugin file path |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $baseFile = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* The app config (/config/app.php) |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
protected $appConfig = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Callbacks for framework's booted event |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $booted = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Callbacks for framework's ready event |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
protected $ready = array(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Get application version |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function version() |
| 54 |
{ |
| 55 |
return self::VERSION; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Static interface to initiate the application |
| 60 |
* @param string $baseFile (root plugin file path) |
| 61 |
* @param array $appConfig (/config/app.php) |
| 62 |
* @return $this |
| 63 |
*/ |
| 64 |
public static function run($baseFile, $appConfig) |
| 65 |
{ |
| 66 |
return new static($baseFile, $appConfig); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Init the application |
| 71 |
* @param string $baseFile (root plugin file path) |
| 72 |
* @param array $appConfig (/config/app.php) |
| 73 |
*/ |
| 74 |
public function __construct($baseFile, $appConfig) |
| 75 |
{ |
| 76 |
$this->baseFile = $baseFile; |
| 77 |
$this->appConfig = $appConfig; |
| 78 |
$this->bootstrapApplication(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Bootup the application |
| 83 |
* @param string $baseFile (root plugin file path) |
| 84 |
* @param array $appConfig (/config/app.php) |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
protected function bootstrapApplication() |
| 88 |
{ |
| 89 |
$this->setAppBaseBindings(); |
| 90 |
$this->setExceptionHandler(); |
| 91 |
$this->loadApplicationTextDomain(); |
| 92 |
$this->bootStrapApplicationProviders(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Register application base bindings |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
protected function setAppBaseBindings() |
| 100 |
{ |
| 101 |
$this->bindAppInstance(); |
| 102 |
$this->registerAppPaths(); |
| 103 |
$this->registerAppUrls(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Bind application instance |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
protected function bindAppInstance() |
| 111 |
{ |
| 112 |
AppFacade::setApplication($this); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Set Application paths |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
protected function registerAppPaths() |
| 120 |
{ |
| 121 |
$path = plugin_dir_path($this->baseFile); |
| 122 |
$this->bindInstance('path', $path); |
| 123 |
$this->bindInstance('path.app', $path.'app/'); |
| 124 |
$this->bindInstance('path.config', $path.'config/'); |
| 125 |
$this->bindInstance('path.public', $path.'public/'); |
| 126 |
$this->bindInstance('path.framework', $path.'framework/'); |
| 127 |
$this->bindInstance('path.resource', $path.'resources/'); |
| 128 |
$this->bindInstance('path.storage', $path.'storage/'); |
| 129 |
$this->bindInstance('path.asset', $path.'resources/assets/'); |
| 130 |
$this->bindInstance('path.language', dirname( plugin_basename( $this->baseFile ) ).'/resources/languages'); |
| 131 |
$this->bindInstance('path.view', $path.'resources/views/'); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Set Application urls |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
protected function registerAppUrls() |
| 139 |
{ |
| 140 |
$url = plugin_dir_url($this->baseFile); |
| 141 |
$this->bindInstance('url', $url); |
| 142 |
$this->bindInstance('url.public', $url.'public/'); |
| 143 |
$this->bindInstance('url.resource', $url.'resources/'); |
| 144 |
$this->bindInstance('url.asset', $url.'resources/assets/'); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Set Application Exception Handler |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
protected function setExceptionHandler() |
| 152 |
{ |
| 153 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 154 |
return new ExceptionHandler($this); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* load languages path for i18n pot files |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
protected function loadApplicationTextDomain() |
| 163 |
{ |
| 164 |
return load_plugin_textdomain( |
| 165 |
$this->getTextDomain(), false, $this->languagePath() |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Bootstrap all service providers |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
protected function bootStrapApplicationProviders() |
| 174 |
{ |
| 175 |
$this->bootstrapWith(array_merge( |
| 176 |
$this->getEngineProviders(), |
| 177 |
$this->getPluginProviders(), |
| 178 |
$this->getCommonProviders() |
| 179 |
)); |
| 180 |
|
| 181 |
$this->fireCallbacks($this->ready); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Boot application with providers |
| 186 |
* @param array $providers |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
public function bootstrapWith(array $providers) |
| 190 |
{ |
| 191 |
$instances = []; |
| 192 |
|
| 193 |
foreach ($providers as $provider) { |
| 194 |
$instances[] = $instance = new $provider($this); |
| 195 |
$instance->booting(); |
| 196 |
} |
| 197 |
|
| 198 |
$this->registerAppFacadeLoader(); |
| 199 |
|
| 200 |
$this->fireCallbacks($this->booted); |
| 201 |
|
| 202 |
foreach ($instances as $provider) { |
| 203 |
$provider->booted(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get engine/core providers |
| 209 |
* @return array |
| 210 |
*/ |
| 211 |
public function getEngineProviders() |
| 212 |
{ |
| 213 |
return $this->getProviders('core'); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get plugin providers (Common) |
| 218 |
* @return array |
| 219 |
*/ |
| 220 |
public function getCommonProviders() |
| 221 |
{ |
| 222 |
return $this->getProviders('plugin')['common']; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get plugin providers (Backend|Frontend) |
| 227 |
* @return array |
| 228 |
*/ |
| 229 |
public function getPluginProviders() |
| 230 |
{ |
| 231 |
if ($this->isUserOnAdminArea()) { |
| 232 |
return $this->getProviders('plugin')['backend']; |
| 233 |
} else { |
| 234 |
return $this->getProviders('plugin')['frontend']; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Register booted events |
| 240 |
* @param mixed $callback |
| 241 |
* @return void |
| 242 |
*/ |
| 243 |
public function booted($callback) |
| 244 |
{ |
| 245 |
$this->booted[] = $this->parseHandler($callback); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Register ready events |
| 250 |
* @param mixed $callback |
| 251 |
* @return void |
| 252 |
*/ |
| 253 |
public function ready($callback) |
| 254 |
{ |
| 255 |
$this->ready[] = $this->parseHandler($callback); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Fire application event's handlers |
| 260 |
* @param array $callbacks |
| 261 |
* @return void |
| 262 |
*/ |
| 263 |
public function fireCallbacks(array $callbacks) |
| 264 |
{ |
| 265 |
foreach ($callbacks as $callback) { |
| 266 |
call_user_func_array($callback, [$this]); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|