| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Framework\Foundation; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use FluentSupport\Framework\Foundation\Config; |
| 7 |
use FluentSupport\Framework\Foundation\Container; |
| 8 |
use FluentSupport\Framework\Foundation\FoundationTrait; |
| 9 |
use FluentSupport\Framework\Foundation\ComponentBinder; |
| 10 |
|
| 11 |
class Application extends Container |
| 12 |
{ |
| 13 |
use FoundationTrait; |
| 14 |
|
| 15 |
protected $file = null; |
| 16 |
protected $baseUrl = null; |
| 17 |
protected $basePath = null; |
| 18 |
protected $handlerNamespace = null; |
| 19 |
protected $controllerNamespace = null; |
| 20 |
protected $permissionNamespace = null; |
| 21 |
|
| 22 |
public function __construct($file = null) |
| 23 |
{ |
| 24 |
$this->init($file); |
| 25 |
$this->setAppLevelNamespace(); |
| 26 |
$this->bootstrapApplication(); |
| 27 |
} |
| 28 |
|
| 29 |
protected function init($file) |
| 30 |
{ |
| 31 |
$this->file = $this->pluginFilePath($file); |
| 32 |
$this->basePath = plugin_dir_path($this->file); |
| 33 |
$this->baseUrl = plugin_dir_url($this->file); |
| 34 |
} |
| 35 |
|
| 36 |
protected function pluginFilePath($file) |
| 37 |
{ |
| 38 |
$file = $file ?: realpath(__DIR__ . '/../../../plugin.php'); |
| 39 |
|
| 40 |
return $file; |
| 41 |
} |
| 42 |
|
| 43 |
protected function setAppLevelNamespace() |
| 44 |
{ |
| 45 |
$autoload = $this->getComposer('autoload'); |
| 46 |
|
| 47 |
$psr4 = array_flip($autoload['psr-4']); |
| 48 |
|
| 49 |
$this->handlerNamespace = $psr4['app/'] . 'Hooks\Handlers'; |
| 50 |
|
| 51 |
$this->policyNamespace = $psr4['app/'] . 'Http\Policies'; |
| 52 |
|
| 53 |
$this->controllerNamespace = $psr4['app/'] . 'Http\Controllers'; |
| 54 |
} |
| 55 |
|
| 56 |
protected function getComposer($section = null) |
| 57 |
{ |
| 58 |
$data = json_decode( |
| 59 |
file_get_contents($this->basePath . 'composer.json'), true |
| 60 |
); |
| 61 |
|
| 62 |
return $section ? $data[$section] : $data; |
| 63 |
} |
| 64 |
|
| 65 |
protected function bootstrapApplication() |
| 66 |
{ |
| 67 |
$this->bindAppInstance(); |
| 68 |
$this->bindPathsAndUrls(); |
| 69 |
$this->loadConfigIfExists(); |
| 70 |
// $this->loadTextdomain(); |
| 71 |
$this->bindComponents($this); |
| 72 |
$this->requireCommonFiles($this); |
| 73 |
} |
| 74 |
|
| 75 |
protected function bindAppInstance() |
| 76 |
{ |
| 77 |
App::setInstance($this); |
| 78 |
$this->instance('app', $this); |
| 79 |
$this->instance(__CLASS__, $this); |
| 80 |
} |
| 81 |
|
| 82 |
protected function bindPathsAndUrls() |
| 83 |
{ |
| 84 |
$this->bindUrls(); |
| 85 |
$this->basePaths(); |
| 86 |
} |
| 87 |
|
| 88 |
protected function bindUrls() |
| 89 |
{ |
| 90 |
$this['url.assets'] = $this->baseUrl . 'assets/'; |
| 91 |
} |
| 92 |
|
| 93 |
protected function basePaths() |
| 94 |
{ |
| 95 |
$this['path'] = $this->basePath; |
| 96 |
$this['path.app'] = $this->basePath . 'app/'; |
| 97 |
$this['path.hooks'] = $this['path.app'] . 'Hooks/'; |
| 98 |
$this['path.http'] = $this['path.app'] . 'Http/'; |
| 99 |
$this['path.controllers'] = $this['path.http'] . 'Controllers/'; |
| 100 |
$this['path.config'] = $this->basePath . 'config/'; |
| 101 |
$this['path.assets'] = $this->basePath . 'assets/'; |
| 102 |
$this['path.resources'] = $this->basePath . 'resources/'; |
| 103 |
$this['path.views'] = $this['path.app'] . 'views/'; |
| 104 |
} |
| 105 |
|
| 106 |
protected function loadConfigIfExists() |
| 107 |
{ |
| 108 |
$data = []; |
| 109 |
|
| 110 |
if (is_dir($this['path.config'])) { |
| 111 |
foreach (glob($this['path.config'] . '*.php') as $file) { |
| 112 |
$data[basename($file, '.php')] = require_once($file); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$this->instance('config', new Config($data)); |
| 117 |
} |
| 118 |
|
| 119 |
protected function loadTextdomain() |
| 120 |
{ |
| 121 |
$this->addAction('init', function() { |
| 122 |
load_plugin_textdomain( |
| 123 |
$this->config->get('app.text_domain'), false, $this->config->get('app.domain_path') |
| 124 |
); |
| 125 |
}); |
| 126 |
} |
| 127 |
|
| 128 |
protected function bindComponents($app) |
| 129 |
{ |
| 130 |
(new ComponentBinder($app))->bindComponents(); |
| 131 |
} |
| 132 |
|
| 133 |
protected function requireCommonFiles($app) |
| 134 |
{ |
| 135 |
require_once $this->basePath . 'app/Hooks/actions.php'; |
| 136 |
require_once $this->basePath . 'app/Hooks/filters.php'; |
| 137 |
|
| 138 |
$this->addAction('rest_api_init', function($wpRestServer) use ($app) { |
| 139 |
try { |
| 140 |
$router = $app->router; |
| 141 |
require_once $this->basePath . 'app/Http/Routes/api.php'; |
| 142 |
$router->registerRoutes(); |
| 143 |
} catch (InvalidArgumentException $e) { |
| 144 |
$app->doCustomAction('handle_exception', $e); |
| 145 |
} |
| 146 |
}); |
| 147 |
} |
| 148 |
} |
| 149 |
|