diff --git a/main/UI/View/MultiPrefixPhpViewResolver.class.php b/main/UI/View/MultiPrefixPhpViewResolver.class.php index e6d202e6cb..45f97bc21e 100644 --- a/main/UI/View/MultiPrefixPhpViewResolver.class.php +++ b/main/UI/View/MultiPrefixPhpViewResolver.class.php @@ -128,10 +128,12 @@ public function setPostfix($postfix) $this->postfix = $postfix; return $this; } - + /** - * @return SimplePhpView - **/ + * @param string $viewName + * @return View + * @throws WrongArgumentException + */ public function resolveViewName($viewName) { Assert::isFalse( @@ -139,12 +141,12 @@ public function resolveViewName($viewName) 'specify at least one prefix' ); - if ($prefix = $this->findPrefix($viewName)) - return $this->makeView($prefix, $viewName); + if ($path = $this->findPath($viewName)) + return $this->makeView($path); - if (!$this->findPrefix($viewName, false)) + if (!$this->findPath($viewName, false)) throw new WrongArgumentException( - 'can not resolve view: '.$viewName + 'can not resolve view: '.is_array($viewName) ? implode($viewName) : $viewName ); return EmptyView::create(); @@ -152,7 +154,7 @@ public function resolveViewName($viewName) public function viewExists($viewName) { - return ($this->findPrefix($viewName) !== null); + return ($this->findPath($viewName) !== null); } /** @@ -169,19 +171,22 @@ public function getViewClassName() { return $this->viewClassName; } - - protected function findPrefix($viewName, $checkDisabled = true) - { - foreach ($this->prefixes as $alias => $prefix) { - if ( - $checkDisabled - && isset($this->disabled[$alias]) - && $this->disabled[$alias] - ) - continue; - - if (file_exists($prefix.$viewName.$this->postfix)) - return $prefix; + + protected function findPath($viewNameList, $checkDisabled = true) + { + $viewNameList = is_array($viewNameList) ? $viewNameList : [$viewNameList]; + foreach ($viewNameList as $viewName) { + foreach ($this->prefixes as $alias => $prefix) { + if ( + $checkDisabled + && isset($this->disabled[$alias]) + && $this->disabled[$alias] + ) + continue; + + if (file_exists($prefix.$viewName.$this->postfix)) + return $prefix.$viewName.$this->postfix; + } } return null; @@ -190,12 +195,9 @@ protected function findPrefix($viewName, $checkDisabled = true) /** * @return View **/ - protected function makeView($prefix, $viewName) + protected function makeView($path) { - return new $this->viewClassName( - $prefix.$viewName.$this->postfix, - $this - ); + return new $this->viewClassName($path, $this); } private function getAutoAlias($prefix) diff --git a/main/UI/View/PhpViewResolver.class.php b/main/UI/View/PhpViewResolver.class.php index 4b75d29860..0029e63a8e 100644 --- a/main/UI/View/PhpViewResolver.class.php +++ b/main/UI/View/PhpViewResolver.class.php @@ -32,20 +32,33 @@ public static function create($prefix = null, $postfix = null) } /** + * @param $viewNameList string|string[] * @return SimplePhpView + * @throws WrongArgumentException **/ - public function resolveViewName($viewName) + public function resolveViewName($viewNameList) { - return - new SimplePhpView( - $this->prefix.$viewName.$this->postfix, - $this - ); + foreach ($this->getViewNameList($viewNameList) as $viewName) { + if ($this->isViewNameExists($viewName)) { + return new SimplePhpView( + $this->prefix.$viewName.$this->postfix, + $this + ); + } + } + throw new WrongArgumentException( + 'can not resolve views: '.implode($this->getViewNameList($viewNameList)) + ); } - public function viewExists($viewName) + public function viewExists($viewNameList) { - return is_readable($this->prefix.$viewName.$this->postfix); + foreach ($this->getViewNameList($viewNameList) as $viewName) { + if ($this->isViewNameExists($viewName)) { + return true; + } + } + return false; } public function getPrefix() @@ -77,5 +90,15 @@ public function setPostfix($postfix) return $this; } + + private function isViewNameExists($viewName) + { + return is_readable($this->prefix.$viewName.$this->postfix); + } + + private function getViewNameList($viewNameOrList) + { + return is_array($viewNameOrList) ? $viewNameOrList : [$viewNameOrList]; + } } ?> \ No newline at end of file diff --git a/test/AllTests.php b/test/AllTests.php index 60a482f1c4..218e3cd872 100644 --- a/test/AllTests.php +++ b/test/AllTests.php @@ -28,6 +28,7 @@ ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Ip'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR, + ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'UI'.DIRECTORY_SEPARATOR.'View'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'Routers'.DIRECTORY_SEPARATOR, ONPHP_TEST_PATH.'main'.DIRECTORY_SEPARATOR.'Utils'.DIRECTORY_SEPARATOR.'AMQP'.DIRECTORY_SEPARATOR, diff --git a/test/main/ViewTest.class.php b/test/main/UI/View/MultiPrefixPhpViewResolverTest.class.php similarity index 54% rename from test/main/ViewTest.class.php rename to test/main/UI/View/MultiPrefixPhpViewResolverTest.class.php index 052cc0a355..fc634e13bf 100644 --- a/test/main/ViewTest.class.php +++ b/test/main/UI/View/MultiPrefixPhpViewResolverTest.class.php @@ -9,30 +9,14 @@ * * ***************************************************************************/ - final class ViewTest extends TestCase + final class MultiPrefixPhpViewResolverTest extends ViewTest { - protected static $resolver; - - public static function setUpBeforeClass() + protected function getResolver() { - self::$resolver = new PhpViewResolver(ONPHP_TEST_PATH.'main/data/views/', EXT_TPL); - } - - public static function tearDownAfterClass() - { - self::$resolver = NULL; - } - - public function testToString() - { - $renderView = self::$resolver->resolveViewName('testView'); - $toStringView = self::$resolver->resolveViewName('testViewToString'); - - $model = Model::create(); - - $this->assertTrue( - $toStringView->toString($model) == $renderView->toString($model) - ); + return MultiPrefixPhpViewResolver::create() + ->setViewClassName('SimplePhpView') + ->addPrefix(ONPHP_TEST_PATH.'main/data/views_nonexists/') + ->addPrefix(ONPHP_TEST_PATH.'main/data/views/'); } } ?> \ No newline at end of file diff --git a/test/main/UI/View/PhpViewResolverTest.class.php b/test/main/UI/View/PhpViewResolverTest.class.php new file mode 100644 index 0000000000..40bd892183 --- /dev/null +++ b/test/main/UI/View/PhpViewResolverTest.class.php @@ -0,0 +1,19 @@ + \ No newline at end of file diff --git a/test/main/data/views/testViewPartList.tpl.html b/test/main/data/views/testViewPartList.tpl.html new file mode 100644 index 0000000000..ee5d61c19d --- /dev/null +++ b/test/main/data/views/testViewPartList.tpl.html @@ -0,0 +1,3 @@ +TestViewPartList Begin view(['nonExistsView', 'testPartView']) +?> TestViewPartListEnd \ No newline at end of file diff --git a/test/misc/ViewTest.class.php b/test/misc/ViewTest.class.php new file mode 100644 index 0000000000..70f430b6f9 --- /dev/null +++ b/test/misc/ViewTest.class.php @@ -0,0 +1,51 @@ +getResolver(); + /* @var $renderView Stringable */ + /* @var $toStringView Stringable */ + $renderView = $resolver->resolveViewName('testView'); + $toStringView = $resolver->resolveViewName('testViewToString'); + + $model = Model::create(); + + $this->assertTrue( + $toStringView->toString($model) == $renderView->toString($model) + ); + } + + public function testResolveViewList() + { + $this->assertEquals( + 'TestView Begin PartView TestViewEnd', + $this->getResolver()->resolveViewName(['testView'])->toString(Model::create()) + ); + $this->assertEquals( + 'TestView Begin PartView TestViewEnd', + $this->getResolver()->resolveViewName(['myView', 'testView'])->toString(Model::create()) + ); + + $this->assertEquals( + 'TestViewPartList Begin PartView TestViewPartListEnd', + $this->getResolver()->resolveViewName('testViewPartList')->toString(Model::create()) + ); + } + + /** + * @return ViewResolver + */ + abstract protected function getResolver(); + } +?> \ No newline at end of file