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