Commits

Mateusz Łopaciński committed 82d424f

Added documentation, fixed few things, added new features

  • Participants
  • Parent commits 4a494d9

Comments (0)

Files changed (15)

+#intellij
+.idea
 
 #php 5.4 development server
 development.php

Ray/Exception/Call.php

 
 namespace Ray\Exception;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Call extends \Exception{}

Ray/Exception/View/File.php

 
 namespace Ray\Exception\View;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class File extends \Exception{}

Ray/Exception/View/Mustache.php

 
 namespace Ray\Exception\View;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Mustache extends \Exception{}
 
 namespace Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Ray{
 
-    public  $config,
-            $router,
-            $view,
-            $request,
-            $response,
-            $dispatch;
+    /**
+     * Configuration handler
+     * @var array
+     */
+    public  $config;
+
+    /**
+     * Routers handler
+     * @var Router
+     */
+    public  $router;
+
+    /**
+     * Request handler
+     * @var Request
+     */
+    public  $request;
+
+    /**
+     * Response handler
+     * @var Response
+     */
+    public  $response;
 
+    /**
+     * Pre/Post Dispatch handler
+     * @var array
+     */
+    public  $dispatch;
+
+    /**
+     * Create new great app
+     * @param array $config Application configuration
+     * @param null $preDispatch Executed before View processing
+     * @param null $postDispatch Executed after View, or Error processing
+     */
     public function __construct($config=array(), $preDispatch=null, $postDispatch=null){
         spl_autoload_register(array('Ray\Ray', 'autoload'));
 
             define('ROOT', realpath(dirname(__FILE__)  . DS . '..' . DS ));
         }
 
+        if(!defined('PATH')){
+            if(defined('RAY_PHAR')){
+                $path = RAY_PHAR;
+            }else{
+                $path = ROOT;
+            }
+
+            define('PATH', $path);
+        }
+
         $this->config = array_merge(array(
             'mode' => 'production',
             'templates' => ROOT . DS . 'template',
         $this->response->responseCode(Response::OK);
     }
 
+    /**
+     * @param $method
+     * @param $args
+     * @return array|bool
+     * @throws Exception\Call
+     */
     public function __call($method, $args){
 
         $reflection = new \ReflectionObject($this->request);
         }
     }
 
+    /**
+     * Show it to the world
+     */
     public function __destruct(){
 
         if($this->router){
                 call_user_func($this->dispatch['pre']);
             }
 
-            $return = $this->router->proccess($this->request->getRequest());
+            $return = $this->router->process($this->request->getRequest());
 
             if($return){
                 $viewClass = isset($return['view']) && $return['view'] ? $return['view'] : $this->config['view'];
                 $send = $view->render();
             }elseif($this->router->hasRoute(Request::METHOD_GET, '/error/404')){
                 $response = Response::NOT_FOUND;
-                
+
                 $request = $this->request->getRequest();
-                
+
                 $request->method = Request::METHOD_GET;
-                
+
                 $request->uri = '/error/404';
-                
+
                 $error = $this->router->proccess($request);
-                
+
                 $viewClass = isset($error['view']) && $error['view'] ? $error['view'] : $this->config['view'];
-                
+
                 $view = new $viewClass($error['callback'], $this->config);
-                
+
                 $send =  $view->render();
-                
+
             }else{
                 $response = Response::NOT_FOUND;
                 $send = Response::NOT_FOUND;
         }
     }
 
+    /**
+     * Run application - alias for __destruct()
+     */
+    public function run(){
+        return $this->__destruct();
+    }
+
+    /**
+     * @param $method
+     * @param $args
+     * @return array|bool
+     */
     private function mapRoute($method, $args){
         if(count($args) >= 2){
             $pattern = $args[0];
         return false;
     }
 
+    /**
+     * Map route
+     * @return array|bool
+     */
     public function map(){
         $args = func_get_args();
         return $this->mapRoute($args);
     }
 
+    /**
+     * Define error handler
+     * @param $code
+     * @param $callable
+     * @param null $view
+     */
     public function error($code, $callable, $view=null){
         $this->get('/error/'.$code, $callable, $view);
     }
 
     /**
-     * Autoload
+     * Autoloader
+     * @static
+     * @param $class
      */
     public static function autoload( $class ) {
-
-        $file = ROOT . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
+        $file = PATH . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
 
         if ( file_exists($file) ) {
             require $file;
 
 namespace Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Request{
 
     const METHOD_HEAD = 'HEAD';
 
     const METHOD_OPTIONS = 'OPTIONS';
 
+    /**
+     * @var array
+     */
     protected $additionalHeaders = array(
         'content-type', 'content-length', 'php-auth-user',
         'php-auth-pw', 'auth-type', 'x-requested-with'
     );
 
+    /**
+     * @var object
+     */
     private $request;
 
     public function __construct(){
 
     }
 
+    /**
+     * Check if XMLHttpRequest exist
+     * @return bool
+     */
     public function isAjax() {
         return ( isset($this->request->headers['X_REQUESTED_WITH']) &&
                 $this->request->headers['X_REQUESTED_WITH'] === 'XMLHttpRequest' );
     }
 
+    /**
+     * Get request object
+     * @return object
+     */
     public function getRequest(){
         return $this->request;
     }
 
+    /**
+     * @param $name
+     * @return mixed
+     */
     protected function convertHeader( $name ) {
         return str_replace('_', '-', strtolower($name));
     }
 
+    /**
+     * Get uri
+     * @return mixed|string
+     * @throws RuntimeException
+     */
     private function getUri(){
         $scheme = ( empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ) ? 'http' : 'https';
 
 
 namespace Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Response{
 
     const OK = '200 OK';
     const SERVICE_UNAVAILABLE = '503 Service Unavailable';
     const GATEWAY_TIMEOUT = '504 Gateway Timeout';
     const HTTP_VERSION_NOT_SUPPORTED = '505 HTTP Version Not Supported';
-    
+
+    /**
+     * @var array
+     */
     private $headers;
-    
+
+    /**
+     * @var string
+     */
     private $responseCode;
     
     public function __construct(){
         $this->responseCode = self::OK;
         $this->headers['Content-Type'] = 'text/html';
     }
-    
-    public function contentType($content){
-        $this->headers['Content-Type'] = 'text/html';
+
+    /**
+     * Set Content-Type header
+     * @param string $content Default text/html
+     * @return Response
+     */
+    public function contentType($content='text/html'){
+        $this->headers['Content-Type'] = $content;
         return $this;
     }
-    
+
+    /**
+     * Set response code
+     * @param string $code Default Response::OK
+     * @return Response
+     */
     public function responseCode($code = self::OK){
         $this->responseCode = $code;
         return $this;
     }
-    
+
+    /**
+     * Add header
+     * @param $key
+     * @param string $value
+     * @return Response
+     */
     public function header($key, $value=''){
         $this->headers[$key] = $value;
         return $this;
     }
-    
-    public function send( $send=null ){
+
+    /**
+     * Send given text, or just headers
+     * @param null $send
+     */
+    public function send( $text=null ){
         if( ! headers_sent() ){
             
             if ( substr(PHP_SAPI, 0, 3) === 'cgi') {
                 header($key . ': ' . $value);
             }
             
-            if($send){
-                echo $send;
+            if($text){
+                echo $text;
             }
             
             flush();
 
 namespace Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Router{
 
+    /**
+     * @var array
+     */
     private $routes = array();
 
+    /**
+     * Add new route
+     * @param $method
+     * @param $pattern
+     * @param $callable
+     * @param null $view
+     * @return array
+     */
     public function map($method, $pattern, $callable, $view=null){
 
         if(is_array($pattern)){
         return $route;
     }
 
+    /**
+     * Check if route exist
+     * @param $method
+     * @param $pattern
+     * @return bool
+     */
     public function hasRoute($method, $pattern){
         return isset($this->routes[$method]) && isset($this->routes[$method][$pattern]);
     }
-    
-    public function proccess($request){
+
+    /**
+     * Check routes against request uri and method
+     * @param $request
+     * @return bool
+     */
+    public function process($request){
 
         if(isset($this->routes[$request->method])){
             $active = false;
             }
 
             if($active){
-                return $active->proccess();
+                return $active->process();
             }
         }
 

Ray/Router/Route.php

 
 use Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Route{
 
+    /**
+     * @var string
+     */
     private $pattern;
 
+    /**
+     * @var Callable
+     */
     private $callable;
 
+    /**
+     * @var string
+     */
     private $view;
 
+    /**
+     * @var array
+     */
     private $params;
 
+    /**
+     * @var array
+     */
     private $conditions = array();
 
+    /**
+     * @param string $pattern
+     * @param Callable $callable
+     * @param string $view
+     */
     public function __construct($pattern, $callable, $view=null){
         $this->pattern = $pattern;
         $this->callable = $callable;
         $this->view = $view;
     }
 
+    /**
+     * Check if Route is matching Request uri and method
+     * @param $call
+     * @return bool
+     */
     public function check($call){
 
         preg_match_all('@:([\w]+)@', $this->pattern, $params, PREG_PATTERN_ORDER);
         return false;
     }
 
+    /**
+     * Set custom conditions
+     * @param array $conditions
+     */
     public function conditions(array $conditions){
         $this->conditions = $conditions;
     }
 
-    public function proccess(){
+    /**
+     * Process route
+     * @return array
+     */
+    public function process(){
 
         if(is_array($this->params)){
             $return = call_user_func_array($this->callable, $this->params);
         );
     }
 
+    /**
+     * Create regex for processing
+     * @param $matches
+     * @return string
+     */
     protected function getRegex( $matches ) {
         $key = substr(current($matches),1);
 
 
 namespace Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 interface View{
-    
+
+    /**
+     * @abstract
+     * Returns view as text
+     *
+     * @return string
+     */
     public function render();
     
 }

Ray/View/File.php

 
 use Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class File implements Ray\View{
-    
+
+    /**
+     * @var array
+     */
     private $config;
-    
+
+    /**
+     * @var array
+     */
     private $options = array(
         'ext' => '.php',
         'layout' => 'default',
         'action' => '404',
         'params' => array()
     );
-    
+
+    /**
+     * @param array $response
+     * @param array $config
+     */
     public function __construct(array $response, $config){
         
         $this->config = $config;
         $this->options = array_merge($this->options, $response);
         
     }
-    
+
+    /**
+     * Render single view
+     * @param $file
+     * @param array $params
+     * @return mixed
+     */
     private function view($file, $params=array()){
         $view = new Ray\View\File\View($file, $params, $this->config, $this->options);
         return $view->render();
     }
-    
+
+    /**
+     * Rendering process
+     * @return mixed
+     * @throws \Ray\Exception\View\File
+     */
     public function render(){
         
         $layout = $this->config['templates'] . DS . $this->options['layout'] . $this->options['ext'];

Ray/View/File/View.php

 
 namespace Ray\View\File;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class View{
-    
+
+    /**
+     * @var string
+     */
     private $__file;
-    
+
+    /**
+     * @var string
+     */
     private $__render;
-    
+
+    /**
+     * @var array
+     */
     private $__params;
-    
+
+    /**
+     * @var array
+     */
     private $__config;
-    
+
+    /**
+     * @var array
+     */
     private $__options;
-    
+
+    /**
+     * @param string $file
+     * @param array $params
+     * @param array $config
+     * @param array $options
+     */
     public function __construct($file, $params=array(), $config, $options){
         
         $this->__file = $file;
         }
         
     }
-    
+
+    /**
+     * Render new View inside View
+     * @param $element
+     * @param array $params
+     * @param string $dir
+     * @return string
+     * @throws Ray\Exception\View\File
+     */
     public function element($element, $params=array(), $dir='element'){
         $file = $this->__config['templates'] . DS . $this->__options['actionDir']
               . ($dir ? DS . $dir : '' ) . DS . $element . $this->__options['ext'];
         
         return;
     }
-    
+
+    /**
+     * Render View
+     * @return string
+     */
     public function render(){
         if(!$this->__render){
             ob_start();

Ray/View/Inline.php

 
 use Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Inline implements Ray\View{
-    
+
+    /**
+     * @var string
+     */
     private $html;
-    
+
+    /**
+     * @param string $response
+     * @param array $config
+     */
     public function __construct($response, $config){
         $this->html = $response;
     }
-    
+
+    /**
+     * Rendering process
+     * @return string
+     */
     public function render(){
         return $this->html;
     }

Ray/View/Mustache.php

 
 use Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class Mustache implements Ray\View{
-    
+
+    /**
+     * @var array
+     */
     protected $options = array(
         'ext' => '.mustache',
         'layout' => 'default',
         'action' => '404',
         'params' => array()
     );
-    
+
+    /**
+     * @var array
+     */
     protected $config;
-    
+
+    /**
+     * @var Mustache\Compiler
+     */
     protected $instance;
-    
+
+    /**
+     * @param array $response
+     * @param array $config
+     */
     public function __construct(array $response, $config){
-        
+
         $this->instance = new Ray\View\Mustache\Compiler();
-        
+
         $this->options = array_merge($this->options, $response);
-        
+
         $this->config = $config;
-        
+
     }
-    
+
+    /**
+     * Rendering process
+     * @param null $layoutAsText
+     * @param null $actionAsText
+     * @return string
+     * @throws \Ray\Exception\View\Mustache
+     */
     public function render($layoutAsText=null, $actionAsText=null){
-        
+
         $layout = $this->config['templates'] . DS . $this->options['layout'] . $this->options['ext'];
-        
+
         $action = $this->config['templates'] . DS . $this->options['actionDir'] . DS . $this->options['action'] . $this->options['ext'];
-        
+
         if($layoutAsText || file_exists($layout)){
             $template = ($layoutAsText) ? $layoutAsText : file_get_contents($layout);
-            
+
             $params = is_array($this->options['params']) ? $this->options['params'] : array();
-            
+
             $params['bodyID'] = $this->options['action'];
-            
+
             if($actionAsText || file_exists($action)){
                 $partial = array(
                     'action' => ($actionAsText) ? $actionAsText : file_get_contents($action)
                 );
-                
+
                 return $this->instance->render($template, $params, $partial);
             }else{
                 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\View\Mustache('Template file not found. Path: ' . $layout);
         }
-        
+
         return;
     }
-    
-}
+
+}

Ray/View/Mustache/OneFile.php

 
 use Ray;
 
+/**
+ * Ray - micro framework for PHP
+ * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
+ * @since 1.0.0
+ * @version 1.0.0
+ */
 class OneFile extends Ray\View\Mustache implements Ray\View {
-    
+
+    /**
+     * @var array
+     */
     protected $options = array(
         'ext' => '',
         'layout' => 'layout',
         'actionDir' => '',
         'action' => '404',
-        'params' => array()
+        'params' => array(),
+        'regex' => "/^@@\s*(.*\S)\s*$/sm"
     );
-    
-    private $preg = "/^@@\s*(.*\S)\s*$/sm";
-    
+
+    /**
+     * Rendering process
+     * @return string
+     * @throws \Ray\Exception\View\Mustache
+     */
     public function render(){
         
         $file = $_SERVER['SCRIPT_FILENAME'];
         
         foreach($lines as $line){            
             if(strlen(trim($line))>0){
-                if(preg_match($this->preg, $line, $match)){
+                if(preg_match($this->options['regex'], $line, $match)){
                     $template = $match[1];
                     $templates[$template] = '';
                 }elseif($template){