From fa581f195e3610292fc23dcf7d9c7cc9cfc3511e Mon Sep 17 00:00:00 2001 From: George Steel Date: Wed, 24 Oct 2018 21:47:47 +0100 Subject: [PATCH 1/5] Add methods and tests that providing the ability to clear/delete placeholder containers --- doc/book/helpers/placeholder.md | 17 ++++++++++++++++- src/Helper/Placeholder.php | 26 +++++++++++++++++++++++++- test/Helper/PlaceholderTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/doc/book/helpers/placeholder.md b/doc/book/helpers/placeholder.md index ad38cb30..88da5af0 100644 --- a/doc/book/helpers/placeholder.md +++ b/doc/book/helpers/placeholder.md @@ -63,7 +63,7 @@ $this->placeholder('foo') placeholder('foo') ?> ``` -The above results in an unodered list with pretty indentation. +The above results in an unordered list with pretty indentation. Because the `Placeholder` container objects extend `ArrayObject`, you can also assign content to a specific key in the container easily, instead of simply @@ -131,6 +131,21 @@ foreach ($this->data as $datum): ?> placeholder('foo')->data ?> ``` +## Clearing Content + +In certain situations it is desirable to remove or clear containers and aggregated content. The placeholder view helper +provides two methods to either delete a specific container or clear all containers at once: + +```php +plugin('placeholder'); +$placeholderHelper->deleteContainer('myNamedContainer'); +// Clear all containers at once +$placeholderHelper->clearContainers(); +?> +``` + ## Concrete Implementations zend-view ships with a number of "concrete" placeholder implementations. These diff --git a/src/Helper/Placeholder.php b/src/Helper/Placeholder.php index aad16d4c..1fd91f2c 100644 --- a/src/Helper/Placeholder.php +++ b/src/Helper/Placeholder.php @@ -23,7 +23,7 @@ class Placeholder extends AbstractHelper /** * Placeholder items * - * @var array + * @var Container\AbstractContainer[] */ protected $items = []; @@ -97,4 +97,28 @@ public function containerExists($key) $return = array_key_exists($key, $this->items); return $return; } + + /** + * Delete a specific container by name + * + * @param string $key + * @return self + */ + public function deleteContainer($key) + { + $key = (string) $key; + unset($this->items[$key]); + return $this; + } + + /** + * Remove all containers + * + * @return self + */ + public function clearContainers() + { + $this->items = []; + return $this; + } } diff --git a/test/Helper/PlaceholderTest.php b/test/Helper/PlaceholderTest.php index 4d7ac5d6..2064c373 100644 --- a/test/Helper/PlaceholderTest.php +++ b/test/Helper/PlaceholderTest.php @@ -77,4 +77,29 @@ public function testPlaceholderRetrievesSameContainerOnSubsequentCalls() $container2 = $this->placeholder->__invoke('foo'); $this->assertSame($container1, $container2); } + + public function testContainersCanBeDeleted() + { + $container = $this->placeholder->__invoke('foo'); + $container->set('Value'); + $this->assertTrue($this->placeholder->containerExists('foo')); + $this->assertSame('Value', (string) $this->placeholder->__invoke('foo')); + $this->placeholder->deleteContainer('foo'); + $this->assertFalse($this->placeholder->containerExists('foo')); + $this->assertSame('', (string) $this->placeholder->__invoke('foo')); + } + + public function testClearContainersRemovesAllContainers() + { + $this->placeholder->__invoke('foo'); + $this->placeholder->__invoke('bar'); + + $this->assertTrue($this->placeholder->containerExists('foo')); + $this->assertTrue($this->placeholder->containerExists('bar')); + + $this->placeholder->clearContainers(); + + $this->assertFalse($this->placeholder->containerExists('foo')); + $this->assertFalse($this->placeholder->containerExists('bar')); + } } From 3cd55e37b89fdc37d267740b984fa6d3fa1f28f7 Mon Sep 17 00:00:00 2001 From: George Steel Date: Mon, 12 Nov 2018 08:51:43 +0000 Subject: [PATCH 2/5] Improve documentation for deleting placeholder containers --- doc/book/helpers/placeholder.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/book/helpers/placeholder.md b/doc/book/helpers/placeholder.md index 88da5af0..86cb225c 100644 --- a/doc/book/helpers/placeholder.md +++ b/doc/book/helpers/placeholder.md @@ -136,14 +136,14 @@ foreach ($this->data as $datum): ?> In certain situations it is desirable to remove or clear containers and aggregated content. The placeholder view helper provides two methods to either delete a specific container or clear all containers at once: +### Delete a single container ```php -plugin('placeholder'); -$placeholderHelper->deleteContainer('myNamedContainer'); -// Clear all containers at once -$placeholderHelper->clearContainers(); -?> +$this->plugin('placeholder')->deleteContainer('myNamedContainer'); +``` + +### Clear all containers +```php +$this->plugin('placeholder')->clearContainers(); ``` ## Concrete Implementations From f9c47f6ae0b00019567bf4919bedb35f4f13cafc Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 6 Dec 2018 16:40:14 -0600 Subject: [PATCH 3/5] Fixes whitespace issues in placeholder docs --- doc/book/helpers/placeholder.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/book/helpers/placeholder.md b/doc/book/helpers/placeholder.md index 86cb225c..4a95bc92 100644 --- a/doc/book/helpers/placeholder.md +++ b/doc/book/helpers/placeholder.md @@ -133,15 +133,18 @@ foreach ($this->data as $datum): ?> ## Clearing Content -In certain situations it is desirable to remove or clear containers and aggregated content. The placeholder view helper -provides two methods to either delete a specific container or clear all containers at once: +In certain situations it is desirable to remove or clear containers and +aggregated content. The placeholder view helper provides two methods to either +delete a specific container or clear all containers at once: ### Delete a single container + ```php $this->plugin('placeholder')->deleteContainer('myNamedContainer'); ``` ### Clear all containers + ```php $this->plugin('placeholder')->clearContainers(); ``` From c7a94f83e1f459068266ccf5d6fa90ad8702e03f Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 6 Dec 2018 16:42:16 -0600 Subject: [PATCH 4/5] New methods should not implement fluent interfaces In these particular cases, you really do not want to operate on the return value, as it's ambiguous what that would mean. --- src/Helper/Placeholder.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Helper/Placeholder.php b/src/Helper/Placeholder.php index 1fd91f2c..8d42e07f 100644 --- a/src/Helper/Placeholder.php +++ b/src/Helper/Placeholder.php @@ -101,24 +101,22 @@ public function containerExists($key) /** * Delete a specific container by name * - * @param string $key - * @return self + * @param string $key + * @return void */ public function deleteContainer($key) { $key = (string) $key; unset($this->items[$key]); - return $this; } /** * Remove all containers * - * @return self + * @return void */ public function clearContainers() { $this->items = []; - return $this; } } From 60f5dc64a7b4dbe081954dbe63c8a55e729834f6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 6 Dec 2018 16:44:42 -0600 Subject: [PATCH 5/5] Adds CHANGELOG entry for #168 --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e578285..5a4f47e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,15 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#168](https://github.com/zendframework/zend-view/pull/168) adds two new methods to `Zend\View\Helper\Placeholder` (and thus any + helper extending it): + + - `deleteContainer(string $name)` can be used to delete a placeholder container. + - `clearContainers()` can be used to clear all placeholder containers. + + These new features are particularly useful when in long-running server + environments, such as Swoole, where you may need to clear the contents on each + request. ### Changed