| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 83.33% covered (warning) | 83.33% | 5 / 6 | CRAP | 93.10% covered (success) | 93.10% | 27 / 29
Context | 0.00% covered (danger) | 0.00% | 0 / 1 | 83.33% covered (warning) | 83.33% | 5 / 6 | 18.11 | 93.10% covered (success) | 93.10% | 27 / 29
__construct | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 8 / 8
serve | 100.00% covered (success) | 100.00% | 1 / 1 | 4 | 100.00% covered (success) | 100.00% | 9 / 9
run | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 | 0.00% covered (danger) | 0.00% | 0 / 2
__get | 100.00% covered (success) | 100.00% | 1 / 1 | 8 | 100.00% covered (success) | 100.00% | 8 / 8
isTestingEnvironment | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 1 / 1
getContext | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 1 / 1

1 | <?php
2 |
3 | namespace Tools;
4 |
5 | /**
6 | * Cms context
7 | * Contains all website informations
8 | * Will be passed to themes files and to modules
9 | **/
10 | class Context
11 | {
12 | /**
13 | * @var \Tools\Hooks $hookManager
14 | * /core/tools/Hooks.php
15 | * allow triggering hooks and registers module's endpoints
16 | * Can be accessed read-only via $instance->hookManager
17 | **/
18 | private $hookManager;
19 |
20 | /**
21 | * @var \Tools\Context $instance Singleton value
22 | **/
23 | private static $instance;
24 |
25 | /**
26 | * @var \Tools\ModuleManager $moduleManager
27 | * /core/tools/ModuleManager.php
28 | * load all active modules and contains informations about them
29 | * Can be accessed read-only via $instance->moduleManager
30 | **/
31 | private $moduleManager;
32 |
33 | /**
34 | * @var \Tools\Router $router
35 | * /core/tools/Router.php
36 | * Contains information about directories and paths
37 | * Allow user to generate links
38 | * Can be accessed read-only via $instance->router
39 | **/
40 | private $router;
41 |
42 | /**
43 | * TODO
44 | * @var \Tools\Cart $cart
45 | * /core/tools/Cart.php
46 | * Not defined yet
47 | * Can be accessed read-only via $instance->cart
48 | **/
49 | private $cart;
50 |
51 | /**
52 | * TODO
53 | * @var \Tools\User $user (entity ?)
54 | * /core/tools/User.php
55 | * Can be accessed read-only via $instance->user
56 | **/
57 | private $user;
58 |
59 | /**
60 | * @var \Tools\AController $controller
61 | * /core/tools/AController.php
62 | * Controller being called by user's request
63 | **/
64 | private $controller;
65 |
66 | /**
67 | * @var string $ip
68 | * Contains the remote client's IP, or empty
69 | * Can be accessed via ->ip
70 | **/
71 | private $ip;
72 |
73 | /**
74 | * @var array $server
75 | * contains $_SERVER or Unit test environment
76 | **/
77 | private $server;
78 |
79 | /**
80 | * Create context from $_SERVER environment
81 | * and initialize all data
82 | * @param array $server optional, contains $_SERVER configuration
83 | * -> REMOTE_ADDR client's ip
84 | * -> SCRIPT_NAME
85 | * -> DOCUMENT_ROOT
86 | * -> REQUEST_SCHEME 'http:https'
87 | * -> REQUEST_URI
88 | * -> HTTP_HOST
89 | **/
90 | public function __construct($server = null)
91 | {
92 | self::$instance = $this;
93 | if ($server == null)
94 | $server = $_SERVER;
95 | $this->ip = $server["REMOTE_ADDR"];
96 | $this->hookManager = new Hooks($this);
97 | $this->server = $server;
98 | $this->router = new Router($this->server, $this);
99 | }
100 |
101 | public function serve()
102 | {
103 | // @codeCoverageIgnoreStart
104 | if (!\Entity\ModelBase::init())
105 | {
106 | @require_once(getcwd().'/core/setup/index.php');
107 | die;
108 | }
109 | // @codeCoverageIgnoreEnd
110 | $this->router->init($this->server);
111 | $this->moduleManager = new ModuleManager($this);
112 | $this->hookManager->trigger("routerSetup");
113 | try
114 | {
115 | $this->controller = $this->router->serveUrl();
116 | if (!$this->controller)
117 | throw new \Exception\Error404();
118 | }
119 | catch (\Exception\Error404 $e)
120 | {
121 | $this->controller = new \Controller\Error404($this, array());
122 | }
123 | return $this->controller;
124 | }
125 |
126 | /**
127 | * Execute controller
128 | **/
129 | public function run()
130 | {
131 | $this->controller->start();
132 | }
133 |
134 | /**
135 | * Getter function
136 | **/
137 | public function __get($key)
138 | {
139 | switch ($key)
140 | {
141 | case "router": return $this->router; break;
142 | case "cart": return $this->cart; break;
143 | case "user": return $this->user; break;
144 | case "moduleManager": return $this->moduleManager; break;
145 | case "hookManager": return $this->hookManager; break;
146 | case "ip": return $this->ip; break;
147 | case "controller": return $this->controller; break;
148 | }
149 | throw new \Exception("Cannot access attribute {$key}");
150 | }
151 |
152 | /**
153 | * Check if executed from phpUnit
154 | **/
155 | public function isTestingEnvironment()
156 | {
157 | return isset($this->server["phpUnit"]) && $this->server["phpUnit"] == true;
158 | }
159 |
160 | /**
161 | * @return \Tools\Context context
162 | * Get the last Context instance
163 | **/
164 | public static function getContext()
165 | {
166 | return self::$instance;
167 | }
168 | }
169 |