summaryrefslogtreecommitdiffstats
path: root/system/classes/request.php
blob: 4105901a5ba664e85cab19aa64640b5bd9107cf8 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

/**
 * Handles client request.
 * @package Core
 */
class Request {

    /**
     * Stores POST data
     * @var array
     * @access private
     */
	private $_post;

    /**
     * Stores GET data
     * @var array
     * @access private
     */
	private $_get;

    /**
     * Current Route
     * @var Route
     * @access public 
     */
	public $route;

    /**
     * Request method
     * @var string
     * @access public  
     */
	public $method;
	
	/**
     * Creates a new request
     * 
     * @param  Route  $route  Route for this request
     * @param  string $method HTTP method for the request (e.g. GET, POST)
	 * @param  array  $post   Array of POST data
	 * @param  array  $get    Array of GET data
	 * @param  array  $server Array of SERVER data
     * @return Request Initialized request
	 *
     * @access public  
     */
	public function __construct($route, $method="GET", $post = array(), $get = array(), $server = array()) {
		$this->route   = $route;
		$this->method  = $method;
		$this->_post   = $post;
		$this->_get    = $get;
		$this->_server = $server;
	}
	
    /**
     * Retrieves a GET parameter 
     * 
     * @param string $key    Parameter key
     * @param mixed $default Default value
     * @return mixed Returns a value if a key is specified,
	 *               or an array of GET parameters if it isn't.
     * @access public  
     */
	public function get($key = null, $default = null) {
		if ($key == null)
			return $this->_get;
		return Misc::arr($this->_get,$key,$default);
	}

    /**
     * Retrieves a POST parameter 
     * 
     * @param string $key    Parameter key
     * @param mixed $default Default value
     * @return mixed Returns a value if a key is specified,
	 *               or an array of POST parameters if it isn't.
     * @access public  
     */
	public function post($key = null, $default = null) {
		if ($key == null)
			return $this->_post;
		return Misc::arr($this->_post,$key,$default);
	}
	
	/**
     * Retrieves a SERVER parameter 
     * 
     * @param string $key    Parameter key
     * @param mixed  $default Default value
     * @return mixed Returns a value if a key is specified,
	 *               or an array of SERVER parameters if it isn't.
     * @access public  
     */
	public function server($key = null, $default = null) {
		if ($key == null)
			return $this->_server;
		return Misc::arr($this->_server,$key,$default);
	}
	
    /**
     * Retrieves a Route parameter 
     * 
     * @param string $key    Parameter key
     * @param mixed $default Default value
     * @return mixed Returns a value if a key is specified,
	 *               or an array of Route parameters if it isn't.
     * @access public  
     */
	public function param($key = null, $default = null) {
		if ($key == null)
			return $this->route->params;
		return Misc::arr($this->route->params,$key,$default);
	}

    /**
     * Initializes the routed Controller and executes specified action 
     * 
     * @return Response A Response object with the body and headers set
     * @access public 
     */
	public function execute() {
		$controller = $this->param('controller').'_Controller';
		if (!class_exists($controller))
			throw new Exception("Class {$controller} doesn't exist",404);
		$controller = new $controller;
		$controller->request = $this;
		$controller->run($this->param('action'));
		return $controller->response;
	}

    /**
     * Creates a Request representing current HTTP request.
     * 
     * @return Request Request 
     * @access public 
     * @static 
     */
	public static function create() {
		$uri = $_SERVER['REQUEST_URI'];
		$basepath=Config::get('core.basepath','/');
		$uri = preg_replace("#^{$basepath}(?:index\.php/)?#i", '/', $uri);
		$url_parts = parse_url($uri);
		return new Request(Route::match($url_parts['path']), $_SERVER['REQUEST_METHOD'], $_POST, $_GET, $_SERVER);
	}

}