Source

Ray / Ray / View / Mustache / OneFile.php

<?php

namespace Ray\View\Mustache;

use Ray;

class OneFile extends Ray\View\Mustache implements Ray\View {
    
    protected $options = array(
        'ext' => '',
        'layout' => 'layout',
        'actionDir' => '',
        'action' => '404',
        'params' => array()
    );
    
    private $preg = "/^@@\s*(.*\S)\s*$/sm";
    
    public function render(){
        
        $file = $_SERVER['SCRIPT_FILENAME'];
        
        $halt = '__halt_compiler();';
        
        $fp = fopen($file, 'r');
        
        if(defined('__COMPILER_HALT_OFFSET__')){
            fseek($fp, __COMPILER_HALT_OFFSET__);
        }else{
            $read = strtolower(fread($fp, filesize($file)));
            
            $pos = strpos($read, $halt);
            
            if($pos > 0){
                $newLine = strpos($read, "\n", $pos);
                
                fseek($fp, $newLine);
            }
        }
        
        $lines = explode("\n", stream_get_contents($fp));
        
        $template = false;
        
        $templates = array();
        
        foreach($lines as $line){            
            if(strlen(trim($line))>0){
                if(preg_match($this->preg, $line, $match)){
                    $template = $match[1];
                    $templates[$template] = '';
                }elseif($template){
                    $templates[$template] .= "\n" . $line;
                }
            }
        };
        
        if(!isset($templates[$this->options['layout']])){
            throw new Ray\Exception\View\Mustache('Layout template not found - Search: '.$this->options['layout']);
            return;
        }
        
        if(!isset($templates[$this->options['action']])){
            throw new Ray\Exception\View\Mustache('Acton template not found - Search: '.$this->options['action']);
            return;
        }
        
        $layout = $templates[$this->options['layout']];
        
        $action = $templates[$this->options['action']];
        
        return parent::render($layout, $action);
    }

}