Subversion Repositories Applications.referentiel

Rev

Rev 6 | Rev 34 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6 Rev 19
Line 4... Line 4...
4
 
4
 
5
// TODO : gerer les retours : dans ce controleur : code retour et envoi ...
5
// TODO : gerer les retours : dans ce controleur : code retour et envoi ...
Line 6... Line 6...
6
class JRest {
6
class JRest {
7
 
7
 
Line 8... Line 8...
8
 	/** Parsed configuration file */
8
 	/** Parsed configuration file */
9
    private $config;
9
    private static $config;
Line 10... Line 10...
10
 
10
 
Line 24... Line 24...
24
	 * Constructor. Parses the configuration file "JRest.ini", grabs any request data sent, records the HTTP
24
	 * Constructor. Parses the configuration file "JRest.ini", grabs any request data sent, records the HTTP
25
	 * request method used and parses the request URL to find out the requested resource
25
	 * request method used and parses the request URL to find out the requested resource
26
	 * @param str iniFile Configuration file to use
26
	 * @param str iniFile Configuration file to use
27
	 */
27
	 */
28
	public function JRest($iniFile = 'jrest.ini.php') {
28
	public function JRest($iniFile = 'jrest.ini.php') {
-
 
29
		// Chargement de la configuration
29
		$this->config = parse_ini_file($iniFile, TRUE);
30
		self::$config = parse_ini_file($iniFile, TRUE);
-
 
31
		// Enregistrement de la méthode gérant l'autoload des classes
-
 
32
		spl_autoload_register(array('JRest', 'chargerClasse'));
-
 
33
		
30
		if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['QUERY_STRING'])) {
34
		if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['QUERY_STRING'])) {
31
			if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
35
			if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
32
				$this->requestData = '';
36
				$this->requestData = '';
33
				$httpContent = fopen('php://input', 'r');
37
				$httpContent = fopen('php://input', 'r');
34
				while ($data = fread($httpContent, 1024)) {
38
				while ($data = fread($httpContent, 1024)) {
Line 39... Line 43...
39
			if (strlen($_SERVER['QUERY_STRING']) == 0) {
43
			if (strlen($_SERVER['QUERY_STRING']) == 0) {
40
				$len = strlen($_SERVER['REQUEST_URI']);
44
				$len = strlen($_SERVER['REQUEST_URI']);
41
			} else {
45
			} else {
42
				$len = -(strlen($_SERVER['QUERY_STRING']) + 1);
46
				$len = -(strlen($_SERVER['QUERY_STRING']) + 1);
43
			}
47
			}
44
			$urlString = substr($_SERVER['REQUEST_URI'], strlen($this->config['settings']['baseURL']), $len);
48
			$urlString = substr($_SERVER['REQUEST_URI'], strlen(self::$config['settings']['baseURL']), $len);
Line 45... Line 49...
45
 
49
 
Line 46... Line 50...
46
			$urlParts = explode('/', $urlString);
50
			$urlParts = explode('/', $urlString);
47
 
51
 
Line 60... Line 64...
60
			trigger_error('I require the server variables REQUEST_URI, REQUEST_METHOD and QUERY_STRING to work.', E_USER_ERROR);
64
			trigger_error('I require the server variables REQUEST_URI, REQUEST_METHOD and QUERY_STRING to work.', E_USER_ERROR);
61
		}
65
		}
62
	}
66
	}
Line 63... Line 67...
63
 
67
 
-
 
68
	/**
-
 
69
	* La méthode __autoload() charge dynamiquement les classes trouvées dans le code.
-
 
70
	* Cette fonction est appelée par php5 quand il trouve une instanciation de classe dans le code.
-
 
71
	*
-
 
72
	*@param string le nom de la classe appelée.
-
 
73
	*@return void le fichier contenant la classe doit être inclu par la fonction.
-
 
74
	*/
-
 
75
	public static function chargerClasse($classe) {
-
 
76
		if (class_exists($classe)) {
-
 
77
			return null;
-
 
78
		}
-
 
79
		
-
 
80
		$chemins = array('', self::$config['settings']['servicesDir']);
-
 
81
		foreach ($chemins as $chemin) {
-
 
82
			$chemin = $chemin.$classe.'.php';
-
 
83
			if (file_exists($chemin)) {
-
 
84
				require_once $chemin;
-
 
85
			}
-
 
86
		}
-
 
87
	}
-
 
88
	
-
 
89
	
64
	/**
90
	/**
65
	 * Execute the request.
91
	 * Execute the request.
66
	 */
92
	 */
