32 private $_responseFormat = null;
37 private $_method = null;
39 private static $_logger = null;
46 parent::__construct($formatter);
47 if (self::$_logger == null) {
51 foreach (self::getAllHeaders() as $name => $value) {
55 if (isset($_SERVER[
'QUERY_STRING'])) {
56 self::fix($_GET, $_SERVER[
'QUERY_STRING']);
58 $this->_method = isset($_SERVER[
'REQUEST_METHOD']) ? $_SERVER[
'REQUEST_METHOD'] :
'';
76 public function initialize($controller=null, $context=null, $action=null) {
78 $basePath = preg_replace(
'/\/?[^\/]*$/',
'', $_SERVER[
'SCRIPT_NAME']);
79 $requestUri = preg_replace(
'/\?.*$/',
'', $_SERVER[
'REQUEST_URI']);
81 if (self::$_logger->isDebugEnabled()) {
82 self::$_logger->debug(
"Request path: ".$requestPath);
86 $baseRequestValues = array();
87 $defaultValuePattern =
'([^/]+)';
89 if ($config->hasSection(
'routes')) {
90 $routes = $config->getSection(
'routes');
91 foreach ($routes as $route => $requestDef) {
93 $allowedMethods = null;
94 if (strpos($route,
'/') !== 0) {
95 list($methodStr, $route) = explode(
'/', $route, 2);
96 $allowedMethods = preg_split(
'/\s*,\s*/', trim($methodStr));
97 $route =
'/'.trim($route);
102 $route = preg_replace_callback(
'/\{([^\}]+)\}/',
function ($match) use($defaultValuePattern, &$params) {
106 $paramParts = explode(
'|', $match[1]);
108 $params[] = $paramParts[0];
110 return sizeof($paramParts) > 1 ?
'('.$paramParts[1].
')' : $defaultValuePattern;
112 $route =
'/^'.str_replace(
'/',
'\/', $route).
'\/?$/';
115 if (self::$_logger->isDebugEnabled()) {
116 self::$_logger->debug(
"Check path: ".$route);
119 if (preg_match($route, $requestPath, $matches)) {
120 if (self::$_logger->isDebugEnabled()) {
121 self::$_logger->debug(
"Match");
124 parse_str($requestDef, $baseRequestValues);
126 for ($i=0, $count=
sizeof($params); $i<$count; $i++) {
127 $baseRequestValues[$params[$i]] = isset($matches[$i+1]) ? $matches[$i+1] : null;
135 if ($allowedMethods != null && !in_array($method, $allowedMethods)) {
141 $requestValues = array_merge($_GET, $_POST, $_FILES);
142 $rawPostBody = file_get_contents(
'php://input');
144 $json = json_decode($rawPostBody,
true);
145 if (is_array($json)) {
146 foreach ($json as $key => $value) {
147 $requestValues[$key] = $value;
152 $controller = isset($requestValues[
'controller']) ?
153 filter_var($requestValues[
'controller'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW) :
154 (isset($baseRequestValues[
'controller']) ? $baseRequestValues[
'controller'] : $controller);
156 $context = isset($requestValues[
'context']) ?
157 filter_var($requestValues[
'context'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW) :
158 (isset($baseRequestValues[
'context']) ? $baseRequestValues[
'context'] : $context);
160 $action = isset($requestValues[
'action']) ?
161 filter_var($requestValues[
'action'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW) :
162 (isset($baseRequestValues[
'action']) ? $baseRequestValues[
'action'] : $action);
168 $this->
setValues(array_merge($baseRequestValues, $requestValues));
175 return $this->_method;
182 $this->_responseFormat = $format;
189 if ($this->_responseFormat == null) {
192 return $this->_responseFormat;
200 $str =
'method='.$this->_method.
', ';
201 $str .=
'responseformat='.$this->_responseFormat.
', ';
202 $str .= parent::__toString();
210 private static function getAllHeaders() {
212 foreach ($_SERVER as $name => $value) {
213 if (substr($name, 0, 5) ==
'HTTP_') {
214 $name = str_replace(
' ',
'-', ucwords(strtolower(str_replace(
'_',
' ', substr($name, 5)))));
215 $headers[$name] = $value;
217 else if ($name ==
"CONTENT_TYPE") {
218 $headers[
"Content-Type"] = $value;
220 else if ($name ==
"CONTENT_LENGTH") {
221 $headers[
"Content-Length"] = $value;
234 private static function fix(&$target, $source, $keep=
false) {
240 $source = preg_replace_callback(
242 # Match at start of string or &
244 # Exclude cases where the period is in brackets, e.g. foo[bar.blarg]
246 # Affected cases: periods and spaces
248 # Keep matching until assignment, next variable, end of string or
252 function ($key) use (&$keys) {
253 $keys[] = $key = base64_encode(urldecode($key[0]));
254 return urlencode($key);
263 parse_str($source, $data);
264 foreach ($data as $key => $val) {
266 if (!in_array($key, $keys)) {
267 $target[$key] = $val;
271 $key = base64_decode($key);
272 $target[$key] = $val;
276 $key = preg_replace(
'/(\.| )/',
'_', $key);
277 $target[$key] = $val;
getFormatter()
Get the Formatter instance.
Default Request implementation.
static getGeneral($message)
Factory method for creating a general error instance.
static getLogger($name)
Get the logger with the given name.
static getInstance($name, $dynamicConfiguration=array())
static escapeForRegex($string)
Escape characters of a string for use in a regular expression Code from http://php.net/manual/de/function.preg-replace.php.
Request holds the request values that are used as input to Controller instances.
getHeader($name, $default=null)
AbstractControllerMessage is the base class for request/response implementations. ...
__toString()
Get a string representation of the message.
__construct(Formatter $formatter)
Constructor.
ApplicationException signals a general application exception.
initialize($controller=null, $context=null, $action=null)
setResponseFormat($format)