Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/3979' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 12, 2013
2 parents 27bdfda + 6768b40 commit b818af1
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 165 deletions.
63 changes: 45 additions & 18 deletions src/Helper/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\View\Helper;

use Zend\View\Exception\InvalidArgumentException;
use Zend\View\Helper\Placeholder\Container;

/**
* Helper for passing data between otherwise segregated Views. It's called
Expand All @@ -26,20 +27,10 @@ class Placeholder extends AbstractHelper
protected $items = array();

/**
* @var \Zend\View\Helper\Placeholder\Registry
* Default container class
* @var string
*/
protected $registry;

/**
* Constructor
*
* Retrieve container registry from Placeholder\Registry, or create new one and register it.
*
*/
public function __construct()
{
$this->registry = Placeholder\Registry::getRegistry();
}
protected $containerClass = 'Zend\View\Helper\Placeholder\Container';

/**
* Placeholder helper
Expand All @@ -55,16 +46,52 @@ public function __invoke($name = null)
}

$name = (string) $name;
return $this->registry->getContainer($name);
return $this->getContainer($name);
}

/**
* createContainer
*
* @param string $key
* @param array $value
* @return Container\AbstractContainer
*/
public function createContainer($key, array $value = array())
{
$key = (string) $key;

$this->items[$key] = new $this->containerClass($value);
return $this->items[$key];
}

/**
* Retrieve a placeholder container
*
* @param string $key
* @return Container\AbstractContainer
*/
public function getContainer($key)
{
$key = (string) $key;
if (isset($this->items[$key])) {
return $this->items[$key];
}

$container = $this->createContainer($key);

return $container;
}

/**
* Retrieve the registry
* Does a particular container exist?
*
* @return \Zend\View\Helper\Placeholder\Registry
* @param string $key
* @return bool
*/
public function getRegistry()
public function containerExists($key)
{
return $this->registry;
$key = (string) $key;
$return = array_key_exists($key, $this->items);
return $return;
}
}
95 changes: 60 additions & 35 deletions src/Helper/Placeholder/Container/AbstractStandalone.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Zend\Escaper\Escaper;
use Zend\View\Exception;
use Zend\View\Helper\Placeholder\Registry;
use Zend\View\Renderer\RendererInterface;

/**
Expand All @@ -31,17 +30,6 @@ abstract class AbstractStandalone
*/
protected $escapers = array();

/**
* @var \Zend\View\Helper\Placeholder\Registry
*/
protected $registry;

/**
* Registry key under which container registers itself
* @var string
*/
protected $regKey;

/**
* Flag whether to automatically escape output, must also be
* enforced in the child class if __toString/toString is overridden
Expand All @@ -50,35 +38,18 @@ abstract class AbstractStandalone
protected $autoEscape = true;

/**
* Constructor
*
*/
public function __construct()
{
$this->setRegistry(Registry::getRegistry());
$this->setContainer($this->getRegistry()->getContainer($this->regKey));
}

/**
* Retrieve registry
*
* @return \Zend\View\Helper\Placeholder\Registry
* Default container class
* @var string
*/
public function getRegistry()
{
return $this->registry;
}
protected $containerClass = 'Zend\View\Helper\Placeholder\Container';

/**
* Set registry object
* Constructor
*
* @param \Zend\View\Helper\Placeholder\Registry $registry
* @return \Zend\View\Helper\Placeholder\Container\AbstractStandalone
*/
public function setRegistry(Registry $registry)
public function __construct()
{
$this->registry = $registry;
return $this;
$this->setContainer($this->getContainer());
}

/**
Expand Down Expand Up @@ -172,9 +143,63 @@ public function setContainer(AbstractContainer $container)
*/
public function getContainer()
{
if (!$this->container instanceof AbstractContainer) {
$this->container = new $this->containerClass();
}
return $this->container;
}

/**
* Delete a container
*
* @return bool
*/
public function deleteContainer()
{
if (null != $this->container) {
$this->container = null;
return true;
}

return false;
}

/**
* Set the container class to use
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @throws Exception\DomainException
* @return \Zend\View\Helper\Placeholder\Container\AbstractStandalone
*/
public function setContainerClass($name)
{
if (!class_exists($name)) {
throw new Exception\DomainException(
sprintf('%s expects a valid container class name; received "%s", which did not resolve',
__METHOD__,
$name
));
}

if (!in_array('Zend\View\Helper\Placeholder\Container\AbstractContainer', class_parents($name))) {
throw new Exception\InvalidArgumentException('Invalid Container class specified');
}

$this->containerClass = $name;
return $this;
}

/**
* Retrieve the container class
*
* @return string
*/
public function getContainerClass()
{
return $this->containerClass;
}