67
	function exec() {
93
	function exec() {
68
		switch ($this->method) {
94
		switch ($this->method) {
Line 86... Line 112...
86
	 * when a resource name is given, or a resource element when a resource and resource unique identifier are given. It does not change the
112
	 * when a resource name is given, or a resource element when a resource and resource unique identifier are given. It does not change the
87
	 * database contents.
113
	 * database contents.
88
	 */
114
	 */
89
	private function get() {
115
	private function get() {
90
		if ($this->resource) {
116
		if ($this->resource) {
91
			$resource_file = 'services/'.ucfirst($this->resource).'.php';
117
			$resource_file = self::$config['settings']['servicesDir'].ucfirst($this->resource).'.php';
92
			$resource_class = ucfirst($this->resource);
118
			$resource_class = ucfirst($this->resource);
93
			if (file_exists($resource_file))  {
119
			if (file_exists($resource_file))  {
94
				include_once $resource_file;
120
				include_once $resource_file;
95
				if (class_exists($resource_class)) {
121
				if (class_exists($resource_class)) {
96
					$service = new $resource_class($this->config);
122
					$service = new $resource_class(self::$config);
97
					if ($this->uid) { // get a resource element
123
					if ($this->uid) { // get a resource element
98
						if (method_exists($service, 'getElement')) {
124
						if (method_exists($service, 'getElement')) {
99
							$service->getElement($this->uid);
125
							$service->getElement($this->uid);
100
						}
126
						}
101
					} elseif (method_exists($service, 'getRessource')) { // get all elements of a ressource
127
					} elseif (method_exists($service, 'getRessource')) { // get all elements of a ressource
Line 136... Line 162...
136
				return;
162
				return;
137
			}
163
			}
Line 138... Line 164...
138
 
164
 
139
			if (count($pairs) != 0) {
165
			if (count($pairs) != 0) {
140
				if ($this->uid) { // get a resource element
166
				if ($this->uid) { // get a resource element
141
					$resource_file = 'services/'.ucfirst($this->resource).'.php';
167
					$resource_file = self::$config['settings']['servicesDir'].ucfirst($this->resource).'.php';
142
					$resource_class = ucfirst($this->resource);
168
					$resource_class = ucfirst($this->resource);
143
					if (file_exists($resource_file)) {
169
					if (file_exists($resource_file)) {
144
						include_once $resource_file;
170
						include_once $resource_file;
145
						if (class_exists($resource_class)) {
171
						if (class_exists($resource_class)) {
146
							$service = new $resource_class($this->config);
172
							$service = new $resource_class(self::$config);
147
							if (method_exists($service,'updateElement')) { // Update element
173
							if (method_exists($service,'updateElement')) { // Update element
148
								// TODO : a voir le retour ...
174
								// TODO : a voir le retour ...
149
								if ($service->updateElement($this->uid, $pairs)) {
175
								if ($service->updateElement($this->uid, $pairs)) {
150
									$this->created();
176
									$this->created();
Line 160... Line 186...
160
			}
186
			}
161
		}
187
		}
162
	}
188
	}
Line 163... Line 189...
163
 
189
 
164
	private function delete() {
190
	private function delete() {
165
		$resource_file = 'services/'.ucfirst($this->resource).'.php';
191
		$resource_file = self::$config['settings']['servicesDir'].ucfirst($this->resource).'.php';
166
		$resource_class = ucfirst($this->resource);
192
		$resource_class = ucfirst($this->resource);
167
		if (file_exists($resource_file)) {
193
		if (file_exists($resource_file)) {
168
			include_once $resource_file;
194
			include_once $resource_file;
169
			if (class_exists($resource_class)) {
195
			if (class_exists($resource_class)) {
170
				$service = new $resource_class($this->config);
196
				$service = new $resource_class(self::$config);
171
				if ($this->uid) { // get a resource element
197
				if ($this->uid) { // get a resource element
172
		 			if (method_exists($service, 'deleteElement')) { // Delete element
198
		 			if (method_exists($service, 'deleteElement')) { // Delete element
173
						if ($service->deleteElement($this->uid)) {
199
						if ($service->deleteElement($this->uid)) {
174
							$this->noContent();
200
							$this->noContent();
Line 188... Line 214...
188
				$pairs = $this->parseRequestData();
214
				$pairs = $this->parseRequestData();
189
			}
215
			}
190
		}
216
		}
Line 191... Line 217...
191
 
217
 
192
		if (count($pairs) != 0) {
218
		if (count($pairs) != 0) {
193
			$resource_file = 'services/'.ucfirst($this->resource).'.php';
219
			$resource_file = self::$config['settings']['servicesDir'].ucfirst($this->resource).'.php';
194
			$resource_class = ucfirst($this->resource);
220
			$resource_class = ucfirst($this->resource);
195
			if (file_exists($resource_file)) {
221
			if (file_exists($resource_file)) {
196
				include_once $resource_file;
222
				include_once $resource_file;
197
				if (class_exists($resource_class)) {
223
				if (class_exists($resource_class)) {
198
					$service = new $resource_class($this->config);
224
					$service = new $resource_class(self::$config);
199
					if (method_exists($service,'createElement')) { // Create a new element
225
					if (method_exists($service,'createElement')) { // Create a new element
200
						if ($service->createElement($pairs)) {
226
						if ($service->createElement($pairs)) {
201
							$this->created();
227
							$this->created();
202
						}
228
						}
Line 277... Line 303...
277
	/**
303
	/**
278
	 * Send a HTTP 406 response header.
304
	 * Send a HTTP 406 response header.
279
	 */
305
	 */
280
	private function notAcceptable() {
306
	private function notAcceptable() {
281
		header('HTTP/1.0 406 Not Acceptable');
307
		header('HTTP/1.0 406 Not Acceptable');
282
		echo join(', ', array_keys($this->config['renderers']));
308
		echo join(', ', array_keys(self::$config['renderers']));
283
	}
309
	}
Line 284... Line 310...
284
 
310
 
285
	/**
311
	/**
286
	 * Send a HTTP 411 response header.
312
	 * Send a HTTP 411 response header.