| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the box project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Kevin Herrera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *     Théo Fidry <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This source file is subject to the MIT license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace KevinGH\Box\RequirementChecker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Terminal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class Printer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $styles = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'reset' => "\033[0m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'red' => "\033[31m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'green' => "\033[32m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'yellow' => "\033[33m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'title' => "\033[33m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'error' => "\033[37;41m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'success' => "\033[30;42m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $verbosity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $supportColors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param int      $verbosity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param bool     $supportColors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param null|int $width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct($verbosity, $supportColors, $width = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (null === $width) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $terminal = new Terminal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $width = \min($terminal->getWidth(), 80); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->verbosity = $verbosity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->supportColors = $supportColors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->width = $width; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function getVerbosity() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $this->verbosity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param int $verbosity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function setVerbosity($verbosity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->verbosity = $verbosity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string      $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param int         $verbosity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param null|string $style | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     public function title($title, $verbosity, $style = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         if (null === $style) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $style = 'title'; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $this->printvln('', $verbosity, $style); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $this->printvln($title, $verbosity, $style); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $this->printvln( | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             \str_repeat( | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 '=', | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                 \min(\strlen($title), $this->width) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $verbosity, | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $style | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $this->printvln('', $verbosity, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param Requirement $requirement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function getRequirementErrorMessage(Requirement $requirement) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($requirement->isFulfilled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $errorMessage = \wordwrap($requirement->getTestMessage(), $this->width - 3, \PHP_EOL.'   ').\PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $errorMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param string      $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param string      $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param int         $verbosity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param null|string $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function block($title, $message, $verbosity, $style = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $prefix = ' ['.$title.'] '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $message = $prefix.trim($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $lines = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $remainingMessage = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         while (\strlen($remainingMessage) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $wrapped = \wordwrap($remainingMessage, $this->width - 3, '¬'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $exploded = \explode('¬', $wrapped); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $line = $exploded[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $remainingMessage = \ltrim(\substr($remainingMessage, \strlen($line))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             if (\strlen($remainingMessage) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $remainingMessage = \str_repeat(' ', \strlen($prefix)).$remainingMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $lines[] = \str_pad($line, $this->width, ' ', STR_PAD_RIGHT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->printvln('', $verbosity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->printvln(\str_repeat(' ', $this->width), $verbosity, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         foreach ($lines as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->printvln($line, $verbosity, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->printv(\str_repeat(' ', $this->width), $verbosity, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->printvln('', $verbosity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param string      $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param int         $verbosity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param null|string $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function printvln($message, $verbosity, $style = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->printv($message, $verbosity, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->printv(\PHP_EOL, $verbosity, null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @param string      $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param int         $verbosity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param null|string $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function printv($message, $verbosity, $style = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         if ($verbosity > $this->verbosity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $message = \wordwrap($message, $this->width); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $message = \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             '%s%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             $this->supportColors ? $this->styles['reset'] : '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         echo $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  |