fenom/src/Fenom/Render.php

212 lines
4.4 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
2013-04-28 11:33:36 +04:00
/*
2013-06-28 11:53:53 +04:00
* This file is part of Fenom.
2013-04-28 11:33:36 +04:00
*
* (c) 2013 Ivan Shalganov
*
2013-04-28 18:08:57 +04:00
* For the full copyright and license information, please view the license.md
2013-04-28 11:33:36 +04:00
* file that was distributed with this source code.
*/
2013-06-28 11:53:53 +04:00
namespace Fenom;
use Fenom;
2013-01-25 18:36:16 +04:00
/**
* Primitive template
2013-07-04 01:28:10 +04:00
* @author Ivan Shalganov <a.cobest@gmail.com>
2013-01-25 18:36:16 +04:00
*/
class Render extends \ArrayObject {
2013-02-13 20:51:27 +04:00
private static $_props = array(
"name" => "runtime",
"base_name" => "",
"scm" => false,
"time" => 0,
"depends" => array()
);
/**
* @var \Closure
*/
protected $_code;
2013-01-25 18:36:16 +04:00
/**
* Template name
* @var string
*/
2013-02-13 20:51:27 +04:00
protected $_name = 'runtime';
2013-04-28 11:33:36 +04:00
/**
* Provider's schema
* @var bool
*/
2013-02-13 20:51:27 +04:00
protected $_scm = false;
2013-04-28 11:33:36 +04:00
/**
* Basic template name
* @var string
*/
2013-02-13 20:51:27 +04:00
protected $_base_name = 'runtime';
2013-01-25 18:36:16 +04:00
/**
2013-06-28 11:53:53 +04:00
* @var Fenom
2013-01-25 18:36:16 +04:00
*/
2013-06-28 11:53:53 +04:00
protected $_fenom;
2013-01-25 18:36:16 +04:00
/**
* Timestamp of compilation
* @var float
2013-01-25 18:36:16 +04:00
*/
protected $_time = 0.0;
2013-04-28 11:33:36 +04:00
/**
* @var array depends list
*/
protected $_depends = array();
2013-01-25 18:36:16 +04:00
2013-04-28 11:33:36 +04:00
/**
2013-07-05 00:02:24 +04:00
* @var int template options (see Fenom options)
2013-04-28 11:33:36 +04:00
*/
2013-04-04 10:56:44 +04:00
protected $_options = 0;
2013-01-25 18:36:16 +04:00
/**
2013-02-13 20:51:27 +04:00
* Template provider
* @var ProviderInterface
2013-01-25 18:36:16 +04:00
*/
2013-02-13 20:51:27 +04:00
protected $_provider;
2013-01-25 18:36:16 +04:00
/**
2013-06-28 11:53:53 +04:00
* @param Fenom $fenom
2013-02-13 20:51:27 +04:00
* @param callable $code template body
* @param array $props
2013-01-25 18:36:16 +04:00
*/
2013-07-29 14:53:21 +04:00
public function __construct(Fenom $fenom, \Closure $code, array $props = array()) {
2013-06-28 11:53:53 +04:00
$this->_fenom = $fenom;
2013-02-13 20:51:27 +04:00
$props += self::$_props;
$this->_name = $props["name"];
$this->_scm = $props["scm"];
$this->_time = $props["time"];
$this->_depends = $props["depends"];
$this->_code = $code;
}
2013-01-25 18:36:16 +04:00
/**
* Get template storage
2013-07-29 14:53:21 +04:00
* @return \Fenom
2013-01-25 18:36:16 +04:00
*/
public function getStorage() {
2013-06-28 11:53:53 +04:00
return $this->_fenom;
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:53:21 +04:00
/**
* Get depends list
* @return array
*/
2013-02-15 01:49:26 +04:00
public function getDepends() {
return $this->_depends;
}
2013-07-29 14:53:21 +04:00
/**
* Get schema name
* @return string
*/
2013-02-13 20:51:27 +04:00
public function getScm() {
return $this->_scm;
}
2013-07-29 14:53:21 +04:00
/**
* Get provider of template source
* @return ProviderInterface
*/
2013-02-13 20:51:27 +04:00
public function getProvider() {
return $this->_fenom->getProvider($this->_scm);
2013-02-13 20:51:27 +04:00
}
2013-07-29 14:53:21 +04:00
/**
* Get name without schema
* @return string
*/
2013-02-13 20:51:27 +04:00
public function getBaseName() {
return $this->_base_name;
}
2013-07-29 14:53:21 +04:00
/**
* Get parse options
* @return int
*/
2013-04-04 10:56:44 +04:00
public function getOptions() {
return $this->_options;
}
2013-01-25 18:36:16 +04:00
/**
* @return string
*/
public function __toString() {
return $this->_name;
}
2013-01-25 18:36:16 +04:00
/**
* Get template name
* @return string
*/
public function getName() {
return $this->_name;
}
public function getTime() {
return $this->_time;
}
2013-01-25 18:36:16 +04:00
/**
* Validate template
2013-01-25 18:36:16 +04:00
* @return bool
*/
public function isValid() {
if(count($this->_depends) === 1) { // if no external dependencies, only self
$provider = $this->_fenom->getProvider($this->_scm);
if($provider->getLastModified($this->_name) !== $this->_time) {
return false;
}
} else {
foreach($this->_depends as $scm => $templates) {
$provider = $this->_fenom->getProvider($scm);
if(!$provider->verify($templates)) {
return false;
}
}
}
return true;
2013-01-25 18:36:16 +04:00
}
/**
* Execute template and write into output
* @param array $values for template
* @return Render
*/
public function display(array $values) {
$this->exchangeArray($values);
$this->_code->__invoke($this);
2013-07-20 12:51:06 +04:00
return $this->exchangeArray(array());
}
2013-01-25 18:36:16 +04:00
/**
* Execute template and return result as string
* @param array $values for template
* @return string
* @throws \Exception
*/
public function fetch(array $values) {
ob_start();
try {
$this->display($values);
return ob_get_clean();
} catch (\Exception $e) {
ob_end_clean();
throw $e;
}
}
/**
* Stub
* @param $method
* @param $args
* @throws \BadMethodCallException
*/
public function __call($method, $args) {
throw new \BadMethodCallException("Unknown method ".$method);
}
2013-01-25 18:36:16 +04:00
}