PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / framework / src / WPFluent / Foundation / Application.php

Application.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at vendor/wpfluent/framework/src/WPFluent/Foundation/Application.php

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