├── classes ├── cli.php ├── controller │ └── cli │ │ └── demo.php └── cli │ └── reloaded.php ├── kohana └── README.md /classes/cli.php: -------------------------------------------------------------------------------- 1 | (/(/)) [--option1=optval1 --option2=optval2] 10 | * Example: ./kohana sebicas/view --username=john --password=secret --name="John Doe" 11 | */ 12 | 13 | // Define Application & System Paths 14 | define('INDEX', '/home/myuser/kohana/index.php'); // Use Full Path if Possible 15 | define('PHP_BIN', '/usr/bin/php'); 16 | 17 | // Count Arguments 18 | $args = count($argv) - 1; 19 | 20 | if($args < 1) 21 | { 22 | echo "\nKohana - CLI Reloaded v0.1 beta by @sebicas"; 23 | echo "\nhttps://github.com/sebicas/kohana-cli-reloaded"; 24 | echo "\n\nError: Please set URI\nExample: $ kohana (/(/)) [--option1=optval1 --option2=optval2]\n\n"; 25 | } 26 | else 27 | { 28 | // Set Command to Execute 29 | $cmd = PHP_BIN.' '.INDEX.' --uri='; 30 | 31 | // Adding Arguments 32 | for ($i = 1; $i <= $args; $i++) 33 | { 34 | $cmd .= $argv[$i].' '; 35 | } 36 | 37 | // Executing Kohana 38 | passthru($cmd); 39 | } -------------------------------------------------------------------------------- /classes/controller/cli/demo.php: -------------------------------------------------------------------------------- 1 | MODPATH.'kohana-cli-reloaded', // Kohana CLI Reloaded 52 | 53 | Example: 54 | 55 | Kohana::modules(array( 56 | // 'auth' => MODPATH.'auth', // Basic authentication 57 | // 'cache' => MODPATH.'cache', // Caching with multiple backends 58 | // 'codebench' => MODPATH.'codebench', // Benchmarking tool 59 | // 'database' => MODPATH.'database', // Database access 60 | // 'image' => MODPATH.'image', // Image manipulation 61 | // 'orm' => MODPATH.'orm', // Object Relationship Mapping 62 | // 'unittest' => MODPATH.'unittest', // Unit testing 63 | // 'userguide' => MODPATH.'userguide', // User guide and API documentation 64 | 'kohana-cli-reloaded' => MODPATH.'kohana-cli-reloaded', // Kohana CLI Reloaded 65 | )); 66 | 67 | See it working 68 | -------------- 69 | 70 | To see a **Demo** of the Module working go to the Module directory and type: 71 | 72 | ./kohana cli_demo 73 | 74 | **Important**: Make sure ./kohana file has executing permissions ( chmod +x ./kohana ) 75 | 76 | Contributing 77 | ------------ 78 | 79 | 1. Fork it. 80 | 2. Create a branch (`git checkout -b my_markup`) 81 | 3. Commit your changes (`git commit -am "Added Snarkdown"`) 82 | 4. Push to the branch (`git push origin my_markup`) 83 | 5. Create an [Issue][1] with a link to your branch 84 | 6. Enjoy a refreshing orange juice and wait 85 | 86 | Colaborators 87 | ------------ 88 | 89 | Thanks to the following colaborators: 90 | 91 | * [sebicas](https://github.com/sebicas) -------------------------------------------------------------------------------- /classes/cli/reloaded.php: -------------------------------------------------------------------------------- 1 | '0;30', 30 | 'dark_gray' => '1;30', 31 | 'blue' => '0;34', 32 | 'light_blue' => '1;34', 33 | 'green' => '0;32', 34 | 'light_green' => '1;32', 35 | 'cyan' => '0;36', 36 | 'light_cyan' => '1;36', 37 | 'red' => '0;31', 38 | 'light_red' => '1;31', 39 | 'purple' => '0;35', 40 | 'light_purple' => '1;35', 41 | 'brown' => '0;33', 42 | 'yellow' => '1;33', 43 | 'light_gray' => '0;37', 44 | 'white' => '1;37', 45 | ); 46 | 47 | protected static $background_colors = array 48 | ( 49 | 'black' => '40', 50 | 'red' => '41', 51 | 'green' => '42', 52 | 'yellow' => '43', 53 | 'blue' => '44', 54 | 'magenta' => '45', 55 | 'cyan' => '46', 56 | 'light_gray' => '47', 57 | ); 58 | 59 | /** 60 | * Throw Kohana Exception if is not running from CLI 61 | * 62 | * @author sebicas 63 | */ 64 | static public function check() 65 | { 66 | if ( ! Kohana::$is_cli) 67 | { 68 | // Show Error Message 69 | echo "Fatal Error: "; 70 | echo "Kohana CLI can only be ran from the command line ... Sorry :)"; 71 | 72 | // Break Execution 73 | exit; 74 | } 75 | } 76 | 77 | /** 78 | * Run Indicated Controller in the Command Line 79 | * 80 | * @author sebicas 81 | */ 82 | static public function run($controller, array $arguments = NULL) 83 | { 84 | // Set Command to Execute 85 | $cmd = CLI::executable.' '.$controller; 86 | 87 | // Adding Arguments; 88 | if(!is_null($arguments) AND is_array($arguments)) 89 | { 90 | // Adding Arguments 91 | foreach($arguments as $name => $value) 92 | { 93 | $cmd .= ' --'.$name.'='.$value; 94 | } 95 | } 96 | 97 | // Executing Kohana 98 | passthru($cmd); 99 | } 100 | 101 | /** 102 | * Reads input from the user. This can have either 1 or 2 arguments. 103 | * 104 | * Usage: 105 | * 106 | * // Waits for any key press 107 | * CLI::read(); 108 | * 109 | * // Takes any input 110 | * $color = CLI::read('What is your favorite color?'); 111 | * 112 | * // Will only accept the options in the array 113 | * $ready = CLI::read('Are you ready?', array('y','n')); 114 | * 115 | * @author Fuel Development Team 116 | * @license MIT License 117 | * @copyright 2010 - 2011 Fuel Development Team 118 | * @link http://fuelphp.com 119 | * 120 | * @return string the user input 121 | */ 122 | public static function read() 123 | { 124 | $args = func_get_args(); 125 | 126 | // Ask question with options 127 | if (count($args) == 2) 128 | { 129 | list($output, $options) = $args; 130 | } 131 | 132 | // No question (probably been asked already) so just show options 133 | elseif (count($args) == 1 && is_array($args[0])) 134 | { 135 | $output = ''; 136 | $options = $args[0]; 137 | } 138 | 139 | // Question without options 140 | elseif (count($args) == 1 && is_string($args[0])) 141 | { 142 | $output = $args[0]; 143 | $options = array(); 144 | } 145 | 146 | // Run out of ideas, EPIC FAIL! 147 | else 148 | { 149 | $output = ''; 150 | $options = array(); 151 | } 152 | 153 | // If a question has been asked with the read 154 | if (!empty($output)) 155 | { 156 | $options_output = ''; 157 | if (!empty($options)) 158 | { 159 | $options_output = ' ( '.implode(' / ', $options).' )'; 160 | } 161 | 162 | fwrite(STDOUT, $output.$options_output.': '); 163 | } 164 | 165 | // Read the input from keyboard. 166 | $input = trim(fgets(STDIN)); 167 | 168 | // If options are provided and the choice is not in the array, tell them to try again 169 | if (!empty($options) && !in_array($input, $options)) 170 | { 171 | CLI::write('This is not a valid option. Please try again.'.PHP_EOL); 172 | 173 | $input = CLI::read($output, $options); 174 | } 175 | 176 | // Read the input 177 | return $input; 178 | } 179 | 180 | /** 181 | * Experimental feature. 182 | * 183 | * Reads hidden input from the user 184 | * 185 | * Usage: 186 | * 187 | * $password = CLI::password('Enter your password : '); 188 | * 189 | * @author Mathew Davies 190 | * @return string 191 | */ 192 | public static function password($text = '') 193 | { 194 | if (Kohana::$is_windows) 195 | { 196 | $vbscript = sys_get_temp_dir().'CLI_Password.vbs'; 197 | 198 | // Create temporary file 199 | file_put_contents($vbscript, 'wscript.echo(InputBox("'.addslashes($text).'"))'); 200 | 201 | $password = shell_exec('cscript //nologo '.escapeshellarg($command)); 202 | 203 | // Remove temporary file. 204 | unlink($vbscript); 205 | } 206 | else 207 | { 208 | $password = shell_exec('/usr/bin/env bash -c \'read -s -p "'.escapeshellcmd($text).'" var && echo $var\''); 209 | } 210 | 211 | CLI::new_line(); 212 | 213 | return trim($password); 214 | } 215 | 216 | /** 217 | * Outputs a string to the cli. If you send an array it will implode them 218 | * with a line break. 219 | * 220 | * @author Fuel Development Team 221 | * @license MIT License 222 | * @copyright 2010 - 2011 Fuel Development Team 223 | * @link http://fuelphp.com 224 | * 225 | * @param string|array $text the text to output, or array of lines 226 | */ 227 | public static function write($text = '', $foreground = NULL, $background = NULL) 228 | { 229 | if (is_array($text)) 230 | { 231 | $text = implode(PHP_EOL, $text); 232 | } 233 | 234 | if ($foreground OR $background) 235 | { 236 | $text = CLI::color($text, $foreground, $background); 237 | } 238 | 239 | fwrite(STDOUT, $text); 240 | } 241 | 242 | /** 243 | * Waits a certain number of seconds, optionally showing a wait message and 244 | * waiting for a key press. 245 | * 246 | * @author Fuel Development Team 247 | * @license MIT License 248 | * @copyright 2010 - 2011 Fuel Development Team 249 | * @link http://fuelphp.com 250 | * 251 | * @param int $seconds number of seconds 252 | * @param bool $countdown show a countdown or not 253 | */ 254 | public static function wait($seconds = 0, $countdown = FALSE) 255 | { 256 | if ($countdown === TRUE) 257 | { 258 | $time = $seconds; 259 | 260 | while ($time > 0) 261 | { 262 | // Display Count Down 263 | self::write($time.' ... '); 264 | 265 | sleep(1); 266 | 267 | $time--; 268 | } 269 | 270 | 271 | } 272 | else 273 | { 274 | if ($seconds > 0) 275 | { 276 | sleep($seconds); 277 | } 278 | else 279 | { 280 | CLI::write(CLI::$wait_msg); 281 | CLI::read(); 282 | } 283 | } 284 | } 285 | 286 | /** 287 | * Returns the given text with the correct color codes for a foreground and 288 | * optionally a background color. 289 | * 290 | * @author Fuel Development Team 291 | * @license MIT License 292 | * @copyright 2010 - 2011 Fuel Development Team 293 | * @link http://fuelphp.com 294 | * 295 | * @param string $text the text to color 296 | * @param atring $foreground the foreground color 297 | * @param string $background the background color 298 | * 299 | * @return string the color coded string 300 | */ 301 | public static function color($text, $foreground, $background = NULL) 302 | { 303 | if (Kohana::$is_windows) 304 | { 305 | return $text; 306 | } 307 | 308 | if (!array_key_exists($foreground, CLI::$foreground_colors)) 309 | { 310 | throw new Kohana_Exception('Invalid CLI foreground color: '.$foreground); 311 | } 312 | 313 | if ($background !== NULL and !array_key_exists($background, CLI::$background_colors)) 314 | { 315 | throw new Kohana_Exception('Invalid CLI background color: '.$background); 316 | } 317 | 318 | $string = "\033[".CLI::$foreground_colors[$foreground]."m"; 319 | 320 | if ($background !== NULL) 321 | { 322 | $string .= "\033[".CLI::$background_colors[$background]."m"; 323 | } 324 | 325 | $string .= $text."\033[0m"; 326 | 327 | return $string; 328 | } 329 | 330 | /** 331 | * Outputs an error to the CLI using STDERR instead of STDOUT 332 | * 333 | * @author Fuel Development Team 334 | * @license MIT License 335 | * @copyright 2010 - 2011 Fuel Development Team 336 | * @link http://fuelphp.com 337 | * 338 | * @param string|array $text the text to output, or array of errors 339 | */ 340 | public static function error($text = '', $foreground = 'light_red', $background = NULL) 341 | { 342 | if (is_array($text)) 343 | { 344 | $text = implode(PHP_EOL, $text); 345 | } 346 | 347 | if ($foreground OR $background) 348 | { 349 | $text = self::color($text, $foreground, $background); 350 | } 351 | 352 | fwrite(STDERR, $text); 353 | } 354 | 355 | /** 356 | * Beeps a certain number of times. 357 | * 358 | * @author Fuel Development Team 359 | * @license MIT License 360 | * @copyright 2010 - 2011 Fuel Development Team 361 | * @link http://fuelphp.com 362 | * 363 | * @param int $num the number of times to beep 364 | */ 365 | public static function beep($num = 1) 366 | { 367 | echo str_repeat("\x07", $num); 368 | } 369 | 370 | /** 371 | * Enter a number of empty lines 372 | * 373 | * @author Fuel Development Team 374 | * @license MIT License 375 | * @copyright 2010 - 2011 Fuel Development Team 376 | * 377 | * @param integer Number of lines to output 378 | * @return void 379 | */ 380 | public static function new_line($num = 1) 381 | { 382 | // Do it once or more, write with empty string gives us a new line 383 | for($i = 0; $i < $num; $i++) 384 | { 385 | self::write(PHP_EOL); 386 | } 387 | } 388 | 389 | /** 390 | * Clears the screen of output 391 | * 392 | * @author Fuel Development Team 393 | * @license MIT License 394 | * @copyright 2010 - 2011 Fuel Development Team 395 | * 396 | * @return void 397 | */ 398 | public static function clear_screen() 399 | { 400 | if(Kohana::$is_windows) 401 | { 402 | self::new_line(40); 403 | } 404 | else 405 | { 406 | // Anything with a flair of Unix will handle these magic characters 407 | fwrite(STDOUT, chr(27)."[H".chr(27)."[2J"); 408 | } 409 | } 410 | 411 | } // End CLI --------------------------------------------------------------------------------