<?php
    
/**
    * o------------------------------------------------------------------------------o
    * | This package is dual licensed as GPL and a commercial license.               |
    * | If you use the code commercially (or if you don't want to be restricted by   |
    * | the GPL license), you will need the commercial license. It's only £49 (GBP - |
    * | roughly $98 depending on the exchange rate) and helps me out a lot. Thanks.  |
    * o------------------------------------------------------------------------------o
    *
    * © Copyright Richard Heyes
    */

    
class Console_Menu
    
{
        private static
$items;
        
        
/**
        * Shows the menu
        */
        
public function Display($title, $items = null)
        {
            if (!empty(
$items)) {
                
self::$items = $items;
            }
            
            
$fp = fopen('php://stdin', 'r');

            do {
                echo
chr(033), "c";
                echo
$title . "\n" . str_repeat('=', strlen($title)). "\n\n";
                
                foreach (
self::$items as $k => $i) {
                    echo ++
$k . ") {$i['text']}\n";
                }
                
                echo
"\n";

                
$input = trim(fgets($fp, 1024));

                
// Check the input is numeric
                
if (!is_numeric($input) OR empty(self::$items[$input - 1])) {
                    echo
chr(033), "cEnter a number from 1 - " . count(self::$items) . " [Enter]";
                    
fgets($fp, 1024);
                    continue;
                }
                
                
// Does the specified function exist?
                
if (!function_exists(self::$items[$input - 1]['func'])) {
                    echo
chr(033), "cThe specified function doesn't exist! [Enter]";
                    
fgets($fp, 1024);
                    continue;
                }
                
                
call_user_func(self::$items[$input - 1]['func']);

            } while (
true);
        }
    }
?>