118 lignes
3,9 Kio
PHP
118 lignes
3,9 Kio
PHP
<?php
|
|
namespace App\Http\Utility;
|
|
use App\Http\Utility\debug;
|
|
|
|
use \Exception;
|
|
use App\Http\Utility\ioUtility;
|
|
use App\Http\Utility\arrayUtility;
|
|
|
|
// Collecteur des configurations de routage
|
|
class routeCollector{
|
|
|
|
//Configuration des routes à charger
|
|
protected $routeConfs = [];
|
|
|
|
//Chemin du dossier de la route courante
|
|
protected $curRoutePath;
|
|
|
|
//Nom de la route courante
|
|
protected $curRouteName;
|
|
|
|
//Préfixe des fichiers
|
|
protected $prefix='';
|
|
|
|
public function __construct() {}
|
|
|
|
// initialiser le chargement des fichier de route
|
|
// @param string|null $prefix : Préfixe des fichiers routes à charger: Exemples : 'api', 'web', ...
|
|
public function getRoutes($prefix=null){
|
|
try{
|
|
$this->prefix = (preg_match("/^[\w\-. ]+$/", $prefix)===1) ? $prefix : '';
|
|
|
|
if(!is_null($prefix) && trim($this->prefix)!==''){
|
|
$this->loadRoutes();
|
|
}
|
|
return $this->routeConfs;
|
|
|
|
}catch(Exception $e){
|
|
echo $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// charge les fichiers routes.php du core et des modules
|
|
private function loadRoutes(){
|
|
$apiRoutePath = app_path().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers'.DIRECTORY_SEPARATOR;
|
|
|
|
//Routes
|
|
if (file_exists($apiRoutePath)) {
|
|
$routeFiles = $this->getRouteFiles($apiRoutePath);
|
|
}
|
|
|
|
if(is_array($routeFiles) && count($routeFiles)){
|
|
foreach($routeFiles as $routeFile) {
|
|
$this->loadRouteFile($routeFile);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Vérifie la présence de fichiers "routes.php"
|
|
// @param string|null $path : Soit le chemin à vérifier récursivement
|
|
private function getRouteFiles($path=null){
|
|
if(!is_null($path)){
|
|
$this->prefix = (trim($this->prefix)!=='') ? $this->prefix.'.':'';
|
|
|
|
// le path du fichier
|
|
$configurationFiles = ioUtility::find($path,$this->prefix.'routes.php');
|
|
|
|
return $configurationFiles;
|
|
}else{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Charge les fichiers de configuration des routes
|
|
private function loadRouteFile($routeFile){
|
|
$this->curRoutePath = dirname($routeFile);
|
|
$curRouteName = explode('/',$this->curRoutePath);
|
|
$this->curRouteName = end($curRouteName);
|
|
|
|
$routeConf[] = require($routeFile);
|
|
$this->routeConfs = arrayUtility::arrayMerge($this->routeConfs,$routeConf);
|
|
}
|
|
|
|
// lecture du fichier
|
|
public function routeConfig($routeMarker){
|
|
$route=explode('|',$routeMarker);
|
|
$routing=$route[0];
|
|
$apiName=$route[1];
|
|
$httpMethod=$route[2];
|
|
$url=$route[3];
|
|
|
|
$webRoutes = $this->getRoutes($routing);
|
|
|
|
if(is_array($webRoutes)){
|
|
foreach ($webRoutes as $apiBlock){
|
|
if($apiBlock['api']===$apiName){
|
|
foreach ($apiBlock[$httpMethod] as $collection){
|
|
if($collection['url']===$url){
|
|
$routeConfig=[
|
|
'api'=>$apiBlock['api'],
|
|
'method'=>$httpMethod
|
|
];
|
|
|
|
if(array_key_exists('restrictions',$apiBlock) && is_array($apiBlock['restrictions']) && count($apiBlock['restrictions']))
|
|
$routeConfig['globalRestriction']=$apiBlock['restrictions'];
|
|
|
|
if(array_key_exists('restrictions',$collection) && is_array($collection['restrictions']) && count($collection['restrictions']))
|
|
$routeConfig['routeRestriction']=$collection['restrictions'];
|
|
|
|
return $routeConfig;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|