-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
83 lines (71 loc) · 2.64 KB
/
Application.php
File metadata and controls
83 lines (71 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace NGFramer\NGFramerPHPBase;
use App\Config\ApplicationConfig;
use NGFramer\NGFramerPHPBase\Controller\Controller;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\CallbackException;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\FileException;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\MiddlewareException;
use NGFramer\NGFramerPHPBase\Defaults\Exceptions\RegistryException;
use NGFramer\NGFramerPHPBase\Response\Response;
class Application
{
// Initialization of the variables used across the application.
public static Application $application;
public Request $request;
public Router $router;
public Controller $controller;
public Session $session;
public Response $response;
// Instantiation of the __construct function.
/**
* Application constructor.
* @throws FileException
*/
public function __construct()
{
// Save the class instance to a static variable.
self::$application = $this;
// Just get the path value and more, no need of dependencies.
$this->request = new Request();
// Router's constructor will need Application instance and request instance.
$this->router = new Router();
// Response's constructor will need nothing.
$this->response = new Response();
// Controller's constructor will need Application instance and response instance.
$this->controller = new Controller();
// Session's constructor will need nothing.
$this->session = Session::init();
// Get all the routes, middlewares, and events.
$this->getRegistry();
}
/**
* Get the default AppRegistry data and developer's AppRegistry data.
* @throws FileException
*/
private function getRegistry(): void
{
// Get the root directory path and application type.
$root = ApplicationConfig::get('root');
$appType = ApplicationConfig::get('appType');
if ($appType == 'app') {
if (file_exists($root . '/Registry/AppRegistry.php')) {
require_once $root . '/Registry/AppRegistry.php';
}
} elseif ($appType == 'api') {
if (file_exists($root . '/Registry/ApiRegistry.php')) {
require_once $root . '/Registry/ApiRegistry.php';
}
}
}
/**
* Function to handle the request and route it to the controller.
* @throws MiddlewareException
* @throws CallbackException
* @throws RegistryException
*/
public function run(): void
{
// Route the request to the controller and get the response
$this->router->handleRoute();
}
}