Skip to content

Commit

Permalink
Merge branch 'master' of ssh://git.zendframework.com:21652/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Schindler committed Jun 21, 2010
2 parents fe5eb70 + 4f51698 commit 44a9cd3
Show file tree
Hide file tree
Showing 13 changed files with 1,203 additions and 45 deletions.
13 changes: 9 additions & 4 deletions library/Zend/Controller/Action/Helper/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,14 @@ protected function _getModuleDir()
*/
protected function _generateDefaultPrefix()
{
$default = 'Zend\View';
$default = 'Zend_View';
if (null === $this->_actionController) {
return $default;
}

$class = get_class($this->_actionController);

if (!strstr($class, '\\')) {
if (!strstr($class, '\\') && !strstr($class, '_')) {
return $default;
}

Expand All @@ -349,7 +349,11 @@ protected function _generateDefaultPrefix()
return $default;
}

$prefix = substr($class, 0, strpos($class, '\\')) . '\View';
if (strstr($class, '\\')) {
$prefix = substr($class, 0, strpos($class, '\\')) . '\View';
} elseif (strstr($class, '_')) {
$prefix = substr($class, 0, strpos($class, '_')) . '_View';
}

return $prefix;
}
Expand Down Expand Up @@ -475,7 +479,8 @@ public function initView($path = null, $prefix = null, array $options = array())
}
}
if (!$pathExists) {
$this->view->addBasePath($path, $prefix);
$namespaced = (false !== strstr($prefix, '\\'));
$this->view->addBasePath($path, $prefix, $namespaced);
}

