Skip to content

Передача ViewResolver'ами не только имя шаблона но и списка имен #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions main/UI/View/MultiPrefixPhpViewResolver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,33 @@ public function setPostfix($postfix)
$this->postfix = $postfix;
return $this;
}

/**
* @return SimplePhpView
**/
* @param string $viewName
* @return View
* @throws WrongArgumentException
*/
public function resolveViewName($viewName)
{
Assert::isFalse(
($this->prefixes === array()),
'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();
}

public function viewExists($viewName)
{
return ($this->findPrefix($viewName) !== null);
return ($this->findPath($viewName) !== null);
}

/**
Expand All @@ -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;
Expand All @@ -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)
Expand Down
39 changes: 31 additions & 8 deletions main/UI/View/PhpViewResolver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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];
}
}
?>
1 change: 1 addition & 0 deletions test/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
}
}
?>
19 changes: 19 additions & 0 deletions test/main/UI/View/PhpViewResolverTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/***************************************************************************
* Copyright (C) 2011 by Igor V. Gulyaev *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

final class PhpViewResolverTest extends ViewTest
{
protected function getResolver()
{
return new PhpViewResolver(ONPHP_TEST_PATH.'main/data/views/', EXT_TPL);
}
}
?>
3 changes: 3 additions & 0 deletions test/main/data/views/testViewPartList.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TestViewPartList Begin <?php
$partViewer->view(['nonExistsView', 'testPartView'])
?> TestViewPartListEnd
51 changes: 51 additions & 0 deletions test/misc/ViewTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/***************************************************************************
* Copyright (C) 2011 by Igor V. Gulyaev *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

abstract class ViewTest extends TestCase
{
public function testToString()
{
$resolver = $this->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();
}
?>