From 95daa95cd284a69003aeb4d24c1b7e2d6dbb3b63 Mon Sep 17 00:00:00 2001 From: bzick Date: Wed, 13 Feb 2013 18:52:47 +0400 Subject: [PATCH] Refactory provider's mechanism --- src/Aspect/Provider/FS.php | 71 +++++++++++++++++++ .../{Provider.php => Provider/MultiFS.php} | 41 +++-------- src/Aspect/ProviderInterface.php | 5 +- tests/cases/Aspect/Provider/ProviderTest.php | 63 ++++++++++++++++ 4 files changed, 147 insertions(+), 33 deletions(-) create mode 100644 src/Aspect/Provider/FS.php rename src/Aspect/{Provider.php => Provider/MultiFS.php} (55%) create mode 100644 tests/cases/Aspect/Provider/ProviderTest.php diff --git a/src/Aspect/Provider/FS.php b/src/Aspect/Provider/FS.php new file mode 100644 index 0000000..c118195 --- /dev/null +++ b/src/Aspect/Provider/FS.php @@ -0,0 +1,71 @@ +_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; + } +} \ No newline at end of file diff --git a/src/Aspect/Provider.php b/src/Aspect/Provider/MultiFS.php similarity index 55% rename from src/Aspect/Provider.php rename to src/Aspect/Provider/MultiFS.php index a318a21..5fb4cc8 100644 --- a/src/Aspect/Provider.php +++ b/src/Aspect/Provider/MultiFS.php @@ -1,13 +1,17 @@ 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; - } -} +} \ No newline at end of file diff --git a/src/Aspect/ProviderInterface.php b/src/Aspect/ProviderInterface.php index c1a69f1..a3f120b 100644 --- a/src/Aspect/ProviderInterface.php +++ b/src/Aspect/ProviderInterface.php @@ -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(); } diff --git a/tests/cases/Aspect/Provider/ProviderTest.php b/tests/cases/Aspect/Provider/ProviderTest.php new file mode 100644 index 0000000..f718020 --- /dev/null +++ b/tests/cases/Aspect/Provider/ProviderTest.php @@ -0,0 +1,63 @@ +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")); + + } +} +