|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Lib\Cli\Main |
| 4 | + * PHP version 7 |
| 5 | + * |
| 6 | + * @category Tools |
| 7 | + * @package Cli |
| 8 | + * @author Bill Rocha <prbr@ymail.com> |
| 9 | + * @copyright 2016 Bill Rocha <http://google.com/+BillRocha> |
| 10 | + * @license <https://opensource.org/licenses/MIT> MIT |
| 11 | + * @version GIT: 0.0.2 |
| 12 | + * @link http://paulorocha.tk/devbr |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Lib\Cli; |
| 16 | + |
| 17 | +use Lib; |
| 18 | + |
| 19 | +/** |
| 20 | + * Lib\Cli\Main Class |
| 21 | + * |
| 22 | + * @category Tools |
| 23 | + * @package Cli |
| 24 | + * @author Bill Rocha <prbr@ymail.com> |
| 25 | + * @license <https://opensource.org/licenses/MIT> MIT |
| 26 | + * @link http://paulorocha.tk/devbr |
| 27 | + */ |
| 28 | +class Main |
| 29 | +{ |
| 30 | + |
| 31 | + private $timer = 0; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor |
| 35 | + * |
| 36 | + * @param array $argv Command line array |
| 37 | + */ |
| 38 | + function __construct($argv) |
| 39 | + { |
| 40 | + echo " Command Line Tool!\n"; |
| 41 | + if (php_sapi_name() !== 'cli') { |
| 42 | + exit('It\'s no cli!'); |
| 43 | + } |
| 44 | + |
| 45 | + //Constants: |
| 46 | + $this->timer = microtime(true); |
| 47 | + |
| 48 | + //Command line settings... |
| 49 | + echo $this->request($argv); |
| 50 | + |
| 51 | + exit("\n Finished in ".number_format((microtime(true)-$this->timer)*1000, 3)." ms.\n"); |
| 52 | + } |
| 53 | + |
| 54 | + //CORE Request |
| 55 | + function request($rqst) |
| 56 | + { |
| 57 | + array_shift($rqst); |
| 58 | + $ax = $rqst; |
| 59 | + foreach ($rqst as $a) { |
| 60 | + array_shift($ax); |
| 61 | + if (strpos($a, '-h') !== false || strpos($a, '?') !== false) { |
| 62 | + return self::help(); |
| 63 | + } |
| 64 | + if (strpos($a, 'optimize:') !== false) { |
| 65 | + return (new Optimizer(substr($a, 9), $ax))->run(); |
| 66 | + } |
| 67 | + if (strpos($a, 'install') !== false) { |
| 68 | + return $this->cmdInstall(substr($a, 7), $ax); |
| 69 | + } |
| 70 | + if (strpos($a, 'update') !== false) { |
| 71 | + return $this->cmdUpdate(substr($a, 6), $ax); |
| 72 | + } |
| 73 | + if (strpos($a, 'key:') !== false) { |
| 74 | + return (new Key(substr($a, 4), $ax))->run(); |
| 75 | + } |
| 76 | + if (strpos($a, 'make:') !== false) { |
| 77 | + return (new Make(substr($a, 5), $ax))->run(); |
| 78 | + //return $this->cmdMake(substr($a, 5), $ax); |
| 79 | + } |
| 80 | + |
| 81 | + //Plugins |
| 82 | + if (strpos($a, 'table:') !== false) { |
| 83 | + return (new Plugin\Table(substr($a, 6), $ax))->run(); |
| 84 | + } |
| 85 | + } |
| 86 | + //or show help... |
| 87 | + return self::help(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * It's same as cmdUpdate |
| 92 | + * @param string $v segment of command |
| 93 | + * @param array $arg all others command line argumments |
| 94 | + * |
| 95 | + * @return string Display user data |
| 96 | + */ |
| 97 | + function cmdInstall($v, $arg) |
| 98 | + { |
| 99 | + return $this->cmdUpdate($v, $arg); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Update command |
| 104 | + * @param string $v segment of command |
| 105 | + * @param array $arg all others command line argumments |
| 106 | + * |
| 107 | + * @return string Display user data |
| 108 | + */ |
| 109 | + function cmdUpdate($v, $arg) |
| 110 | + { |
| 111 | + $devbr = _APP.'Composer/devbr/'; |
| 112 | + $dir = scandir($devbr); |
| 113 | + $o = ''; |
| 114 | + |
| 115 | + foreach ($dir as $k) { |
| 116 | + if ($k == '.' || $k == '..') { |
| 117 | + continue; |
| 118 | + } |
| 119 | + if (is_file($devbr.$k.'/install.php')) { |
| 120 | + $o .= include $devbr.$k.'/install.php'; |
| 121 | + } |
| 122 | + } |
| 123 | + return $o; |
| 124 | + } |
| 125 | + |
| 126 | + // Checa um diretório e cria se não existe - retorna false se não conseguir ou não existir |
| 127 | + /** |
| 128 | + * Check or create a directory |
| 129 | + * @param string $dir path of the directory |
| 130 | + * @param boolean $create False/true for create |
| 131 | + * @param string $perm indiucates a permission - default 0777 |
| 132 | + * |
| 133 | + * @return bool status of directory (exists/created = false or true) |
| 134 | + */ |
| 135 | + static function checkAndOrCreateDir($dir, $create = false, $perm = '0777') |
| 136 | + { |
| 137 | + if (is_dir($dir) && is_writable($dir)) { |
| 138 | + return true; |
| 139 | + } elseif ($create === false) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + @mkdir($dir, $perm, true); |
| 144 | + @chmod($dir, $perm); |
| 145 | + |
| 146 | + if (is_writable($dir)) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * return a help information text |
| 154 | + * |
| 155 | + * @return string a help text... |
| 156 | + */ |
| 157 | + static function help() |
| 158 | + { |
| 159 | + return ' |
| 160 | +
|
| 161 | + Usage: php index.php [command:type] [options] |
| 162 | +
|
| 163 | + key:generate Generate new keys |
| 164 | + key:list List all installed Cyphers |
| 165 | +
|
| 166 | + make:controller <name> Create a controller with <name> |
| 167 | + make:model <name> Create a model with <name> |
| 168 | + make:html <name> Create a html file with <name> |
| 169 | +
|
| 170 | + optimize:scan [save name] Scan CSS&JS source files |
| 171 | + Optional indicates "save" and a |
| 172 | + "name" for save in config file the scan |
| 173 | +
|
| 174 | + optimize:css Optimize CSS configurated files |
| 175 | + optimize:js Optimize JS configurated files |
| 176 | + optimize:all Optimize ALL configurated files |
| 177 | +
|
| 178 | + -h or ? Show this help |
| 179 | + '; |
| 180 | + } |
| 181 | +} |
0 commit comments