mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Refactory provider's mechanism
This commit is contained in:
parent
b713985c5e
commit
95daa95cd2
71
src/Aspect/Provider/FS.php
Normal file
71
src/Aspect/Provider/FS.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
63
tests/cases/Aspect/Provider/ProviderTest.php
Normal file
63
tests/cases/Aspect/Provider/ProviderTest.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace Aspect\Provider;
|
||||
use Aspect;
|
||||
|
||||
class FSTest extends \PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @var Provider
|
||||
*/
|
||||
public $provider;
|
||||
|
||||
public function setUp() {
|
||||
$this->provider = new FS(ASPECT_RESOURCES.'/template');
|
||||
}
|
||||
|
||||
public function testIsTemplateExists() {
|
||||
$this->assertTrue($this->provider->isTemplateExists("template1.tpl"));
|
||||
$this->assertFalse($this->provider->isTemplateExists("unexists.tpl"));
|
||||
}
|
||||
|
||||
public function testGetSource() {
|
||||
$src = $this->provider->getSource("template1.tpl", $time);
|
||||
clearstatcache();
|
||||
$this->assertEquals(file_get_contents(ASPECT_RESOURCES.'/template/template1.tpl'), $src);
|
||||
$this->assertEquals(filemtime(ASPECT_RESOURCES.'/template/template1.tpl'), $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetSourceInvalid() {
|
||||
$this->provider->getSource("unexists.tpl", $time);
|
||||
}
|
||||
|
||||
public function testGetLastModified() {
|
||||
$time = $this->provider->getLastModified("template1.tpl");
|
||||
clearstatcache();
|
||||
$this->assertEquals(filemtime(ASPECT_RESOURCES.'/template/template1.tpl'), $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetLastModifiedInvalid() {
|
||||
$this->provider->getLastModified("unexists.tpl");
|
||||
}
|
||||
|
||||
public function testGetLastModifiedBatch() {
|
||||
$times = $this->provider->getLastModifiedBatch(array("template1.tpl", "template2.tpl", "parent.tpl"));
|
||||
clearstatcache();
|
||||
foreach($times as $template => $time) {
|
||||
$this->assertEquals(filemtime(ASPECT_RESOURCES."/template/$template"), $time);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetLastModifiedBatchInvalid() {
|
||||
$this->provider->getLastModifiedBatch(array("template1.tpl", "unexists.tpl", "parent.tpl"));
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user