fenom/src/Cytro/Render.php

188 lines
3.9 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
2013-04-28 11:33:36 +04:00
/*
* This file is part of Cytro.
*
* (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-04-04 10:56:44 +04:00
namespace Cytro;
use Cytro;
2013-01-25 18:36:16 +04:00
/**
* Primitive template
*/
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-04-04 10:56:44 +04:00
* @var Cytro
2013-01-25 18:36:16 +04:00
*/
2013-04-28 11:33:36 +04:00
protected $_cytro;
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
/**
* @var int tempalte options (see Cytro options)
*/
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-04-28 11:33:36 +04:00
* @param Cytro $cytro
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-04-28 11:33:36 +04:00
public function __construct(Cytro $cytro, \Closure $code, $props = array()) {
$this->_cytro = $cytro;
2013-02-13 20:51:27 +04:00
$props += self::$_props;
$this->_name = $props["name"];
2013-04-28 11:33:36 +04:00
$this->_provider = $this->_cytro->getProvider($props["scm"]);
2013-02-13 20:51:27 +04:00
$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-04-04 10:56:44 +04:00
* @return Cytro
2013-01-25 18:36:16 +04:00
*/
public function getStorage() {
2013-04-28 11:33:36 +04:00
return $this->_cytro;
2013-01-25 18:36:16 +04:00
}
2013-02-15 01:49:26 +04:00
public function getDepends() {
return $this->_depends;
}
2013-02-13 20:51:27 +04:00
public function getScm() {
return $this->_scm;
}
public function getProvider() {
return $this->_provider;
}
public function getBaseName() {
return $this->_base_name;
}
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() {
2013-04-28 11:33:36 +04:00
$provider = $this->_cytro->getProvider(strstr($this->_name, ":"), true);
if($provider->getLastModified($this->_name) >= $this->_time) {
return false;
}
foreach($this->_depends as $tpl => $time) {
2013-04-28 11:33:36 +04:00
if($this->_cytro->getTemplate($tpl)->getTime() !== $time) {
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-01-25 18:36:16 +04:00
return $this;
}
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
}