Refactory provider's mechanism

This commit is contained in:
bzick
2013-02-13 18:52:47 +04:00
parent b713985c5e
commit 95daa95cd2
4 changed files with 147 additions and 33 deletions

View File

@ -0,0 +1,71 @@
<?php
namespace Aspect\Provider;
use Aspect\ProviderInterface;
/**
* Templates provider
* @author Ivan Shalganov
*/
class FS implements ProviderInterface {
private $_path;
public function __construct($template_dir) {
if($_dir = realpath($template_dir)) {
$this->_path = $_dir;
} else {
throw new \LogicException("Template directory {$template_dir} doesn't exists");
}
}
/**
*
* @param string $tpl
* @param int $time
* @return string
*/
public function getSource($tpl, &$time) {
$tpl = $this->_getTemplatePath($tpl);
clearstatcache(null, $tpl);
$time = filemtime($tpl);
return file_get_contents($tpl);
}
public function getLastModified($tpl) {
clearstatcache(null, $tpl = $this->_getTemplatePath($tpl));
return filemtime($tpl);
}
public function getList() {
}
/**
* Get template path
* @param $tpl
* @return string
* @throws \RuntimeException
*/
protected function _getTemplatePath($tpl) {
if(($path = realpath($this->_path."/".$tpl)) && strpos($path, $this->_path) === 0) {
return $path;
} else {
throw new \RuntimeException("Template $tpl not found");
}
}
/**
* @param string $tpl
* @return bool
*/
public function isTemplateExists($tpl) {
return file_exists($this->_path."/".$tpl);
}
public function getLastModifiedBatch($tpls) {
$tpls = array_flip($tpls);
foreach($tpls as $tpl => &$time) {
$time = $this->getLastModified($tpl);
}
return $tpls;
}
}

View File

@ -1,13 +1,17 @@
<?php
namespace Aspect;
namespace Aspect\Provider;
/**
* Templates provider
* @author Ivan Shalganov
*/
class Provider implements ProviderInterface {
class Provider extends FS {
private $_tpl_path = array();
public function setTemplateDirs($dirs) {
public function __construct($template_dir) {
$this->addTemplateDirs($template_dir);
}
public function addTemplateDirs($dirs) {
foreach((array)$dirs as $dir) {
$this->addTemplateDir($dir);
}
@ -22,30 +26,13 @@ class Provider implements ProviderInterface {
}
}
/**
* @param string $tpl
* @return string
*/
public function loadCode($tpl) {
return file_get_contents($tpl = $this->_getTemplatePath($tpl));
}
public function getLastModified($tpl) {
clearstatcache(null, $tpl = $this->_getTemplatePath($tpl));
return filemtime($tpl);
}
public function getAll() {
}
/**
* Get template path
* @param $tpl
* @return string
* @throws \RuntimeException
*/
private function _getTemplatePath($tpl) {
protected function _getTemplatePath($tpl) {
foreach($this->_tpl_path as $tpl_path) {
if(($path = realpath($tpl_path."/".$tpl)) && strpos($path, $tpl_path) === 0) {
return $path;
@ -60,19 +47,11 @@ class Provider implements ProviderInterface {
*/
public function isTemplateExists($tpl) {
foreach($this->_tpl_path as $tpl_path) {
if(($path = realpath($tpl_path."/".$tpl)) && strpos($path, $tpl_path) === 0) {
if(file_exists($tpl_path."/".$tpl)) {
return true;
}
}
return false;
}
public function getLastModifiedBatch($tpls) {
$tpls = array_flip($tpls);
foreach($tpls as $tpl => &$time) {
$time = $this->getLastModified($tpl);
}
return $tpls;
}
}
}

View File

@ -9,9 +9,10 @@ interface ProviderInterface {
public function isTemplateExists($tpl);
/**
* @param string $tpl
* @param int $time
* @return string
*/
public function loadCode($tpl);
public function getSource($tpl, &$time);
/**
* @param string $tpl
@ -24,5 +25,5 @@ interface ProviderInterface {
/**
* @return array
*/
public function getAll();
public function getList();
}