Overview

Namespaces

  • Cz
  • None

Classes

  • Cli
  • Cz\Cli
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 
<?php
    /**
     * Colors list: http://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/
     * 
     * @author      Jan Pecha, <janpecha@email.cz>
     * @license     New BSD License
     * @link        http://janpecha.iunas.cz/
     * @version     2013-01-22-1
     */
    
    namespace Cz;
    
    class Cli
    {
        const COLOR_ERROR = '0;31',
            COLOR_SUCCESS = '0;32',
            COLOR_WARNING = '0;33',
            COLOR_INFO = '0;34';
        
        const C_BLACK = 30,
            C_RED = 31,
            C_GREEN = 32,
            C_YELLOW = 33,
            C_BLUE = 34,
            C_PURPLE = 35,
            C_CYAN = 36,
            C_WHITE = 37;
        
        
        /** @var  bool|NULL */
        protected static $isWindows = NULL;
        
        
        
        /**
         * @param   string
         * @return  void
         */
        public static function log($str)
        {
            echo "$str\n";
        }
        
        
        
        /**
         * @param   string
         * @return  void
         */
        public static function error($str)
        {
            if(self::$isWindows === NULL)
            {
                self::detectOs();
            }
            
            if(!self::$isWindows)
            {
                $str = self::coloredString($str, static::COLOR_ERROR);
            }
            
            fwrite(STDERR, $str);
        }
        
        
        
        /**
         * @param   string
         * @return  void
         */
        public static function success($str)
        {
            if(self::$isWindows === NULL)
            {
                self::detectOs();
            }
            
            if(!self::$isWindows)
            {
                $str = self::coloredString($str, static::COLOR_SUCCESS);
            }
            
            echo $str;
        }
        
        
        
        /**
         * @param   string
         * @return  void
         */
        public static function warn($str)
        {
            if(self::$isWindows === NULL)
            {
                self::detectOs();
            }
            
            if(!self::$isWindows)
            {
                $str = self::coloredString($str, static::COLOR_WARNING);
            }
            
            fwrite(STDERR, $str);   // TODO: zmenit vystupni proud, ??echo
        }
        
        
        
        /**
         * @param   string
         * @return  void
         */
        public static function info($str)
        {
            if(self::$isWindows === NULL)
            {
                self::detectOs();
            }
            
            if(!self::$isWindows)
            {
                $str = self::coloredString($str, static::COLOR_INFO);
            }
            
            echo $str;
        }
        
        
        
        /**
         * @param   string
         * @param   bool
         * @return  void
         */
        public static function format($str, $bold = FALSE, $color = NULL)
        {
            if($color === NULL)
            {
                $color = static::C_WHITE;
            }
            
            echo "\033[", // start
                ((int)(bool)$bold), // style
                ';',
                $color, // color
                'm',
                $str, // message
                "\033[0m"; // reset
        }
        
        
        
        /**
         * @param   array
         * @return  array|FALSE
         */
        public static function parseParams(array $argv)
        {
            $params = array();
            $lastName = NULL;
            
            if(isset($argv[1]))     // count($argv) > 1
            {
                // remove argv[0]
                array_shift($argv);
                
                // parsing
                foreach($argv as $argument)
                {
                    if($argument{0} === '-')
                    {
                        $name = trim($argument, '-');
                        $lastName = $name;
                    
                        if(!isset($params[$name]))
                        {
                            $params[$name] = TRUE;
                        }
                    }
                    elseif($lastName === NULL)
                    {
                        throw new \Exception("Bad argument '$argument'");
                    }
                    else
                    {
                        if($params[$lastName] === TRUE)
                        {
                            $params[$lastName] = $argument;
                        }
                        else    // string || array
                        {
                            if(is_string($params[$lastName]))
                            {
                                $newParams = array(
                                    $params[$lastName],
                                    $argument,
                                );
                            
                                $params[$lastName] = $newParams;
                            }
                            else
                            {
                                $params[$lastName][] = $argument;
                            }
                        
                            $lastName = NULL;
                        }
                    }
                }
                
                return $params;
            }
            
            return FALSE;
        }
        
        
        
        /**
         * @param   string
         * @param   string
         * @return  string
         */
        public static function formatPath($dir, $currentDir)
        {
            if($dir{0} === '/')
            {
                return $dir;
            }
            
            return $currentDir . '/' . $dir;
        }
        
        
        
        /**
         * @return  void
         */
        protected static function detectOs()
        {
            if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
            {
                self::$isWindows = TRUE;
            }
            else
            {
                self::$isWindows = FALSE;
            }
        }
        
        
        
        /**
         * @param   string
         * @param   string
         * @return  string
         */
        protected static function coloredString($str, $color)
        {
            return "\033[" . $color . "m" . $str . "\033[0m\r\n";
            #return "\033[" . $color . "m" . $str . "\033[37m\r\n";
        }
    }

czproject/phpcli v1.0.0 API documentation API documentation generated by ApiGen