Skip to content

Commit

Permalink
Command-line access?
Browse files Browse the repository at this point in the history
  • Loading branch information
ringmaster committed Nov 17, 2012
1 parent dd0a9d7 commit 049bbfa
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib/Microsite/Console.php
@@ -0,0 +1,21 @@
<?php

namespace Microsite;

use \Microsite\Console\Color;

/**
* Class for implementing console-based tools
*/
class Console
{

public static function run()
{
echo Color::yellow("Microsite - 2.0");

echo "\n\n";
}
}

?>
121 changes: 121 additions & 0 deletions src/lib/Microsite/Console/Color.php
@@ -0,0 +1,121 @@
<?php

namespace Microsite\Console;

class Color {

public static $foregrounds = [
'black' => '0;30',
'dkgray' => '1;30',
'blue' => '0;34',
'ltblue' => '1;34',
'green' => '0;32',
'ltgreen' => '1;32',
'cyan' => '0;36',
'ltcyan' => '1;36',
'red' => '0;31',
'ltred' => '1;31',
'purple' => '0;35',
'ltpurple' => '1;35',
'brown' => '0;33',
'yellow' => '1;33',
'ltgray' => '0;37',
'white' => '1;37',
];

public static $backgrounds = [
'black' => '40',
'red' => '41',
'green' => '42',
'yellow' => '43',
'blue' => '44',
'magenta' => '45',
'cyan' => '46',
'ltgray' => '47',
];

public static function black($string, $background = '') {
return self::color($string, 'black', $background);
}

public static function dkgray($string, $background = '') {
return self::color($string, 'dkgray', $background);
}

public static function blue($string, $background = '') {
return self::color($string, 'blue', $background);
}

public static function ltblue($string, $background = '') {
return self::color($string, 'ltblue', $background);
}

public static function green($string, $background = '') {
return self::color($string, 'green', $background);
}

public static function ltgreen($string, $background = '') {
return self::color($string, 'ltgreen', $background);
}

public static function cyan($string, $background = '') {
return self::color($string, 'cyan', $background);
}

public static function ltcyan($string, $background = '') {
return self::color($string, 'ltcyan', $background);
}

public static function red($string, $background = '') {
return self::color($string, 'red', $background);
}

public static function ltred($string, $background = '') {
return self::color($string, 'ltred', $background);
}

public static function purple($string, $background = '') {
return self::color($string, 'purple', $background);
}

public static function ltpurple($string, $background = '') {
return self::color($string, 'ltpurple', $background);
}

public static function brown($string, $background = '') {
return self::color($string, 'brown', $background);
}

public static function yellow($string, $background = '') {
return self::color($string, 'yellow', $background);
}

public static function ltgray($string, $background = '') {
return self::color($string, 'ltgray', $background);
}

public static function white($string, $background = '') {
return self::color($string, 'white', $background);
}

public static function color($string, $foreground, $background = '') {
$result = '';
if(isset(self::$foregrounds[$foreground])) {
$result .= "\033[" . self::$foregrounds[$foreground] . 'm';
}
if(isset(self::$backgrounds[$background])) {
$result .= "\033[" . self::$backgrounds[$background] . 'm';
}

if($result == '') {
$result = $string;
}
else {
$result .= $string . "\033[0m";
}

return $result;
}
}

?>
Binary file modified src/microsite.phar
Binary file not shown.
15 changes: 15 additions & 0 deletions src/stub.php
@@ -1,9 +1,14 @@
<?php

// Disable the static web routeing of this phar from an including file by defining NO_STATIC_WEB, if necessary
if(defined('NO_STATIC_WEB')) {
Phar::mapPhar('microsite.phar');
}
else {
/**
* Rewrite a direct request to an internal file
* @return string The rewritten URL filename
*/
function microsite_rewrite() {
$r = $_SERVER['REQUEST_URI'];
if(strpos($r, '/microsite.phar') !== false) {
Expand All @@ -16,6 +21,7 @@ function microsite_rewrite() {
return 'index.php';
}
}

$index = str_replace('\\', DIRECTORY_SEPARATOR, 'index.php');
$fourohfour = str_replace('\\', DIRECTORY_SEPARATOR, '404.php');
$mimes = array(
Expand Down Expand Up @@ -62,6 +68,15 @@ function microsite_rewrite() {
);
Phar::webPhar("microsite.phar", 'index.php', '404.php', $mimes, 'microsite_rewrite');
}

// Make sure the autoloader is loaded
include 'phar://microsite.phar/lib/Microsite/Autoloader.php';

// Initialize the autoloader class
\Microsite\Autoloader::init();

// If we're running on the console, do other useful things
if( php_sapi_name() == 'cli' ) {
\Microsite\Console::run();
}
__HALT_COMPILER();

0 comments on commit 049bbfa

Please sign in to comment.