| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Marwan Al-Soltany <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Marwan Al-Soltany 2020 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For the full copyright and license information, please view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * the LICENSE file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace MAKS\AmqpAgent\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ReflectionObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use DateTimeZone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * A class containing miscellaneous helper functions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @since 1.2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | final class Utility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Returns a DateTime object with the right time zone. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $time A valid php date/time string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param string $timezone A valid php timezone string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |     public static function time(string $time = 'now', ?string $timezone = null): DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 4 |  |         $timezone = $timezone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |             ? $timezone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |             : date_default_timezone_get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |         $timezoneObject = $timezone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 4 |  |             ? new DateTimeZone($timezone) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |             : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |         return new DateTime($time, $timezoneObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Generates a user-level notice, warning, or an error with styling. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param array|string|null $text [optional] The text wished to be styled (when passing an array, if array key is a valid color it will style this array element value with its key). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $color [optional] Case sensitive ANSI color name in this list [black, red, green, yellow, magenta, cyan, white, default] (when passing array, this parameter will be the fallback). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param int $type [optional] Error type (E_USER family). 1024 E_USER_NOTICE, 512 E_USER_WARNING, 256 E_USER_ERROR, 16384 E_USER_DEPRECATED. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return bool True if error type is accepted. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 4 |  |     public static function emit($text = null, ?string $color = 'yellow', int $type = E_USER_NOTICE): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $colors = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 4 |  |             'reset'   => 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             'black'   => 30, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             'red'     => 31, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             'green'   => 32, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'yellow'  => 33, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             'blue'    => 34, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             'magenta' => 35, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             'cyan'    => 36, | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             'white'   => 37, | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             'default' => 39, | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $types = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 4 |  |             E_USER_NOTICE     => E_USER_NOTICE, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 4 |  |             E_USER_WARNING    => E_USER_WARNING, | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 4 |  |             E_USER_ERROR      => E_USER_ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 4 |  |             E_USER_DEPRECATED => E_USER_DEPRECATED, | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 4 |  |         $cli = php_sapi_name() === 'cli' || php_sapi_name() === 'cli-server' || http_response_code() === false; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 4 |  |         $trim = ' \t\0\x0B'; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 4 |  |         $backspace = chr(8); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 4 |  |         $wrapper = $cli ? "\033[%dm %s\033[0m" : "@COLOR[%d] %s"; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 4 |  |         $color = $colors[$color] ?? 39; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 4 |  |         $type = $types[$type] ?? 1024; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 4 |  |         $message = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 4 |  |         if (is_array($text)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 2 |  |             foreach ($text as $segmentColor => $string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 2 |  |                 $string = trim($string, $trim); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 2 |  |                 if (is_string($segmentColor)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |                     $segmentColor = $colors[$segmentColor] ?? $color; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |                     $message .= !strlen($message) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 2 |  |                         ? sprintf($wrapper, $segmentColor, $backspace . $string) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 2 |  |                         : sprintf($wrapper, $segmentColor, $string); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 2 |  |                     continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |                 $message = $message . $string; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 2 |  |         } elseif (is_string($text)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |             $string = $backspace . trim($text, $trim); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |             $message = sprintf($wrapper, $color, $string); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |             $string = $backspace . 'From ' . __METHOD__ . ': No message was specified!'; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |             $message = sprintf($wrapper, $color, $string); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 4 |  |         $message = $cli ? $message : preg_replace('/@COLOR\[\d+\]/', '', $message); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 4 |  |         return trigger_error($message, $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Returns the passed key(s) from the backtrace. Note that the backtrace is reversed (last is first). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string|array $pluck The key to to get as a string or an array of strings (keys) from this list [file, line, function, class, type, args]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param int $offset [optional] The offset of the backtrace (last executed is index at 0). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return string|int|array|null A string or int if a string is passed, an array if an array is passed and null if no match was found. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 9 |  |     public static function backtrace($pluck, int $offset = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 9 |  |         $backtrace = array_reverse(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 9 |  |         $plucked = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 9 |  |         if (count($backtrace) < $offset + 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 8 |  |         } elseif (is_string($pluck)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             $plucked = isset($backtrace[$offset][$pluck]) ? $backtrace[$offset][$pluck] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 7 |  |         } elseif (is_array($pluck)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 7 |  |             $plucked = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 7 |  |             foreach ($pluck as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 7 |  |                 !isset($backtrace[$offset][$key]) ?: $plucked[$key] = $backtrace[$offset][$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 8 |  |         return is_string($plucked) || is_array($plucked) && count($plucked, COUNT_RECURSIVE) ? $plucked : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Executes a CLI command in the specified path synchronously or asynchronous (cross platform). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @since 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param string $command The command to execute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param string $path [optional] The path where the command should be executed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param bool $asynchronous [optional] Wether the command should be a background process (asynchronous) or not (synchronous). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return string|null The command result (as a string if possible) if synchronous otherwise null. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 6 |  |     public static function execute(string $command, string $path = null, bool $asynchronous = false): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 6 |  |         if (!strlen($command)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |             throw new Exception('No valid command is specified!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 5 |  |         $isWindows = PHP_OS == 'WINNT' || substr(php_uname(), 0, 7) == 'Windows'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 5 |  |         $apWrapper = $isWindows ? 'start /B %s > NUL' : '/usr/bin/nohup %s >/dev/null 2>&1 &'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 5 |  |         if (strlen($path) && getcwd() !== $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 5 |  |             chdir(realpath($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 5 |  |         if ($asynchronous) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 5 |  |             $command = sprintf($apWrapper, $command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 5 |  |         if ($isWindows && $asynchronous) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             pclose(popen($command, 'r')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 5 |  |         return shell_exec($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |  |