Commits

Mateusz Łopaciński committed c8b2c76

Initial php5.2 commit

Comments (0)

Files changed (15)

Ray/Exception/Call.php

 <?php
 
-namespace Ray\Exception;
-
-class Call extends \Exception{}
+class Ray_Exception_Call extends Exception{}

Ray/Exception/View/File.php

 <?php
 
-namespace Ray\Exception\View;
-
-class File extends \Exception{}
+class Ray_Exception_View_File extends Exception{}

Ray/Exception/View/Mustache.php

 <?php
 
-namespace Ray\Exception\View;
-
-class Mustache extends \Exception{}
+class Ray_Exception_View_Mustache extends Exception{}
 <?php
 
-namespace Ray;
-
 class Ray{
     
     public  $config,
             $errors,
             $dispatch;
     
+    public static $instance;
+    
+    public static function getInstance(){
+        if(self::$instance){
+            return self::$instance;
+        }
+    }
+    
     public function __construct($config=array(), $preDispatch=null, $postDispatch=null){
-        spl_autoload_register(array('Ray\Ray', 'autoload'));
+        
+        self::$instance = $this;
+        
+        spl_autoload_register(array('Ray', 'autoload'));
 
         if ( @date_default_timezone_set(date_default_timezone_get()) === false ) {
             date_default_timezone_set('UTC');
         $this->config = array_merge(array(
             'mode' => 'production',
             'templates' => ROOT . DS . 'template',
-            'view' => 'Ray\View\Inline'
+            'view' => 'Ray_View_Inline'
         ), $config);
         
         if($preDispatch && is_callable($preDispatch)){
             $this->dispatch['post'] = $postDispatch;
         }
         
-        $this->router = new Router();
+        $this->router = new Ray_Router();
         
-        $this->response = new Response();
+        $this->response = new Ray_Response();
         
-        $this->request = new Request();
+        $this->request = new Ray_Request();
         
-        $this->response->responseCode(Response::OK);
+        $this->response->responseCode(Ray_Response::OK);
     }
     
     public function __call($method, $args){
         
-        $reflection = new \ReflectionObject($this->request);
+        $reflection = new ReflectionObject($this->request);
         
         $via = 'METHOD_' . strtoupper($method);
         
             if($return){
                 $view = new $this->config['view']($return, $this->config);
                 $send = $view->render();
-            }elseif(isset($this->error[Response::NOT_FOUND])){
+            }elseif(isset($this->error[Ray_Response::NOT_FOUND])){
                 $response = Response::NOT_FOUND;
-                $send =  $this->error[Response::NOT_FOUND];
+                $send =  $this->error[Ray_Response::NOT_FOUND];
             }else{
-                $response = Response::NOT_FOUND;
-                $send = Response::NOT_FOUND;
+                $response = Ray_Response::NOT_FOUND;
+                $send = Ray_Response::NOT_FOUND;
             }
             
             if(isset($response)){
         if(is_callable($callable)){
             $this->errors[$code] = $callable;
         }else{
-            throw new Exception\Call('Hey, man $callable isnt callable');
+            throw new Ray_Exception_Call('Hey, man $callable isnt callable');
         }
     }
     
      */
     public static function autoload( $class ) {
 
-        $file = ROOT . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
+        $file = ROOT . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
         
         if ( file_exists($file) ) {
             require $file;
 <?php
 
-namespace Ray;
-
-class Request{
+class Ray_Request{
     
     const METHOD_HEAD = 'HEAD';
     
 <?php
 
-namespace Ray;
-
-class Response{
+class Ray_Response{
 
     const OK = '200 OK';
     const CREATED = '201 Created';
 <?php
 
-namespace Ray;
-
-class Router{
+class Ray_Router{
 
     private $routes = array();
     
     public function map($method, $pattern, $callable){
-        $route = $this->routes[$method][$pattern] =  new Router\Route($pattern, $callable);
+        $route = $this->routes[$method][$pattern] =  new Ray_Router_Route($pattern, $callable);
         
         return $route;
     }
 <?php
 
-namespace Ray\Router;
-
-use Ray;
-
-class Route{
+class Ray_Router_Route{
     
     private $pattern;
     
 <?php
 
-namespace Ray;
-
-interface View{
+interface Ray_View{
     
     public function render();
     
 <?php
 
-namespace Ray\View;
-
-use Ray;
-
-class File implements Ray\View{
+class Ray_View_File implements Ray_View{
     
     private $config;
     
     }
     
     private function view($file, $params=array()){
-        $view = new Ray\View\File\View($file, $params, $this->config, $this->options);
+        $view = new Ray_View_File_View($file, $params, $this->config, $this->options);
         return $view->render();
     }
     
                 
                 return $this->view($layout, $params);
             }else{
-                throw new Ray\Exception\View\File('Action template file not found. Path: '. $action);
+                throw new Ray_Exception_View_File('Action template file not found. Path: '. $action);
             }
             
         }else{
-            throw new Ray\Exception\View\File('Template file not found. Path: ' . $layout);
+            throw new Ray_Exception_View_File('Template file not found. Path: ' . $layout);
         }
         
         return;

Ray/View/File/View.php

 <?php
 
-namespace Ray\View\File;
-
-class View{
+class Ray_View_File_View{
     
     private $__file;
     
             
             return $view->render();
         }else{
-            throw new Ray\Exception\View\File('Element template file not found. Path: '. $file);
+            throw new Ray_Exception_View_File('Element template file not found. Path: '. $file);
         }
         
         return;
 <?php
 
-namespace Ray\View;
-
-use Ray;
-
-class Inline implements Ray\View{
+class Ray_View_Inline implements Ray_View{
     
     private $html;
     

Ray/View/Mustache.php

 <?php
 
-namespace Ray\View;
-
-use Ray;
-
-class Mustache implements Ray\View{
+class Ray_View_Mustache implements Ray_View{
     
     private $options = array(
         'ext' => '.mustache',
     
     public function __construct(array $response, $config){
         
-        $this->instance = new Ray\View\Mustache\Compiler();
+        $this->instance = new Ray_View_Mustache_Compiler();
         
         $this->options = array_merge($this->options, $response);
         
                 
                 return $this->instance->render($template, $params, $partial);
             }else{
-                throw new Ray\Exception\View\Mustache('Action template file not found. Path: '. $action);
+                throw new Ray_Exception_View_Mustache('Action template file not found. Path: '. $action);
             }
             
         }else{
-            throw new Ray\Exception\Mustache('Template file not found. Path: ' . $layout);
+            throw new Ray_Exception_Mustache('Template file not found. Path: ' . $layout);
         }
         
         return;

Ray/View/Mustache/Compiler.php

 <?php
 
-namespace Ray\View\Mustache;
-
 /**
  * A Mustache implementation in PHP.
  *
  *
  * @author Justin Hileman {@link http://justinhileman.com}
  */
-class Compiler {
+class Ray_View_Mustache_Compiler {
 
 	const VERSION      = '0.9.0';
 	const SPEC_VERSION = '1.1.2';
 error_reporting(E_ALL | E_STRICT);
 
 require_once 'Ray/Ray.php';
-    
+
 try{
     
-    $app = new Ray\Ray(array(
-        'view' => 'Ray\View\Mustache'
-    ));
-    
-    $app->error(404, function(){
+    function error404(){
         return array(
             'action' => '404'
         );
-    });
-    
-    $app->get('/', function(){
+    }
+
+    function index(){
         return array(
             'action' => 'index'
         );
-    });
-    
-    $app->get('/post/:id/', function($id=7){
+    }
+
+    function post($id){
         return array(
-            'action' => 'post'
+            'action' => 'post',
+            'params' => array(
+                'id' => $id
+            )
         );
-    });
+    }
+    
+    $app = new Ray(array(
+        'view' => 'Ray_View_File'
+    ));
+    
+    $app->error(404, 'error404');
+    
+    $app->get('/', 'index');
+    
+    $app->get('/post/:id', 'post');
     
-}catch(\Exception $e){
-    echo '<pre>' . $e . '</pre>';
+}catch(Exception $e){
+    die('<pre>' . $e . '</pre>');
 }