Notice: Use of undefined constant Links - assumed 'Links' in /srv/nizarium.com/fw/index.php on line 89

Notice: Use of undefined constant Hello - assumed 'Hello' in /srv/nizarium.com/fw/index.php on line 90

Notice: Use of undefined constant UserInfo - assumed 'UserInfo' in /srv/nizarium.com/fw/index.php on line 91

Notice: Use of undefined constant UserInfo - assumed 'UserInfo' in /srv/nizarium.com/fw/index.php on line 92

Notice: Use of undefined constant TestClass - assumed 'TestClass' in /srv/nizarium.com/fw/index.php on line 93

Notice: Use of undefined constant TestClass - assumed 'TestClass' in /srv/nizarium.com/fw/index.php on line 94

Notice: Use of undefined constant ShowSource - assumed 'ShowSource' in /srv/nizarium.com/fw/index.php on line 96

Notice: Use of undefined constant Download - assumed 'Download' in /srv/nizarium.com/fw/index.php on line 97
<?php

if (!class_exists('Application'))
{
    class 
Request
    
{
        public 
$method;
        public 
$path;
        public 
$callback;
        public 
$asserts = array();

        public function 
__construct()
        {
            if (
func_num_args() == 2)
            {
                
$this->method '';
                
$this->path func_get_arg(0);
                
$this->callback func_get_arg(1);
            }
            else
            {
                
$this->method strtolower(func_get_arg(0));
                
$this->path func_get_arg(1);
                
$this->callback func_get_arg(2);
            }

            
Application::request($this);
        }

        public function 
assert($name$re)
        {
            
$this->asserts[$name] = $re;

            return 
$this;
        }

        private static function 
array_filter_callback_no_empty_str($value)
        {
            return 
$value != '';
        }

        public function 
run()
        {
            if (
$this->method != '' && strtolower($_SERVER['REQUEST_METHOD']) != $this->method)
                return 
false;

            
$args = array();

            
$uri func_num_args() >= func_get_arg(0) : $_SERVER['REQUEST_URI'];
            
$uri explode('?'$_SERVER['REQUEST_URI'], 2);
            
$uri explode('/'$uri[0]);
            
$path explode('/'$this->path);

            
$uri array_values(array_filter($uri, array('Request''array_filter_callback_no_empty_str')));
            
$path array_values(array_filter($path, array('Request''array_filter_callback_no_empty_str')));

            if (
count($uri) != count($path))
                return 
false;

            
$method_name ''// for use as class method if callback is class name or object (see below)
            
for($i 0$i count($path); $i++)
            {
                if (
preg_match('|^\{(.*)\}$|'$path[$i], $match))
                {
                    if (!isset(
$this->asserts[$match[1]]) || preg_match($this->asserts[$match[1]], $uri[$i]))
                    {
                        
$args[$match[1]] = $uri[$i];
                    }
                    else
                    {
                        return 
false;
                    }
                }
                else
                {
                    if (
$uri[$i] != $path[$i])
                        return 
false;

                    
$words explode('_'$path[$i]);
                    foreach (
$words as $word)
                    {
                        
$method_name .= ucfirst($word);
                    }
                }
            }

            if (
is_callable($this->callback) || function_exists($this->callback))
            {
                
$result call_user_func_array($this->callback$args);
            }
            else if (
is_object($this->callback) || (is_string($this->callback) && class_exists($this->callback)))
            {
                
$class_name is_string($this->callback) ? $this->callback get_class($this->callback);
                
$object is_object($this->callback) ? $this->callback : new $this->callback();

                if (
substr($method_name0strlen($class_name)) == $class_name)
                    
$method_name substr($method_namestrlen($class_name));

                if (
$method_name == '')
                    
$method_name 'Index';

                if (
method_exists($object$method_name))
                    
$result call_user_func_array(array($object$method_name), $args);
                else
                    
$result false;
            }
            else
            {
                
$result false;
            }

            if (
is_bool($result))
                return 
$result;
            else
                return 
true;
        }
    }

    function 
Request()
    {
        
$args func_get_args();
        if (
count($args) == 2)
            return new 
Request($args[0], $args[1]);
        else
            return new 
Request($args[0], $args[1], $args[2]);
    }

    class 
Application
    
{
        private 
$requests = array();
        private 
$before = array();
        private 
$after = array();
        protected static 
$instance;

        private function 
__construct()
        {
        }

        private function 
__clone()
        {
        }

        public static function 
getInstance()
        {
            if (!
is_object(self::$instance))
            {
                
self::$instance = new self;
            }

            return 
self::$instance;
        }

        public static function 
before($callback)
        {
            if (!
is_callable($callback) && !function_exists($callback))
                return;

            
self::getInstance()->before[] = $callback;
        }

        public static function 
after($callback)
        {
            if (!
is_callable($callback) && !function_exists($callback))
                return;

            
self::getInstance()->after[] = $callback;
        }

        public static function 
request($request)
        {
            if (!
is_object($request) || !is_a($request'Request'))
                return;

            
self::getInstance()->requests[] = $request;
        }

        private function 
i_run()
        {
            foreach(
$this->before as &$callback)
                
call_user_func($callback);
        
            
$params explode('/'$_SERVER['REQUEST_URI']);
            
$done false;
            while (!
$done)
            {
                foreach(
$this->requests as &$request)
                {
                    
$done $request->run($params);
                    if (
$done) break;
                }

                if (!
$done)
                {
                    if (
array_pop($params) === null)
                        break;
                }
            }

            foreach(
$this->after as &$callback)
                
call_user_func($callback$done);

            return 
$done;
        }

        public static function 
init()
        {
            
self::getInstance();
        }

        public static function 
run()
        {
            return 
self::getInstance()->i_run();
        }
    }

    
Application::init();
}
else
{
    
Application::run();
}

?>