forked from Siwayll/deuton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeuton.php
More file actions
185 lines (165 loc) · 4.18 KB
/
Copy pathDeuton.php
File metadata and controls
185 lines (165 loc) · 4.18 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* FrameWork en lignes de commande
*
* @author Siwaÿll <sanath.labs@gmail.com>
* @license beerware http://wikipedia.org/wiki/Beerware
*/
namespace Siwayll\Deuton;
/**
* FrameWork en lignes de commande
*
* @author Siwaÿll <sanath.labs@gmail.com>
* @license beerware http://wikipedia.org/wiki/Beerware
*/
class Deuton
{
/**
* Numéro de version de Deuton
*/
const VERSION = '3.2';
/**
* Nom de l'interface des modules Deuton
*/
const INTERFACE_NAME = 'Siwayll\Deuton\iModule';
/**
* Configuration de Deuton
*
* @var Config
*/
protected static $config = false;
/**
* Arguments de la commande
*
* @var \Siwayll\Deuton\Opt
*/
protected static $arg;
/**
*
* @var \Siwayll\Deuton\Display
*/
protected static $display;
/**
* Lancement de Deuton
*
* @param Config $conf Configuration de base de Deuton
*
* @return void
*/
public static function run(Config $conf)
{
self::prepare($conf);
$className = self::$arg->getCmd();
if (!empty($className)) {
self::exec($className);
self::stop();
}
$stopCmd = self::$config->get('core', 'stopCmd');
do {
Display::write(DEUTON_PROMPT);
$taskName = trim(fgets(STDIN));
if ($taskName == $stopCmd) {
break;
}
if (strpos($taskName, ':') === 0) {
self::$arg->parseCmd(substr($taskName, 1));
self::exec(self::$arg->getCmd());
continue;
}
} while ($taskName != $stopCmd);
}
protected static function exec($className)
{
try {
self::validateModule($className);
$className::run(self::$arg);
} catch (\Exception $exc) {
self::displayError($exc->getMessage());
unset($exc);
}
}
/**
* Initialisation de Deuton
*
* @return void
*/
public static function prepare($conf = null)
{
/** On empêche la répétition de la préparation **/
if (self::$config !== false) {
return;
}
self::$config = $conf;
define('DEUTON_PROMPT', self::$config->get('display', 'prompt'));
define('DEUTON_MIN_PROMPT', self::$config->get('display', 'minPrompt'));
self::$arg = new Opt();
$stopCmd = self::$config->get('core', 'stopCmd');
if (empty($stopCmd)) {
self::displayError('information stopCmd vide');
self::stop();
}
}
/**
* Renvoie la configuration Deuton
*
* @return Config
*/
public static function getConf()
{
return self::$config;
}
/**
* Contrôle la validité d'un module
*
* @param string $className Nom de la classe à contrôler
*
* @return boolean
* @throws \Siwayll\Deuton\Exception si la classe n'existe pas
*/
protected static function validateModule($className)
{
if (!class_exists($className)) {
throw new Exception('Module ' . $className . ' introuvable');
}
$interfaces = class_implements($className, true);
if (isset($interfaces[self::INTERFACE_NAME])) {
return true;
}
throw new Exception('Module ' . $className . ' incorrecte');
}
/**
* Execute un module
*
* @param string $className Nom du module à charger
* @param \Siwayll\Deuton\Opt $arg Options
*
* @return void
*/
public static function launch($className, $arg)
{
self::validateModule($className);
$className::run($arg);
}
/**
* Arrêt complet du script
*
* @return void
*/
public static function stop()
{
die("\r");
}
/**
* Affiche une erreur
*
* @param string $message Message d'erreur
*
* @return void
*/
public static function displayError($message)
{
$line = '{%color:red}ERROR{%color:reset} {%background:red}'
. $message . '{%color:reset}';
Display::line($line);
}
}