|
1
|
|
|
<?php |
|
2
|
|
|
namespace JayaCode\Framework\Core\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Http\Request; |
|
5
|
|
|
use JayaCode\Framework\Core\Http\Response; |
|
6
|
|
|
use JayaCode\Framework\Core\View\View; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Controller |
|
10
|
|
|
* @package JayaCode\Framework\Core\Controller |
|
11
|
|
|
*/ |
|
12
|
|
|
class Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Request |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $request; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Response |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $response; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var View |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $viewEngine; |
|
28
|
|
|
/** |
|
29
|
|
|
* Controller constructor. |
|
30
|
|
|
* @param Request $request |
|
31
|
|
|
* @param Response $response |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(Request $request = null, Response $response = null) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->initialize($request, $response); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Request|null $request |
|
40
|
|
|
* @param Response $response |
|
41
|
|
|
*/ |
|
42
|
|
|
public function initialize(Request $request = null, Response $response = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->request = $request; |
|
45
|
|
|
$this->response = $response; |
|
46
|
|
|
|
|
47
|
|
|
$this->viewEngine = new View(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Response $response |
|
52
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
53
|
|
|
*/ |
|
54
|
|
|
public function out(Response $response) |
|
55
|
|
|
{ |
|
56
|
|
|
return $response->send(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param Request|null $request |
|
61
|
|
|
* @param Response|null $response |
|
62
|
|
|
* @return Controller |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function create(Request $request = null, Response $response = null) |
|
65
|
|
|
{ |
|
66
|
|
|
return new static($request, $response); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return Response |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getResponse() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->response; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Response $response |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setResponse($response) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->response = $response; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Render Template View |
|
87
|
|
|
* @param $name |
|
88
|
|
|
* @param array $data |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function view($name, array $data = array()) |
|
92
|
|
|
{ |
|
93
|
|
|
$tpl = $this->viewEngine->loadTemplate($name); |
|
94
|
|
|
return $tpl->render($data); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|