1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: thiago |
5
|
|
|
* Date: 01/04/18 |
6
|
|
|
* Time: 10:07 |
7
|
|
|
*/ |
8
|
|
|
namespace MyWonderland; |
9
|
|
|
|
10
|
|
|
use MyWonderland\Controller\HomeController; |
11
|
|
|
use MyWonderland\Controller\SpotifyAuthController; |
12
|
|
|
use MyWonderland\Domain\Manager\SessionManager; |
13
|
|
|
use MyWonderland\Service\RequestService; |
14
|
|
|
use MyWonderland\Service\SongkickService; |
15
|
|
|
use MyWonderland\Service\SpotifyService; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A simple handmade Dependency Injection Container |
19
|
|
|
* @package MyWonderland |
20
|
|
|
*/ |
21
|
|
|
class Container |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Each time a shared object is called, the object instance created |
25
|
|
|
* for the first call will be returned |
26
|
|
|
* |
27
|
|
|
* Static methods and static variables (aka class methods and class variables) |
28
|
|
|
* are a way of putting code and data into a kind of namespace. |
29
|
|
|
* |
30
|
|
|
* https://stackoverflow.com/questions/11496884/private-static-method-vs-static-method |
31
|
|
|
*/ |
32
|
|
|
static private $shared = []; |
33
|
|
|
|
34
|
|
|
private $parameters = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Containerontainer constructor. |
38
|
|
|
* @param array $parameters |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $parameters = []) |
41
|
|
|
{ |
42
|
|
|
$this->parameters = $parameters; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
public function getTwig() { |
47
|
|
|
if (isset(self::$shared['twig'])) { |
48
|
|
|
return self::$shared['twig']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates'); |
52
|
|
|
self::$shared['twig'] = new \Twig_Environment($loader); |
53
|
|
|
return self::$shared['twig']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getRequestService() { |
57
|
|
|
if (isset(self::$shared['requestService'])) { |
58
|
|
|
return self::$shared['requestService']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
self::$shared['requestService'] = new RequestService(); |
62
|
|
|
return self::$shared['requestService']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function build($class) { |
67
|
|
|
switch ($class) { |
68
|
|
|
case HomeController::class: |
69
|
|
|
return $this->getHomeController(); |
70
|
|
|
case SpotifyAuthController::class: |
71
|
|
|
return $this->getSpotifyAuthController(); |
72
|
|
|
default: |
73
|
|
|
return new $class(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
public function getHomeController() { |
79
|
|
|
return new HomeController( |
80
|
|
|
new SessionManager(), |
81
|
|
|
$this->getTwig(), |
82
|
|
|
new SpotifyService($this->getRequestService()), |
83
|
|
|
new SongkickService($this->getRequestService()) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function getSpotifyAuthController() { |
89
|
|
|
return new SpotifyAuthController( |
90
|
|
|
new SessionManager(), |
91
|
|
|
$this->getTwig(), |
92
|
|
|
new SpotifyService($this->getRequestService()) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |