| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BFW\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Helpers for cli applications | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Cli | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Get parameters passed to script in cli command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Add BFW obligatory parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @see http://php.net/manual/en/function.getopt.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param string $options Each character in this string will be used as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      *      option characters and matched against options passed to the script | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param array $longopts An array of options. Each element in this array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      *      will be used as option strings and matched against options passed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      *      to the script | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public static function getCliParams($options, $longopts = array()) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $longopts = array_merge($longopts, array('type_site::')); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $opt      = getopt('f:'.$options, $longopts); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         unset($opt['f']); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         return $opt; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Display a message in the console | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string $msg Message to display | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string|null $colorTxt (default null) Text color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string|null $colorBg (default null) Background color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $style (default "normal") Style for the message (bold etc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public static function displayMsg($msg, $colorTxt = null, $colorBg = null, $style = 'normal') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if ($colorTxt == null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             echo $msg."\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         //Gestion cas avec couleur | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $styleNum = self::styleForShell($style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($styleNum === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $styleNum = self::styleForShell('normal'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $colorTxtNum = self::colorForShell('white', 'txt'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ($colorTxt !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $colorTxtNumArg = self::colorForShell($colorTxt, 'txt'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             if ($colorTxtNumArg !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $colorTxtNum = $colorTxtNumArg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $colorBgNum = self::colorForShell('black', 'bg'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($colorBg !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $colorBgNumArg = self::colorForShell($colorBg, 'bg'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             if ($colorBgNumArg !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $colorBgNum = $colorBgNumArg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         echo "\033[".$styleNum.";".$colorBgNum.";" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             .$colorTxtNum | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ."m".$msg."\033[0m\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Convert text color to shell value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string $color The humain color text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $type ("txt"|"bg") If the color is for text of background | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return integer|boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @throws Exception If the color is not available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public static function colorForShell($color, $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $colorList = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'black'   => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'red'     => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'green'   => 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'yellow'  => 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'blue'    => 4, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'magenta' => 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'cyan'    => 6, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             'white'   => 7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if (!in_array($color, $colorList)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             throw new Exception('Color '.$color.' is not available in function.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if ($type === 'txt') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             return $colorList[$color] + 30; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } elseif ($type === 'bg') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return $colorList[$color] + 40; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * Convert a humain style text to shell value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param string $style The style value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return boolean|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public static function styleForShell($style) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $styleList = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'normal'        => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'bold'          => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             'not-bold'      => 21, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             'underline'     => 4, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             'not-underline' => 24, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             'blink'         => 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             'not-blink'     => 25, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             'reverse'       => 7, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             'not-reverse'   => 27 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (!in_array($style, $styleList)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $styleList[$style]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  |