/**
* Overloading: set property value
*
Expand Down
2 changes: 2 additions & 0 deletions src/Helper/Placeholder/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Registry
*/
public static function getRegistry()
{
trigger_error('Placeholder view helpers should no longer use a singleton registry', E_USER_DEPRECATED);
if (null === static::$instance) {
static::$instance = new static();
}
Expand All @@ -56,6 +57,7 @@ public static function getRegistry()
*/
public static function unsetRegistry()
{
trigger_error('Placeholder view helpers should no longer use a singleton registry', E_USER_DEPRECATED);
static::$instance = null;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Strategy/PhpRendererStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ public function injectResponse(ViewEvent $e)
// populated, and set the content from them.
if (empty($result)) {
$placeholders = $renderer->plugin('placeholder');
$registry = $placeholders->getRegistry();
foreach ($this->contentPlaceholders as $placeholder) {
if ($registry->containerExists($placeholder)) {
$result = (string) $registry->getContainer($placeholder);
if ($placeholders->containerExists($placeholder)) {
$result = (string) $placeholders->getContainer($placeholder);
break;
}
}
Expand Down
12 changes: 0 additions & 12 deletions test/Helper/HeadLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class HeadLinkTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
PlaceholderRegistry::unsetRegistry();
Helper\Doctype::unsetDoctypeRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->view = new View();
Expand All @@ -63,17 +62,6 @@ public function tearDown()
unset($this->helper);
}

public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
{
$registry = PlaceholderRegistry::getRegistry();
if ($registry->containerExists('Zend_View_Helper_HeadLink')) {
$registry->deleteContainer('Zend_View_Helper_HeadLink');
}
$this->assertFalse($registry->containerExists('Zend_View_Helper_HeadLink'));
$helper = new Helper\HeadLink();
$this->assertTrue($registry->containerExists('Zend_View_Helper_HeadLink'));
}

public function testHeadLinkReturnsObjectInstance()
{
$placeholder = $this->helper->__invoke();
Expand Down
12 changes: 0 additions & 12 deletions test/Helper/HeadMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class HeadMetaTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->error = false;
PlaceholderRegistry::unsetRegistry();
Helper\Doctype::unsetDoctypeRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->view = new View();
Expand All @@ -70,17 +69,6 @@ public function handleErrors($errno, $errstr)
$this->error = $errstr;
}

public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
{
$registry = PlaceholderRegistry::getRegistry();
if ($registry->containerExists('Zend_View_Helper_HeadMeta')) {
$registry->deleteContainer('Zend_View_Helper_HeadMeta');
}
$this->assertFalse($registry->containerExists('Zend_View_Helper_HeadMeta'));
$helper = new Helper\HeadMeta();
$this->assertTrue($registry->containerExists('Zend_View_Helper_HeadMeta'));
}

public function testHeadMetaReturnsObjectInstance()
{
$placeholder = $this->helper->__invoke();
Expand Down
12 changes: 0 additions & 12 deletions test/Helper/HeadScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class HeadScriptTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
Registry::unsetRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->helper = new Helper\HeadScript();
}
Expand All @@ -60,17 +59,6 @@ public function tearDown()
unset($this->helper);
}

public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
{
$registry = Registry::getRegistry();
if ($registry->containerExists('Zend_View_Helper_HeadScript')) {
$registry->deleteContainer('Zend_View_Helper_HeadScript');
}
$this->assertFalse($registry->containerExists('Zend_View_Helper_HeadScript'));
$helper = new Helper\HeadScript();
$this->assertTrue($registry->containerExists('Zend_View_Helper_HeadScript'));
}

public function testHeadScriptReturnsObjectInstance()
{
$placeholder = $this->helper->__invoke();
Expand Down
12 changes: 0 additions & 12 deletions test/Helper/HeadStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class HeadStyleTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
Registry::unsetRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->helper = new Helper\HeadStyle();
}
Expand All @@ -59,17 +58,6 @@ public function tearDown()
unset($this->helper);
}

public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
{
$registry = Registry::getRegistry();
if ($registry->containerExists('Zend_View_Helper_HeadStyle')) {
$registry->deleteContainer('Zend_View_Helper_HeadStyle');
}
$this->assertFalse($registry->containerExists('Zend_View_Helper_HeadStyle'));
$helper = new Helper\HeadStyle();
$this->assertTrue($registry->containerExists('Zend_View_Helper_HeadStyle'));
}

public function testHeadStyleReturnsObjectInstance()
{
$placeholder = $this->helper->__invoke();
Expand Down
12 changes: 0 additions & 12 deletions test/Helper/HeadTitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class HeadTitleTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
Registry::unsetRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->helper = new Helper\HeadTitle();
}
Expand All @@ -59,17 +58,6 @@ public function tearDown()
unset($this->helper);
}

public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
{
$registry = Registry::getRegistry();
if ($registry->containerExists('Zend_View_Helper_HeadTitle')) {
$registry->deleteContainer('Zend_View_Helper_HeadTitle');
}
$this->assertFalse($registry->containerExists('Zend_View_Helper_HeadTitle'));
$helper = new Helper\HeadTitle();
$this->assertTrue($registry->containerExists('Zend_View_Helper_HeadTitle'));
}

public function testHeadTitleReturnsObjectInstance()
{
$placeholder = $this->helper->__invoke();
Expand Down
Loading

0 comments on commit b818af1

Please sign in to comment.