1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: class Cli {
22:
23:
24: private static $parametres_obligatoires = array('chemin_modules');
25:
26:
27: 28: 29: 30: 31:
32: public static function executer() {
33: if ($_SERVER['argc'] < 2){
34: trigger_error("Erreur: vous n'avez pas indiqué le nom du script. Voir '".$_SERVER['argv'][0]." help'.\n", E_USER_ERROR);
35: }
36:
37:
38: $argv = $_SERVER['argv'];
39:
40: array_shift($argv);
41:
42: $script = array_shift($argv);
43:
44: $parametres = self::getParametres($argv);
45:
46:
47: $Script = Cli::charger($script, $parametres);
48: if (!is_null($Script)) {
49: $Script->executer();
50: }
51:
52:
53: echo GestionnaireException::getExceptions();
54:
55:
56: exit(0);
57: }
58:
59: private static function charger($script_nom, $parametres) {
60: $Script = null;
61: Config::verifierPresenceParametres(self::$parametres_obligatoires);
62:
63: if (strpos($script_nom, DS)) {
64: $decompoScriptNom = explode(DS, $script_nom);
65: $script_nom = array_pop($decompoScriptNom);
66: $dossier_nom = implode(DS, $decompoScriptNom);
67: } else {
68: $dossier_nom = strtolower($script_nom);
69: }
70:
71: $classe_nom = self::obtenirNomClasse($script_nom);
72: $fichier_script = Config::get('chemin_modules').$dossier_nom.DS.$classe_nom.'.php';
73:
74: if (!file_exists($fichier_script)){
75: trigger_error("Erreur : script '$fichier_script' inconnu!\n", E_USER_ERROR);
76: } else {
77: require_once $fichier_script;
78: if (!class_exists( $classe_nom)) {
79: trigger_error("Erreur: impossible de trouver la classe de la commande : $classe_nom\n", E_USER_ERROR);
80: } else {
81: $Script = new $classe_nom($script_nom, $parametres);
82: }
83: }
84: return $Script;
85: }
86:
87: private static function obtenirNomClasse($script_nom) {
88: $nom_classe = implode('', array_map('ucfirst', explode('_', strtolower($script_nom))));
89: return $nom_classe;
90: }
91:
92: private static function getParametres($argv) {
93: $parametres = array();
94:
95: while (count($argv)) {
96: if (isset($argv[1]) && $argv[1]{0} != '-') {
97: $param = array_shift($argv);
98: $parametres[$param] = array_shift($argv);
99: } elseif (!isset($argv[1]) || $argv[1]{0} == '-') {
100: $parametres[array_shift($argv)] = null;
101: } else {
102: trigger_error("Erreur: valeur manquante pour le paramêtre '".$argv[0]."' \n", E_USER_ERROR);
103: }
104: }
105: return $parametres;
106: }
107: }
108: ?>