1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core; |
4
|
|
|
|
5
|
|
|
use Core\Dependency\Request; |
6
|
|
|
use Core\Dependency\Response; |
7
|
|
|
use Core\Dependency\Session; |
8
|
|
|
use PDO; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Container for dependency injection |
13
|
|
|
* we take care of setting our template and database connections |
14
|
|
|
* We also call our Request and Session objects for the SuperGlobals access |
15
|
|
|
* @package Core |
16
|
|
|
* |
17
|
|
|
* PHP version 7 |
18
|
|
|
*/ |
19
|
|
|
class Container |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
//used for the model connection |
23
|
|
|
/** |
24
|
|
|
* @var PDO this is to store the pdo connection. We only need to set once |
25
|
|
|
*/ |
26
|
|
|
private $dbh = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Dependency\Request object |
30
|
|
|
*/ |
31
|
|
|
private $request; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Dependency\Session object |
35
|
|
|
*/ |
36
|
|
|
private $session; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
private $response; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* gets the twig template environment |
43
|
|
|
* @return \Twig_Environment |
44
|
|
|
*/ |
45
|
|
|
public function getTemplate(): \Twig_Environment |
46
|
|
|
{ |
47
|
|
|
$twigOptions = []; |
48
|
|
|
if (!Config::DEV_ENVIRONMENT) { |
49
|
|
|
$twigOptions = [ |
50
|
|
|
'cache' => dirname(__DIR__) . '/Cache' |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
$loader = new \Twig_Loader_Filesystem(dirname(__DIR__) . '/App/Views'); |
54
|
|
|
$twig = new \Twig_Environment($loader, $twigOptions); |
55
|
|
|
|
56
|
|
|
return $twig; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* create the database connection via PDO |
61
|
|
|
* @return PDO |
62
|
|
|
*/ |
63
|
|
|
public function setPdo(): \PDO |
64
|
|
|
{ |
65
|
|
|
if ($this->dbh) { |
66
|
|
|
return $this->dbh; |
67
|
|
|
} |
68
|
|
|
$dsn = "mysql:host=" . Config::DB_HOST . ";dbname=" . Config::DB_NAME . ";charset=utf8"; //Creating the Data Source name |
69
|
|
|
$opt = [ |
70
|
|
|
PDO::ATTR_PERSISTENT => true, |
71
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
72
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC |
73
|
|
|
]; |
74
|
|
|
$this->dbh = new PDO($dsn, Config::DB_USER, Config::DB_PASSWORD, $opt);; |
75
|
|
|
return $this->dbh; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Creates the request object if not already present and returns it |
81
|
|
|
* @return Dependency\Request|Request |
82
|
|
|
*/ |
83
|
|
|
public function getRequest(): Dependency\Request |
84
|
|
|
{ |
85
|
|
|
if (!$this->request) { |
86
|
|
|
$this->request = new Request(); |
87
|
|
|
} |
88
|
|
|
return $this->request; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates the response object if not already present and returns it |
93
|
|
|
* @return Response |
94
|
|
|
*/ |
95
|
|
|
public function getResponse(): Dependency\Response |
96
|
|
|
{ |
97
|
|
|
if (!$this->response) { |
98
|
|
|
$this->response = new Response(); |
99
|
|
|
} |
100
|
|
|
return $this->response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Creates the session object if not already present and returns it |
105
|
|
|
* @return Dependency\Session|session |
106
|
|
|
*/ |
107
|
|
|
public function getSession(): Dependency\Session |
108
|
|
|
{ |
109
|
|
|
if (!$this->session) { |
110
|
|
|
$this->session = new Session(); |
111
|
|
|
} |
112
|
|
|
return $this->session; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |