Skip to content

Commit 0d43e34

Browse files
author
Jean-François Hivert
committed
Version 2.1
1 parent a636f39 commit 0d43e34

38 files changed

Lines changed: 2880 additions & 430 deletions

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ You have to install a PHP version >= 7.1:
3030

3131
#### REPOSITORY
3232
* git clone https://github.com/cloudwatt/php-cli-shell_base
33-
* git checkout tags/v2.0
34-
35-
#### ADDON
36-
*Be careful, you have to install the same version of the addon as base version*
37-
Follow the addon README to install the addon
33+
* git checkout tags/v2.1
34+
35+
#### ADDON / APPLICATION
36+
*Be careful, you have to install the same version of the addon or application as base version*
37+
Follow the addon README to install the addon or application:
38+
- PHPIPAM: https://github.com/cloudwatt/php-cli-shell_phpipam/README.md
39+
- PATCHMANAGER: https://github.com/cloudwatt/php-cli-shell_patchmanager/README.md
40+
- FIREWALL: https://github.com/cloudwatt/php-cli-shell_firewall/README.md

cli/results.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Results extends C\StatusValues
88
/**
99
* Flag KEEP_FAILED_STATUS is mandatory
1010
*
11-
* @param $flags int
11+
* @param int $flags
1212
* @return $this
1313
*/
1414
public function __construct($flags = self::ALLOW_CHANGES | self::KEEP_FAILED_STATUS)
@@ -24,7 +24,7 @@ public function __construct($flags = self::ALLOW_CHANGES | self::KEEP_FAILED_STA
2424
/**
2525
* Flag KEEP_FAILED_STATUS is mandatory
2626
*
27-
* @param $flags int
27+
* @param int $flags
2828
* @return $this
2929
*/
3030
public function setFlags($flags)

cli/shell/main.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ protected function _throwException(Exception $exception)
6363

6464
public static function errorHandler($errno, $errstr, $errfile, $errline)
6565
{
66+
/**
67+
* Ne pas désactiver le buffer ici sinon il y aura une différence entre les ErrorException et les Exception
68+
* Lors d'une Error et donc d'une ErrorException, le buffer, s'il est activé, serait désactivé
69+
* mais lors d'une Exception "normale" le buffer resterait activé
70+
*/
71+
/*$bufferLevel = ob_get_level();
72+
73+
for($i=0; $i<$bufferLevel; $i++) {
74+
ob_end_flush();
75+
}*/
76+
6677
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
6778
}
6879

cli/shell/program/program.php

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ protected function _printInformations($type, $items, $title = false)
4242
{
4343
if($items !== false && C\Tools::is('array&&count>0', $items))
4444
{
45+
if($this->_SHELL->isOneShotCall()) {
46+
return true;
47+
}
48+
4549
$results = array();
4650

4751
if($title === false)
@@ -107,11 +111,15 @@ protected function _printInformations($type, $items, $title = false)
107111
}
108112
}
109113

110-
$this->_RESULTS->append($results);
111114
$this->_SHELL->EOL();
112115
return true;
113116
}
114-
else {
117+
else
118+
{
119+
if($this->_SHELL->isOneShotCall()) {
120+
return false;
121+
}
122+
115123
$this->_SHELL->error("Aucun élément à afficher", 'orange');
116124
}
117125

@@ -133,7 +141,8 @@ protected function _printObjectInfos(array $cases, array $args, $fromCurrentCont
133141
{
134142
foreach($cases as $type => $method)
135143
{
136-
$objects = call_user_func(array($this, $method), $args[0], $fromCurrentContext);
144+
$callable = array($this, $method);
145+
$objects = call_user_func($callable, $args[0], $fromCurrentContext);
137146

138147
if(count($objects) > 0) {
139148
$objectType = $type;
@@ -165,40 +174,46 @@ public function printObjectsList($context = null)
165174
* Récupère les informations de l'ensemble des éléments ou des objets
166175
*
167176
* @todo optimiser garder en cache en fonction de context
177+
*
178+
* @param string $context Context/path where retrieve objects
179+
* @param array $args Arguments from commands (optionnal)
180+
* @return array Objects
168181
*/
169-
abstract protected function _getObjects($context = null);
182+
abstract protected function _getObjects($context = null, array $args = null);
170183

171184
/**
172185
* Affiche les informations de plusieurs types d'éléments ou d'objets
173186
* Le code doit pouvoir fonctionner sur un tableau simple ou sur un tableau d'objets
174187
*/
175188
protected function _printObjectsList(array $objects)
176189
{
177-
foreach($objects as $type => &$items)
190+
if(!$this->_SHELL->isOneShotCall())
178191
{
179-
if(count($items) > 0)
192+
foreach($objects as $type => &$items)
180193
{
181-
$this->_SHELL->EOL()->print($this->_LIST_TITLES[$type], 'black', 'white', 'bold');
182-
183-
/**
184-
* /!\ L'ordre de base dans items est conservé ce qui rend le résultat incertain
185-
* Préférer l'utilisation de la méthode C\Tools::arrayFilter qui filtre et garanti l'ordre
186-
*/
187-
//$item = array_intersect_key($item, array_flip($this->_LIST_FIELDS[$type]['fields']));
194+
if(count($items) > 0)
195+
{
196+
$this->_SHELL->EOL()->print($this->_LIST_TITLES[$type], 'black', 'white', 'bold');
188197

189-
if($this->_LIST_FIELDS[$type]['fields'] !== false) {
190-
$items = C\Tools::arrayFilter($items, $this->_LIST_FIELDS[$type]['fields']);
191-
}
198+
/**
199+
* /!\ L'ordre de base dans items est conservé ce qui rend le résultat incertain
200+
* Préférer l'utilisation de la méthode C\Tools::arrayFilter qui filtre et garanti l'ordre
201+
*/
202+
//$item = array_intersect_key($item, array_flip($this->_LIST_FIELDS[$type]['fields']));
192203

193-
$table = C\Tools::formatShellTable($items);
204+
if($this->_LIST_FIELDS[$type]['fields'] !== false) {
205+
$items = C\Tools::arrayFilter($items, $this->_LIST_FIELDS[$type]['fields']);
206+
}
194207

195-
$this->_SHELL->EOL()->print($table, 'grey');
196-
$this->_RESULTS->append($items);
197-
$this->_SHELL->EOL();
208+
$table = C\Tools::formatShellTable($items);
209+
$this->_SHELL->EOL()->print($table, 'grey')->EOL();
210+
}
198211
}
212+
unset($items);
213+
214+
$this->_SHELL->deleteWaitingMsg(); // Garanti la suppression du message
199215
}
200216

201-
$this->_SHELL->deleteWaitingMsg(); // Garanti la suppression du message
202217
return $objects;
203218
}
204219

@@ -215,9 +230,10 @@ protected function _printObjectsList(array $objects)
215230
*
216231
* @param string $cmd Command
217232
* @param false|null|string $search Search
233+
* @param string $cwd
218234
* @return Core\StatusValue
219235
*/
220-
public function shellAutoC_filesystem($cmd, $search = null)
236+
public function shellAutoC_filesystem($cmd, $search = null, $cwd = null)
221237
{
222238
$Core_StatusValue = new C\StatusValue(false, array());
223239

@@ -244,10 +260,23 @@ public function shellAutoC_filesystem($cmd, $search = null)
244260
$homePathname = C\Tools::getHomePathname();
245261
$base = preg_replace('#^(~)#i', $homePathname, $search);
246262
}
263+
elseif($cwd !== null)
264+
{
265+
if(C\Tools::is('string&&!empty', $cwd) && ($cwd = realpath($cwd)) !== false) {
266+
$mode = 'relative';
267+
$workingPathname = $cwd;
268+
}
269+
else {
270+
throw new Exception("Current working directory '".$cwd."' is not valid", E_USER_ERROR);
271+
}
272+
}
247273
else {
248274
$mode = 'relative';
249275
$workingPathname = C\Tools::getWorkingPathname();
276+
}
250277

278+
if($mode === 'relative')
279+
{
251280
if($workingPathname !== DIRECTORY_SEPARATOR) {
252281
$base = $workingPathname.DIRECTORY_SEPARATOR.$search;
253282
}
@@ -414,7 +443,7 @@ protected function _shellAutoC_filesystem_browser($base, $search = null)
414443
try {
415444
$FilesystemIterator = new FilesystemIterator($base, FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS);
416445
}
417-
catch(Exception $e) {}
446+
catch(\Exception $e) {}
418447

419448
if(!isset($e))
420449
{

cli/shell/shell.php

Lines changed: 111 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ protected function _launchShell()
315315
}
316316
}
317317

318+
public function executeCmdCall($cmd)
319+
{
320+
return $this->_routeCliCmdCall($cmd);
321+
}
322+
318323
public function dispatchCmdCall($cmd, array $args)
319324
{
320325
$this->_preRoutingShellCmd($cmd, $args);
@@ -328,6 +333,7 @@ protected function _preRoutingShellCmd(&$cmd, array &$args)
328333
foreach($args as &$arg) {
329334
$arg = preg_replace('#^("|\')|("|\')$#i', '', $arg);
330335
}
336+
unset($arg);
331337

332338
$this->displayWaitingMsg(false, false);
333339
}
@@ -380,7 +386,7 @@ protected function _routeShellStatus($cmd, $status)
380386
$this->error($this->_manCommands[$cmd], 'red');
381387
}
382388
else {
383-
$this->error("Une erreur s'est produit lors de l'exécution de cette commande", 'red');
389+
$this->error("Une erreur s'est produite lors de l'exécution de cette commande", 'red');
384390
}
385391
}
386392

@@ -442,6 +448,14 @@ public function format($text, $textColor = 'green', $bgColor = false, $textStyle
442448
return $this->_e($text, $textColor, $bgColor, $textStyle, true);
443449
}
444450

451+
/**
452+
* @param int $multiplier
453+
* @param string $textColor
454+
* @param string $bgColor
455+
* @param string $textStyle
456+
* @param bool $autoDelWaitingMsg
457+
* @return $this
458+
*/
445459
public function EOL($multiplier = 1, $textColor = false, $bgColor = false, $textStyle = false, $autoDelWaitingMsg = true)
446460
{
447461
if($autoDelWaitingMsg) {
@@ -452,6 +466,32 @@ public function EOL($multiplier = 1, $textColor = false, $bgColor = false, $text
452466
return $this; // /!\ Important
453467
}
454468

469+
/**
470+
* @param string $text
471+
* @param string $textColor
472+
* @param string $bgColor
473+
* @param string $textStyle
474+
* @param bool $autoDelWaitingMsg
475+
* @return $this
476+
*/
477+
public function echo($text, $textColor = 'green', $bgColor = false, $textStyle = false, $autoDelWaitingMsg = true)
478+
{
479+
if($autoDelWaitingMsg) {
480+
$this->deleteWaitingMsg();
481+
}
482+
483+
$this->_e($text, $textColor, $bgColor, $textStyle, false);
484+
return $this; // /!\ Important
485+
}
486+
487+
/**
488+
* @param string $text
489+
* @param string $textColor
490+
* @param string $bgColor
491+
* @param string $textStyle
492+
* @param bool $autoDelWaitingMsg
493+
* @return $this
494+
*/
455495
public function print($text, $textColor = 'green', $bgColor = false, $textStyle = false, $autoDelWaitingMsg = true)
456496
{
457497
if($autoDelWaitingMsg) {
@@ -465,14 +505,31 @@ public function print($text, $textColor = 'green', $bgColor = false, $textStyle
465505
* - Si $autoDelWaitingMsg === false
466506
*/
467507
$this->EOL(1, false, false, false, false);
468-
return $this->_e($text, $textColor, $bgColor, $textStyle, false);
508+
$this->_e($text, $textColor, $bgColor, $textStyle, false);
509+
return $this; // /!\ Important
469510
}
470511

512+
/**
513+
* @param string $text
514+
* @param string $textColor
515+
* @param string $bgColor
516+
* @param string $textStyle
517+
* @param bool $autoDelWaitingMsg
518+
* @return $this
519+
*/
471520
public function debug($text, $textColor = 'black', $bgColor = 'white', $textStyle = false, $autoDelWaitingMsg = true)
472521
{
473522
return $this->print($text, $textColor, $bgColor, $textStyle, $autoDelWaitingMsg);
474523
}
475524

525+
/**
526+
* @param string|Exception $text
527+
* @param string $textColor
528+
* @param string $bgColor
529+
* @param string $textStyle
530+
* @param bool $autoDelWaitingMsg
531+
* @return $this
532+
*/
476533
public function error($text, $textColor = 'red', $bgColor = false, $textStyle = false, $autoDelWaitingMsg = true)
477534
{
478535
if($autoDelWaitingMsg) {
@@ -486,33 +543,69 @@ public function error($text, $textColor = 'red', $bgColor = false, $textStyle =
486543
* - Si $autoDelWaitingMsg === false
487544
*/
488545
$this->EOL(1, false, false, false, false);
489-
return $this->_e($text, $textColor, $bgColor, $textStyle, false);
546+
547+
if($text instanceof \Exception) {
548+
$this->throw($text, false, false);
549+
}
550+
else {
551+
$this->_e($text, $textColor, $bgColor, $textStyle, false);
552+
}
553+
554+
return $this; // /!\ Important
490555
}
491556

492-
public function throw(\Exception $exception)
557+
/**
558+
* @param Exception $exception
559+
* @param bool $throwUnknownException
560+
* @param bool $exitAfterProcess
561+
* @return $this
562+
*/
563+
public function throw(\Exception $exception, $throwUnknownException = true, $exitAfterProcess = false)
493564
{
494-
if($exception instanceof E\Message) {
495-
return $this->error($exception->getMessage(), 'orange');
565+
if($exception instanceof E\Message)
566+
{
567+
$codeColors = array(
568+
E_USER_ERROR => 'red',
569+
E_USER_WARNING => 'orange',
570+
E_USER_NOTICE => 'blue',
571+
);
496572
}
497573
else
498574
{
499-
switch($exception->getCode())
575+
$codeColors = array(
576+
E_USER_ERROR => 'red',
577+
E_USER_WARNING => 'red',
578+
E_USER_NOTICE => 'orange',
579+
);
580+
}
581+
582+
$eCode = $exception->getCode();
583+
$eMessage = $exception->getMessage();
584+
585+
switch($exception->getCode())
586+
{
587+
case E_USER_ERROR:
588+
case E_USER_WARNING:
589+
case E_USER_NOTICE: {
590+
$this->error($eMessage, $codeColors[$eCode]);
591+
break;
592+
}
593+
default:
500594
{
501-
case E_USER_ERROR: {
502-
$this->error($exception->getMessage(), 'red');
503-
exit;
504-
}
505-
case E_USER_WARNING: {
506-
return $this->error($exception->getMessage(), 'red');
507-
}
508-
case E_USER_NOTICE: {
509-
return $this->error($exception->getMessage(), 'orange');
510-
}
511-
default: {
595+
if($throwUnknownException) {
512596
throw $exception;
513597
}
598+
else {
599+
$this->error($eMessage, 'red');
600+
}
514601
}
515602
}
603+
604+
if($exitAfterProcess) {
605+
exit;
606+
}
607+
608+
return $this;
516609
}
517610

518611
public function __get($name)

0 commit comments

Comments
 (0)