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 <?php
namespace Aspect; namespace Aspect\Provider;
/** /**
* Templates provider * Templates provider
* @author Ivan Shalganov * @author Ivan Shalganov
*/ */
class Provider implements ProviderInterface { class Provider extends FS {
private $_tpl_path = array(); 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) { foreach((array)$dirs as $dir) {
$this->addTemplateDir($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 * Get template path
* @param $tpl * @param $tpl
* @return string * @return string
* @throws \RuntimeException * @throws \RuntimeException
*/ */
private function _getTemplatePath($tpl) { protected function _getTemplatePath($tpl) {
foreach($this->_tpl_path as $tpl_path) { foreach($this->_tpl_path as $tpl_path) {
if(($path = realpath($tpl_path."/".$tpl)) && strpos($path, $tpl_path) === 0) { if(($path = realpath($tpl_path."/".$tpl)) && strpos($path, $tpl_path) === 0) {
return $path; return $path;
@ -60,19 +47,11 @@ class Provider implements ProviderInterface {
*/ */
public function isTemplateExists($tpl) { public function isTemplateExists($tpl) {
foreach($this->_tpl_path as $tpl_path) { 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 true;
} }
} }
return false; 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); public function isTemplateExists($tpl);
/** /**
* @param string $tpl * @param string $tpl
* @param int $time
* @return string * @return string
*/ */
public function loadCode($tpl); public function getSource($tpl, &$time);
/** /**
* @param string $tpl * @param string $tpl
@ -24,5 +25,5 @@ interface ProviderInterface {
/** /**
* @return array * @return array
*/ */
public function getAll(); public function getList();
} }

View 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"));
}
}