Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixes #3978 #3979

Merged
merged 2 commits into from

2 participants

@SocalNick

Fix #3978 - deprecating Placeholder Registry by having Placeholder and Placeholder Abstract Standalone implement their own containers.

@weierophinney weierophinney was assigned
@weierophinney weierophinney merged commit f00ba19 into from
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/3979' into develop
Close #3979
Fixes #3978
002a010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php
@@ -269,7 +269,6 @@ public function reset()
// reset singleton
StaticEventManager::resetInstance();
- Placeholder\Registry::unsetRegistry();
return $this;
}
View
63 library/Zend/View/Helper/Placeholder.php
@@ -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
@@ -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
@@ -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;
}
}
View
95 library/Zend/View/Helper/Placeholder/Container/AbstractStandalone.php
@@ -11,7 +11,6 @@
use Zend\Escaper\Escaper;
use Zend\View\Exception;
-use Zend\View\Helper\Placeholder\Registry;
use Zend\View\Renderer\RendererInterface;
/**
@@ -32,17 +31,6 @@
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
* @var bool
@@ -50,35 +38,18 @@
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());
}
/**
@@ -172,10 +143,64 @@ 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
*
* @param string $key
View
2  library/Zend/View/Helper/Placeholder/Registry.php
@@ -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();
}
@@ -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;
}
View
5 library/Zend/View/Strategy/PhpRendererStrategy.php
@@ -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;
}
}
View
12 tests/ZendTest/View/Helper/HeadLinkTest.php
@@ -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();
@@ -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();
View
12 tests/ZendTest/View/Helper/HeadMetaTest.php
@@ -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();
@@ -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();
View
12 tests/ZendTest/View/Helper/HeadScriptTest.php
@@ -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();
}
@@ -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();
View
12 tests/ZendTest/View/Helper/HeadStyleTest.php
@@ -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();
}
@@ -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();
View
12 tests/ZendTest/View/Helper/HeadTitleTest.php
@@ -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();
}
@@ -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();
View
12 tests/ZendTest/View/Helper/InlineScriptTest.php
@@ -43,7 +43,6 @@ class InlineScriptTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
- Registry::unsetRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->helper = new Helper\InlineScript();
}
@@ -59,17 +58,6 @@ public function tearDown()
unset($this->helper);
}
- public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
- {
- $registry = Registry::getRegistry();
- if ($registry->containerExists('Zend_View_Helper_InlineScript')) {
- $registry->deleteContainer('Zend_View_Helper_InlineScript');
- }
- $this->assertFalse($registry->containerExists('Zend_View_Helper_InlineScript'));
- $helper = new Helper\InlineScript();
- $this->assertTrue($registry->containerExists('Zend_View_Helper_InlineScript'));
- }
-
public function testInlineScriptReturnsObjectInstance()
{
$placeholder = $this->helper->__invoke();
View
14 tests/ZendTest/View/Helper/Placeholder/RegistryTest.php
@@ -39,7 +39,6 @@ class RegistryTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
- Registry::unsetRegistry();
$this->registry = new Registry();
}
@@ -143,19 +142,6 @@ public function testUsingCustomContainerClassCreatesContainersOfCustomClass()
$this->assertTrue($container instanceof MockContainer);
}
- public function testGetRegistryReturnsRegistryInstance()
- {
- $registry = Registry::getRegistry();
- $this->assertTrue($registry instanceof Registry);
- }
-
- public function testGetRegistrySubsequentTimesReturnsSameInstance()
- {
- $registry1 = Registry::getRegistry();
- $registry2 = Registry::getRegistry();
- $this->assertSame($registry1, $registry2);
- }
-
/**
* @group ZF-10793
*/
View
91 tests/ZendTest/View/Helper/Placeholder/StandaloneContainerTest.php
@@ -10,6 +10,7 @@
namespace ZendTest\View\Helper\Placeholder;
+use Zend\View\Helper\Placeholder\Container;
use Zend\View\Renderer\PhpRenderer as View;
/**
@@ -25,6 +26,11 @@ class StandaloneContainerTest extends \PHPUnit_Framework_TestCase
{
/**
+ * @var Foo
+ */
+ protected $helper;
+
+ /**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
@@ -32,22 +38,79 @@ class StandaloneContainerTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
- \Zend\View\Helper\Placeholder\Registry::unsetRegistry();
- $this->basePath = __DIR__ . '/_files/modules';
$this->helper = new Foo();
}
/**
- * Tears down the fixture, for example, close a network connection.
- * This method is called after a test is executed.
- *
* @return void
*/
- public function tearDown()
+ public function testSetContainer()
+ {
+ $container = new Container();
+ $this->assertNotSame($container, $this->helper->getContainer());
+ $this->helper->setContainer($container);
+ $this->assertSame($container, $this->helper->getContainer());
+ }
+
+ /**
+ * @return void
+ */
+ public function testGetContainer()
+ {
+ $container = $this->helper->getContainer();
+ $this->assertInstanceOf('Zend\View\Helper\Placeholder\Container', $container);
+ }
+
+ /**
+ * @return void
+ */
+ public function testGetContainerCreatesNewContainer()
+ {
+ $this->helper->deleteContainer();
+ $container = $this->helper->getContainer();
+ $this->assertInstanceOf('Zend\View\Helper\Placeholder\Container', $container);
+ }
+
+ /**
+ * @return void
+ */
+ public function testDeleteContainer()
{
- unset($this->helper);
+ $this->assertNotNull($this->helper->getContainer());
+ $this->assertTrue($this->helper->deleteContainer());
+ $this->assertFalse($this->helper->deleteContainer());
}
+ /**
+ * @expectedException DomainException
+ * @return void
+ */
+ public function testSetContainerClassThrowsDomainException()
+ {
+ $this->helper->setContainerClass('bat');
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @return void
+ */
+ public function testSetContainerClassThrowsInvalidArgumentException()
+ {
+ $this->helper->setContainerClass(get_class($this));
+ }
+
+ /**
+ * @return void
+ */
+ public function testSetGetContainerClass()
+ {
+ $this->helper->setContainerClass('ZendTest\View\Helper\Placeholder\Bar');
+ $this->assertEquals('ZendTest\View\Helper\Placeholder\Bar', $this->helper->getContainerClass());
+ }
+
+ /**
+ * @return void
+ */
public function testViewAccessorWorks()
{
$view = new View();
@@ -55,7 +118,10 @@ public function testViewAccessorWorks()
$this->assertSame($view, $this->helper->getView());
}
- public function testContainersPersistBetweenInstances()
+ /**
+ * @return void
+ */
+ public function testContainerDoesNotPersistBetweenInstances()
{
$foo1 = new Foo;
$foo1->append('Foo');
@@ -64,9 +130,9 @@ public function testContainersPersistBetweenInstances()
$foo2 = new Foo;
$foo2->append('Bar');
- $test = $foo1->toString();
- $this->assertContains('Foo', $test);
- $this->assertContains(' - ', $test);
+ $test = $foo2->toString();
+ $this->assertNotContains('Foo', $test);
+ $this->assertNotContains(' - ', $test);
$this->assertContains('Bar', $test);
}
}
@@ -76,3 +142,6 @@ class Foo extends \Zend\View\Helper\Placeholder\Container\AbstractStandalone
protected $_regKey = 'foo';
public function direct() {}
}
+
+class Bar extends \Zend\View\Helper\Placeholder\Container\AbstractContainer
+{}
View
8 tests/ZendTest/View/Helper/PlaceholderTest.php
@@ -38,7 +38,6 @@ class PlaceholderTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
- PlaceholderRegistry::unsetRegistry();
$this->placeholder = new Helper\Placeholder();
}
@@ -51,13 +50,6 @@ public function setUp()
public function tearDown()
{
unset($this->placeholder);
- PlaceholderRegistry::unsetRegistry();
- }
-
- public function testMultiplePlaceholdersUseSameRegistry()
- {
- $placeholder = new Helper\Placeholder();
- $this->assertSame($this->placeholder->getRegistry(), $placeholder->getRegistry());
}
/**
View
2  tests/ZendTest/View/Helper/RenderToPlaceholderTest.php
@@ -34,7 +34,7 @@ public function setUp()
public function testDefaultEmpty()
{
$this->_view->plugin('renderToPlaceholder')->__invoke('rendertoplaceholderscript.phtml', 'fooPlaceholder');
- $placeholder = new PlaceholderHelper();
+ $placeholder = $this->_view->plugin('placeholder');
$this->assertEquals("Foo Bar" . "\n", $placeholder->__invoke('fooPlaceholder')->getValue());
}
View
3  tests/ZendTest/View/Strategy/PhpRendererStrategyTest.php
@@ -27,9 +27,6 @@ class PhpRendererStrategyTest extends TestCase
{
public function setUp()
{
- // Necessary to ensure placeholders do not persist between individual tests
- PlaceholderRegistry::unsetRegistry();
-
$this->renderer = new PhpRenderer;
$this->strategy = new PhpRendererStrategy($this->renderer);
$this->event = new ViewEvent();
Something went wrong with that request. Please try again.