From f17eebecf2edf7cc085922c44dcf2486aec7025c Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 25 Apr 2010 21:50:02 -0700 Subject: [PATCH] Adding CakeRequest and its test case. Moving features from Dispatcher into CakeRequest. --- cake/libs/cake_request.php | 136 ++++++++++++++++++++ cake/tests/cases/libs/cake_request.test.php | 43 +++++++ 2 files changed, 179 insertions(+) create mode 100644 cake/libs/cake_request.php create mode 100644 cake/tests/cases/libs/cake_request.test.php diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php new file mode 100644 index 000000000..7f62d7737 --- /dev/null +++ b/cake/libs/cake_request.php @@ -0,0 +1,136 @@ +_processPost(); + } + if (isset($params['form']['data'])) { + $params['data'] = $params['form']['data']; + unset($params['form']['data']); + } + if (isset($_GET)) { + $this->_processGet(); + } + $this->_processFiles(); + } + +/** + * process the post data and set what is there into the object. + * + * @return void + */ + protected function _processPost() { + $this->params['form'] = $_POST; + if (ini_get('magic_quotes_gpc') === '1') { + $this->params['form'] = stripslashes_deep($this->params['form']); + } + if (env('HTTP_X_HTTP_METHOD_OVERRIDE')) { + $this->params['form']['_method'] = env('HTTP_X_HTTP_METHOD_OVERRIDE'); + } + if (isset($this->params['form']['_method'])) { + if (!empty($_SERVER)) { + $_SERVER['REQUEST_METHOD'] = $this->params['form']['_method']; + } else { + $_ENV['REQUEST_METHOD'] = $this->params['form']['_method']; + } + unset($this->params['form']['_method']); + } + } + +/** + * Process the GET parameters and move things into the object. + * + * @return void + */ + protected function _processGet() { + if (ini_get('magic_quotes_gpc') === '1') { + $url = stripslashes_deep($_GET); + } else { + $url = $_GET; + } + if (isset($this->params['url'])) { + $this->params['url'] = array_merge($this->params['url'], $url); + } else { + $this->params['url'] = $url; + } + } + +/** + * Process $_FILES and move things into the object. + * + * @return void + */ + protected function _processFiles() { + if (isset($_FILES) && is_array($_FILES)) { + foreach ($_FILES as $name => $data) { + if ($name != 'data') { + $this->params['form'][$name] = $data; + } + } + } + + if (isset($_FILES['data'])) { + foreach ($_FILES['data'] as $key => $data) { + foreach ($data as $model => $fields) { + if (is_array($fields)) { + foreach ($fields as $field => $value) { + if (is_array($value)) { + foreach ($value as $k => $v) { + $this->params['data'][$model][$field][$k][$key] = $v; + } + } else { + $this->params['data'][$model][$field][$key] = $value; + } + } + } else { + $this->params['data'][$model][$key] = $fields; + } + } + } + } + } +} \ No newline at end of file diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php new file mode 100644 index 000000000..3c8b18c06 --- /dev/null +++ b/cake/tests/cases/libs/cake_request.test.php @@ -0,0 +1,43 @@ +_server = $_SERVER; + $this->_get = $_GET; + $this->_post = $_POST; + $this->_files = $_FILES; + } + +/** + * end test + * + * @return void + */ + function endTest() { + $_SERVER = $this->_server; + $_GET = $this->_get; + $_POST = $this->_post; + $_FILES = $this->_files; + } + +/** + * test construction + * + * @return void + */ + function testConstructionGetParsing() { + $GET = array( + 'one' => 'param', + 'two' => 'banana' + ); + $request = new CakeRequest(); + $this->assertEqual($request->params['url'], $_GET); + } +} \ No newline at end of file