2016-04-04 09:47:17 +00:00
|
|
|
<?php
|
|
|
|
/**************************************/
|
|
|
|
/* Classe de gestion de route (match) */
|
|
|
|
/**************************************/
|
|
|
|
|
2016-10-18 11:09:39 +00:00
|
|
|
namespace manager\router;
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
class Route{
|
|
|
|
|
|
|
|
// ATTRIBUTS
|
|
|
|
private $pattern;
|
|
|
|
private $callback;
|
2016-09-12 16:04:53 +00:00
|
|
|
private $matches = [];
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
/* Initialise la route
|
|
|
|
*
|
|
|
|
* @pattern<String> Pattern correspondant a la route
|
|
|
|
* @callback<Function> Fonction de callback de la route
|
|
|
|
*
|
|
|
|
* @return this<Route> Retour de l'instance courante
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct($pattern, $callback){
|
|
|
|
// On enregistre la fonction de callback
|
|
|
|
$this->callback = $callback;
|
|
|
|
|
|
|
|
// On formatte le pattern en regexp
|
|
|
|
$this->pattern = '#^'.$pattern.'$#';
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verifie si l'URL correspond a la route
|
|
|
|
*
|
|
|
|
* @url<String> URL pour laquelle on veut verifier
|
|
|
|
*
|
|
|
|
* @return match<Boolean> TRUE si match sinon FAUX
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function match($url){
|
|
|
|
// Si ne match pas -> FALSE
|
|
|
|
if( !preg_match($this->pattern, $url, $matches) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// On supprime le premier match global
|
|
|
|
array_shift($matches);
|
|
|
|
|
|
|
|
$this->matches = $matches;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Amorcage de la fonction de callback
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function call(){
|
|
|
|
return call_user_func($this->callback, $this->matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-18 11:09:39 +00:00
|
|
|
?>
|