// Register view with action controller (unless already registered)
Expand Down
29 changes: 21 additions & 8 deletions library/Zend/Loader/PluginLoader/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,36 +122,49 @@ public function __construct(Array $prefixToPaths = array(), $staticRegistryName
* Format prefix for internal use
*
* @param string $prefix
* @param bool $namespaced Whether the paths are namespaced or prefixed; namespaced by default
* @return string
*/
protected function _formatPrefix($prefix)
protected function _formatPrefix($prefix, $namespaced = true)
{
if($prefix == "") {
return $prefix;
}

$last = strlen($prefix) - 1;
if ($prefix{$last} == '\\') {
return $prefix;
}

return rtrim($prefix, '\\') . '\\';
switch ($namespaced) {
case true:
$last = strlen($prefix) - 1;
if ($prefix{$last} == '\\') {
return $prefix;
}

return $prefix . '\\';
case false:
$last = strlen($prefix) - 1;
if ($prefix{$last} == '_') {
return $prefix;
}

return $prefix . '_';
}
}

/**
* Add prefixed paths to the registry of paths
*
* @param string $prefix
* @param string $path
* @param bool $namespaced Whether the paths are namespaced or prefixed; namespaced by default
* @return \Zend\Loader\PluginLoader\PluginLoader
*/
public function addPrefixPath($prefix, $path)
public function addPrefixPath($prefix, $path, $namespaced = true)
{
if (!is_string($prefix) || !is_string($path)) {
throw new Exception('Zend\\Loader\\PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
}

$prefix = $this->_formatPrefix($prefix);
$prefix = $this->_formatPrefix($prefix, $namespaced);
$path = rtrim($path, '/\\') . '/';

if ($this->_useStaticRegistry) {
Expand Down
39 changes: 26 additions & 13 deletions library/Zend/View/AbstractView.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,22 @@ public function setBasePath($path, $classPrefix = 'Zend\View')
*
* @param string $path
* @param string $prefix Prefix to use for helper and filter paths
* @param bool $namespaced Whether the paths are namespaced or prefixed; namespaced by default
* @return \Zend\View\AbstractView
*/
public function addBasePath($path, $classPrefix = 'Zend\View')
public function addBasePath($path, $classPrefix = 'Zend\View', $namespaced = true)
{
$path = rtrim($path, '/');
$path = rtrim($path, '\\');
$path .= DIRECTORY_SEPARATOR;
$classPrefix = rtrim($classPrefix, '\\') . '\\';
if ($namespaced) {
$classPrefix = rtrim($classPrefix, '\\') . '\\';
} else {
$classPrefix = rtrim($classPrefix, '_') . '_';
}
$this->addScriptPath($path . 'scripts');
$this->addHelperPath($path . 'helpers', $classPrefix . 'Helper');
$this->addFilterPath($path . 'filters', $classPrefix . 'Filter');
$this->addHelperPath($path . 'helpers', $classPrefix . 'Helper', $namespaced);
$this->addFilterPath($path . 'filters', $classPrefix . 'Filter', $namespaced);
return $this;
}

Expand Down Expand Up @@ -511,11 +516,12 @@ public function getPluginLoader($type)
* @param string|array The directory (-ies) to add.
* @param string $classPrefix Class prefix to use with classes in this
* directory; defaults to Zend_View_Helper
* @param bool $namespaced Whether the paths are namespaced or prefixed; namespaced by default
* @return \Zend\View\AbstractView
*/
public function addHelperPath($path, $classPrefix = 'Zend\View\Helper\\')
public function addHelperPath($path, $classPrefix = 'Zend\View\Helper\\', $namespaced = true)
{
return $this->_addPluginPath('helper', $classPrefix, (array) $path);
return $this->_addPluginPath('helper', $classPrefix, (array) $path, $namespaced);
}

/**
Expand Down Expand Up @@ -572,9 +578,9 @@ public function registerHelper($helper, $name)
}

if (!$helper instanceof ViewInterface) {
if (!method_exists($helper, $name)) {
if (!method_exists($helper, 'direct')) {
$e = new Exception(
'View helper must implement Zend_View_Interface or have a method matching the name provided'
'View helper must implement Zend\\View\\Interface or have a "direct" method'
);
$e->setView($this);
throw $e;
Expand Down Expand Up @@ -619,11 +625,12 @@ public function getHelpers()
* @param string|array The directory (-ies) to add.
* @param string $classPrefix Class prefix to use with classes in this
* directory; defaults to Zend_View_Filter
* @param bool $namespaced Whether the paths are namespaced or prefixed; namespaced by default
* @return \Zend\View\AbstractView
*/
public function addFilterPath($path, $classPrefix = 'Zend\View\Filter\\')
public function addFilterPath($path, $classPrefix = 'Zend\View\Filter\\', $namespaced = true)
{
return $this->_addPluginPath('filter', $classPrefix, (array) $path);
return $this->_addPluginPath('filter', $classPrefix, (array) $path, $namespaced);
}

/**
Expand Down Expand Up @@ -885,7 +892,12 @@ public function escape($var)
return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);
}

return call_user_func($this->_escape, $var);
if (1 === func_num_args()) {
return call_user_func($this->_escape, $var);
}

$args = func_get_args();
return call_user_func_array($this->_escape, $args);
}

/**
Expand Down Expand Up @@ -1097,13 +1109,14 @@ private function _setFilterClass($name, $class, $file)
* @param string $type
* @param string $classPrefix
* @param array $paths
* @param bool $namespaced Whether the paths are namespaced or prefixed; namespaced by default
* @return \Zend\View\AbstractView
*/
private function _addPluginPath($type, $classPrefix, array $paths)
private function _addPluginPath($type, $classPrefix, array $paths, $namespaced = true)
{
$loader = $this->getPluginLoader($type);
foreach ($paths as $path) {
$loader->addPrefixPath($classPrefix, $path);
$loader->addPrefixPath($classPrefix, $path, $namespaced);
}
return $this;
}
Expand Down
12 changes: 7 additions & 5 deletions tests/Zend/Controller/Action/Helper/ViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,22 @@ protected function _checkDefaults($module = 'foo', $count = 1)
$this->assertContains($module, $scriptPaths[0]);

$helperPaths = $this->helper->view->getHelperPaths();
$test = ucfirst($module) . '\View\Helper\\';
$testNS = ucfirst($module) . '\View\Helper\\';
$testPrefix = ucfirst($module) . '_View_Helper_';
$found = false;
foreach ($helperPaths as $prefix => $paths) {
if ($test == $prefix) {
if ($testNS == $prefix || $testPrefix == $prefix) {
$found = true;
}
}
$this->assertTrue($found, 'Did not find auto-initialized helper path: ' . var_export($helperPaths, 1));

$filterPaths = $this->helper->view->getFilterPaths();
$test = ucfirst($module) . '\View\Filter\\';
$testNS = ucfirst($module) . '\View\Filter\\';
$testPrefix = ucfirst($module) . '_View_Filter_';
$found = false;
foreach ($filterPaths as $prefix => $paths) {
if ($test == $prefix) {
if ($testNS == $prefix || $testPrefix == $prefix) {
$found = true;
}
}
Expand Down Expand Up @@ -808,7 +810,7 @@ public function testCorrectViewHelperPathShouldBePropagatedWhenSubControllerInvo

$this->helper->render();
$body = $this->response->getBody();
$this->assertContains('fooUseHelper invoked', $body, 'Received ' . $body);
$this->assertContains('FooUseHelper invoked', $body, 'Received ' . $body);
}

public function testCorrectViewHelperPathShouldBePropagatedWhenSubControllerInvokedInDefaultModule()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class SampleZfHelper extends \Zend\View\Helper\AbstractHelper
class Zend_View_Helper_SampleZfHelper extends \Zend\View\Helper\AbstractHelper
{
public function direct()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class FooUseHelper extends \Zend\View\Helper\AbstractHelper
class Foo_View_Helper_FooUseHelper extends \Zend\View\Helper\AbstractHelper
{
public function direct()
{
return __FUNCTION__ . ' invoked';
return __CLASS__ . ' invoked';
}
}
2 changes: 1 addition & 1 deletion tests/Zend/View/Helper/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testJsonHelperReturnsJsonEncodedString()
{
$data = $this->helper->direct('foobar');
$this->assertTrue(is_string($data));
$this->assertEquals(array('foobar'), \Zend\JSON\JSON::decode($data));
$this->assertEquals('foobar', \Zend\JSON\JSON::decode($data));
}

public function testJsonHelperDisablesLayoutsByDefault()
Expand Down
Loading

0 comments on commit 44a9cd3

Please sign in to comment.