-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathController.php
131 lines (119 loc) · 3.63 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* File defining the Core Controller
*
* PHP Version 5.3
*
* @category Backend
* @package Core
* @author J Jurgens du Toit <jrgns@backend-php.net>
* @copyright 2011 - 2012 Jade IT (cc)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
namespace Backend\Core;
use Backend\Interfaces\ControllerInterface;
use Backend\Interfaces\RequestInterface;
use Backend\Interfaces\DependencyInjectionContainerInterface;
use Backend\Core\Exception as CoreException;
/**
* Controller that acts as the connection between Models and Views.
*
* @category Backend
* @package Core
* @author J Jurgens du Toit <jrgns@backend-php.net>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
class Controller implements ControllerInterface
{
/**
* The Dependency Injection Container to be used when getting services.
*
* @var \Backend\nterfaces\DependencyInjectionContainerInterface
*/
protected $container = null;
/**
* This contains the Request that's being actioned.
*
* @var \Backend\nterfaces\RequestInterface
*/
protected $request = null;
/**
* The constructor for the object
*/
public function __construct(
DependencyInjectionContainerInterface $container = null,
RequestInterface $request = null
) {
$this->container = $container;
$this->request = $request;
}
/**
* Set the Request for the Controller.
*
* @param \Backend\Interfaces\RequestInterface $request The request for the
* Controller.
*
* @return \Backend\Interfaces\ControllerInterface The current object.
*/
public function setRequest(RequestInterface $request)
{
$this->request = $request;
return $this;
}
/**
* Get the Controller's Request
*
* @return \Backend\Interfaces\RequestInterface The Controller's Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Set the Controller's DI Container.
*
* @param \Backend\Interfaces\DependencyInjectionContainerInterface $container
* The DI Container for the Controller.
*
* @return \Backend\Interfaces\ControllerInterface The current object.
*/
public function setContainer(DependencyInjectionContainerInterface $container)
{
$this->container = $container;
return $this;
}
/**
* Get the Controller's DI Container
*
* @return \Backend\Interfaces\DependencyInjectionContainerInterface The
* Controller's DI Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Create a redirection Response
*
* @param string $location The location to redirect to
* @param int $responseCode The HTTP status code to use
*
* @return \Backend\Core\Response The Response object
*/
public function redirect($location, $responseCode = 302)
{
if (substr($responseCode, 0, 1) !== '3') {
throw new CoreException('Invalid Redirection Response Code');
}
if ($location[0] === '/' && (empty($location[1]) || $location[1] !== '/')) {
// Relative Redirect
$location = $this->container->get('request_context')->getLink() . $location;
}
$responseClass = $this->container->getParameter('response.class');
$response = new $responseClass('Redirecting to ' . $location, $responseCode);
$response->setHeader('Location', $location);
return $response;
}
}