PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.3
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.3
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Foundation / Application.php

Application.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.3, at vendor/wpfluent/framework/src/WPFluent/Foundation/Application.php

309 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Framework\Foundation;
4
5 use InvalidArgumentException;
6 use FluentSupport\Framework\Foundation\Config;
7 use FluentSupport\Framework\Container\Container;
8 use FluentSupport\Framework\Foundation\ComponentBinder;
9 use FluentSupport\Framework\Foundation\FoundationTrait;
10 use FluentSupport\Framework\Foundation\AsyncRequestTrait;
11 use FluentSupport\Framework\Foundation\CronTaskSchedulerTrait;
12
13 class Application extends Container
14 {
15 use FoundationTrait;
16 use AsyncRequestTrait;
17 use CronTaskSchedulerTrait;
18
19 /**
20 * Main plugin file's absolute path
21 * @var string
22 */
23 protected $file = null;
24
25 /**
26 * Plugin's base url
27 * @var string
28 */
29 protected $baseUrl = null;
30
31 /**
32 * Plugin's base path
33 * @var string
34 */
35 protected $basePath = null;
36
37 /**
38 * Default namespace for hook's handlers
39 * @var string
40 */
41 protected $handlerNamespace = null;
42
43 /**
44 * Default namespace for controllers
45 * @var string
46 */
47 protected $controllerNamespace = null;
48
49 /**
50 * Default namespace for policy handlers
51 * @var string
52 */
53 protected $permissionNamespace = null;
54
55 /**
56 * Composer JSON
57 * @var null|array
58 */
59 protected static $composer = null;
60
61 /**
62 * Construct the application instance
63 *
64 * @param string $file The main plugin file's absolute path
65 * @return null
66 */
67 public function __construct($file = null)
68 {
69 $this->init($file);
70 $this->setAppLevelNamespace();
71 $this->bootstrapApplication();
72 }
73
74 /**
75 * Init the application instance
76 *
77 * @param string $file The main plugin file's absolute path
78 *
79 * @return null
80 */
81 protected function init($file)
82 {
83 $this['__pluginfile__'] = $this->file = $file;
84 $this->basePath = plugin_dir_path($this->file);
85 $this->baseUrl = plugin_dir_url($this->file);
86 }
87
88 /**
89 * Set the default application level namespaces to resolve
90 * the controllers, policies and various hook handlers.
91 *
92 * @return null
93 */
94 protected function setAppLevelNamespace()
95 {
96 $composer = $this->getComposer();
97
98 $psr4 = array_flip($composer['autoload']['psr-4']);
99
100 $this->policyNamespace = $psr4['app/'] . 'Http\Policies';
101
102 $this->handlerNamespace = $psr4['app/'] . 'Hooks\Handlers';
103
104 $this->controllerNamespace = $psr4['app/'] . 'Http\Controllers';
105
106 $this['__namespace__'] = $composer['extra']['wpfluent']['namespace']['current'];
107 }
108
109 /**
110 * Get the composer data as an array
111 *
112 * @param string $section Specific key
113 *
114 * @return array partial or full composer data array
115 */
116 public function getComposer($section = null)
117 {
118 if (is_null(static::$composer)) {
119 static::$composer = json_decode(
120 file_get_contents($this->basePath . 'composer.json'), true
121 );
122 }
123
124 return $section ? static::$composer[$section] : static::$composer;
125 }
126
127 /**
128 * Bootstrap the application.
129 *
130 * @return null
131 */
132 protected function bootstrapApplication()
133 {
134 $this->bindAppInstance();
135 $this->bindPathsAndUrls();
136 $this->loadConfigIfExists();
137 $this->registerTextdomain();
138 $this->bindCoreComponents();
139 $this->requireCommonFiles($this);
140 $this->registerAsyncActions();
141 $this->addRestApiInitAction($this);
142 }
143
144 /**
145 * Bind application instance in the container.
146 *
147 * @return null
148 */
149 protected function bindAppInstance()
150 {
151 App::setInstance($this);
152 $this->instance('app', $this);
153 $this->instance(__CLASS__, $this);
154 }
155
156 /**
157 * Bind the paths and urls
158 *
159 * @return null
160 */
161 protected function bindPathsAndUrls()
162 {
163 $this->bindUrls();
164 $this->basePaths();
165 }
166
167 /**
168 * Bind urls
169 *
170 * @return null
171 */
172 protected function bindUrls()
173 {
174 $this['url.assets'] = $this->baseUrl . 'assets/';
175 }
176
177 /**
178 * Bind paths
179 *
180 * @return null
181 */
182 protected function basePaths()
183 {
184 $this['path'] = $this->basePath;
185 $this['path.app'] = $this->basePath . 'app/';
186 $this['path.hooks'] = $this['path.app'] . 'Hooks/';
187 $this['path.http'] = $this['path.app'] . 'Http/';
188 $this['path.controllers'] = $this['path.http'] . 'Controllers/';
189 $this['path.config'] = $this->basePath . 'config/';
190 $this['path.assets'] = $this->basePath . 'assets/';
191 $this['path.resources'] = $this->basePath . 'resources/';
192 $this['path.views'] = $this['path.app'] . 'Views/';
193 }
194
195 /**
196 * Load application's config and set
197 * the data in the Config instance.
198 *
199 * @return null
200 */
201 protected function loadConfigIfExists()
202 {
203 $data = [];
204
205 if (is_dir($this['path.config'])) {
206 foreach (glob($this['path.config'] . '*.php') as $file) {
207 $data[basename($file, '.php')] = require_once($file);
208 }
209 }
210
211 $this->instance('config', new Config($data));
212 }
213
214 /**
215 * Register plugin's text domain
216 *
217 * @return null
218 */
219 protected function registerTextdomain()
220 {
221 // $this->addAction('init', function() {
222 // load_plugin_textdomain(
223 // $this->config->get('app.text_domain'), false, $this->textDomainPath()
224 // );
225 // });
226 }
227
228 /**
229 * Resolve the text domain path.
230 *
231 * @return null
232 */
233 protected function textDomainPath()
234 {
235 return basename($this['path']) . $this->config->get('app.domain_path');
236 }
237
238 /**
239 * Bind the components of the framework into the container so
240 * they'll be available throughout the application life cycle.
241 *
242 * @return null
243 */
244 protected function bindCoreComponents()
245 {
246 (new ComponentBinder($this))->bindComponents();
247 }
248
249 /**
250 * Load (include) the files where hooks are registered.
251 *
252 * @param self $app
253 *
254 * @return null
255 */
256 protected function requireCommonFiles($app)
257 {
258 require_once $this->basePath . 'app/Hooks/actions.php';
259 require_once $this->basePath . 'app/Hooks/filters.php';
260
261 if (file_exists($includes = $this->basePath . 'app/Hooks/includes.php')) {
262 require_once $includes;
263 }
264 }
265
266 /**
267 * Register the rest api init actions and routes
268 *
269 * @param self $app
270 */
271 protected function addRestApiInitAction($app)
272 {
273 $this->addAction('rest_api_init', function($wpRestServer) use ($app) {
274 try {
275 $this->registerRestRoutes($app->router);
276 } catch (InvalidArgumentException $e) {
277 return $app->response->json([
278 'message' => $e->getMessage()
279 ], $e->getCode() ?: 500);
280 }
281 });
282 }
283
284 /**
285 * Register rest routes.
286 *
287 * @param \FluentSupport\Framework\Http\Router $router
288 *
289 * @return null
290 */
291 protected function registerRestRoutes($router)
292 {
293 $router->registerRoutes(
294 $this->requireRouteFile($router)
295 );
296 }
297
298 /**
299 * Load (include) routes
300 *
301 * @param \FluentSupport\Framework\Http\Router $router
302 * @return null
303 */
304 protected function requireRouteFile($router)
305 {
306 require_once $this['path.http'] . 'Routes/routes.php';
307 }
308 }
309