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

Commit

Permalink
Merge remote-tracking branch 'weierophinney/feature/registry-remove-s…
Browse files Browse the repository at this point in the history
…ingleton'
  • Loading branch information
akrabat committed Jun 28, 2012
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 219 deletions.
6 changes: 0 additions & 6 deletions src/Helper/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ class Currency extends AbstractHelper
*/
public function __construct($currency = null)
{
if ($currency === null) {
if (\Zend\Registry::isRegistered('Zend_Currency')) {
$currency = \Zend\Registry::get('Zend_Currency');
}
}

$this->setCurrency($currency);
}

Expand Down
85 changes: 53 additions & 32 deletions src/Helper/Doctype.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace Zend\View\Helper;

use Zend\View\Exception,
Zend\Registry,
ArrayObject;

/**
Expand Down Expand Up @@ -53,23 +52,59 @@ class Doctype extends AbstractHelper
const CUSTOM = 'CUSTOM';
/**#@-*/

/**
* @var ArrayObject Shared doctypes to use throughout all instances
*/
protected static $registeredDoctypes;

/**
* Default DocType
* @var string
*/
protected $_defaultDoctype = self::HTML4_LOOSE;
protected $defaultDoctype = self::HTML4_LOOSE;

/**
* Registry containing current doctype and mappings
* @var \ArrayObject
* @var ArrayObject
*/
protected $_registry;
protected $registry;

/**
* Registry key in which helper is stored
* @var string
* Register the default doctypes we understand
*
* @return void
*/
protected $_regKey = 'Zend_View_Helper_Doctype';
protected static function registerDefaultDoctypes()
{
static::$registeredDoctypes = new ArrayObject(array(
'doctypes' => array(
self::XHTML11 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
self::XHTML1_STRICT => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
self::XHTML1_TRANSITIONAL => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
self::XHTML1_FRAMESET => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
self::XHTML1_RDFA => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">',
self::XHTML_BASIC1 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">',
self::XHTML5 => '<!DOCTYPE html>',
self::HTML4_STRICT => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
self::HTML4_LOOSE => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
self::HTML4_FRAMESET => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
self::HTML5 => '<!DOCTYPE html>',
),
));
}

/**
* Unset the static doctype registry
*
* Mainly useful for testing purposes. Sets {@link $registeredDoctypes} to
* a null value.
*
* @return void
*/
public static function unsetDoctypeRegistry()
{
static::$registeredDoctypes = null;
}

/**
* Constructor
Expand All @@ -78,28 +113,11 @@ class Doctype extends AbstractHelper
*/
public function __construct()
{
if (!Registry::isRegistered($this->_regKey)) {
$this->_registry = new ArrayObject(array(
'doctypes' => array(
self::XHTML11 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
self::XHTML1_STRICT => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
self::XHTML1_TRANSITIONAL => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
self::XHTML1_FRAMESET => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
self::XHTML1_RDFA => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">',
self::XHTML_BASIC1 => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">',
self::XHTML5 => '<!DOCTYPE html>',
self::HTML4_STRICT => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
self::HTML4_LOOSE => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
self::HTML4_FRAMESET => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
self::HTML5 => '<!DOCTYPE html>',
)
));

Registry::set($this->_regKey, $this->_registry);
$this->setDoctype($this->_defaultDoctype);
} else {
$this->_registry = Registry::get($this->_regKey);
if (null === static::$registeredDoctypes) {
static::registerDefaultDoctypes();
$this->setDoctype($this->defaultDoctype);
}
$this->registry = static::$registeredDoctypes;
}

/**
Expand Down Expand Up @@ -136,7 +154,7 @@ public function __invoke($doctype = null)
$type = self::CUSTOM;
}
$this->setDoctype($type);
$this->_registry['doctypes'][$type] = $doctype;
$this->registry['doctypes'][$type] = $doctype;
break;
}
}
Expand All @@ -152,7 +170,7 @@ public function __invoke($doctype = null)
*/
public function setDoctype($doctype)
{
$this->_registry['doctype'] = $doctype;
$this->registry['doctype'] = $doctype;
return $this;
}

Expand All @@ -163,7 +181,10 @@ public function setDoctype($doctype)
*/
public function getDoctype()
{
return $this->_registry['doctype'];
if (!isset($this->registry['doctype'])) {
$this->setDoctype($this->defaultDoctype);
}
return $this->registry['doctype'];
}

/**
Expand All @@ -173,7 +194,7 @@ public function getDoctype()
*/
public function getDoctypes()
{
return $this->_registry['doctypes'];
return $this->registry['doctypes'];
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Helper/HeadTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ public function setTranslator($translate)
*/
public function getTranslator()
{
if (null === $this->_translator) {
if (\Zend\Registry::isRegistered('Zend_Translator')) {
$this->setTranslator(\Zend\Registry::get('Zend_Translator'));
}
}
return $this->_translator;
}

Expand Down
29 changes: 18 additions & 11 deletions src/Helper/Placeholder/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace Zend\View\Helper\Placeholder;

use Zend\Registry as RegistryZend;
use Zend\View\Exception;

/**
Expand All @@ -35,10 +34,9 @@
class Registry
{
/**
* Zend_Registry key under which placeholder registry exists
* @const string
* @var Registry Singleton instance
*/
const REGISTRY_KEY = 'Zend\View\Helper\Placeholder\Registry';
protected static $instance;

/**
* Default container class
Expand All @@ -55,18 +53,27 @@ class Registry
/**
* Retrieve or create registry instance
*
* @return mixed
* @return Registry
*/
public static function getRegistry()
{
if (RegistryZend::isRegistered(self::REGISTRY_KEY)) {
$registry = RegistryZend::get(self::REGISTRY_KEY);
} else {
$registry = new self();
RegistryZend::set(self::REGISTRY_KEY, $registry);
if (null === static::$instance) {
static::$instance = new self();
}

return $registry;
return static::$instance;
}

/**
* Unset the singleton
*
* Primarily useful for testing purposes; sets {@link $instance} to null.
*
* @return void
*/
public static function unsetRegistry()
{
static::$instance = null;
}

/**
Expand Down
7 changes: 0 additions & 7 deletions src/Helper/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace Zend\View\Helper;

use Zend\Locale\Locale,
Zend\Registry,
Zend\Translator\Adapter\AbstractAdapter as TranslationAdapter,
Zend\Translator\Translator as Translation,
Zend\View,
Expand Down Expand Up @@ -145,12 +144,6 @@ public function setTranslator($translator)
*/
public function getTranslator()
{
if ($this->translator === null) {
if (Registry::isRegistered('Zend_Translator')) {
$this->setTranslator(Registry::get('Zend_Translator'));
}
}

return $this->translator;
}

Expand Down
33 changes: 0 additions & 33 deletions test/Helper/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ class CurrencyTest extends \PHPUnit_Framework_TestCase
*/
public $helper;

public function clearRegistry()
{
$regKey = 'Zend_Currency';
if (\Zend\Registry::isRegistered($regKey)) {
$registry = \Zend\Registry::getInstance();
unset($registry[$regKey]);
}
}

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
Expand All @@ -61,8 +52,6 @@ public function clearRegistry()
*/
public function setUp()
{
$this->clearRegistry();

$cache = CacheFactory::adapterFactory('memory', array('memory_limit' => 0));
Currency\Currency::setCache($cache);

Expand All @@ -78,7 +67,6 @@ public function setUp()
public function tearDown()
{
unset($this->helper);
$this->clearRegistry();
}

public function testCurrencyObjectPassedToConstructor()
Expand All @@ -99,13 +87,6 @@ public function testLocalCurrencyObjectUsedWhenPresent()
$this->assertEquals('€ 0,12', $this->helper->__invoke(0.123));
}

public function testCurrencyObjectInRegistryUsedInAbsenceOfLocalCurrencyObject()
{
$curr = new Currency\Currency('de_AT');
\Zend\Registry::set('Zend_Currency', $curr);
$this->assertEquals('€ 1.234,56', $this->helper->__invoke(1234.56));
}

public function testPassingNonNullNonCurrencyObjectToConstructorThrowsException()
{
try {
Expand Down Expand Up @@ -145,20 +126,6 @@ public function testCurrencyObjectNullByDefault()
$this->assertNotNull($this->helper->getCurrency());
}

public function testLocalCurrencyObjectIsPreferredOverRegistry()
{
$currReg = new Currency\Currency('de_AT');
\Zend\Registry::set('Zend_Currency', $currReg);

$this->helper = new Helper\Currency();
$this->assertSame($currReg, $this->helper->getCurrency());

$currLoc = new Currency\Currency('en_US');
$this->helper->setCurrency($currLoc);
$this->assertSame($currLoc, $this->helper->getCurrency());
$this->assertNotSame($currLoc, $currReg);
}

public function testHelperObjectReturnedWhenNoArgumentsPassed()
{
$helper = $this->helper->__invoke();
Expand Down
19 changes: 2 additions & 17 deletions test/Helper/DoctypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ class DoctypeTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
$regKey = 'Zend_View_Helper_Doctype';
if (\Zend\Registry::isRegistered($regKey)) {
$registry = \Zend\Registry::getInstance();
unset($registry[$regKey]);
}
Helper\Doctype::unsetDoctypeRegistry();
$this->helper = new Helper\Doctype();
}

Expand All @@ -72,16 +68,6 @@ public function tearDown()
unset($this->helper);
}

public function testRegistryEntryCreatedAfterInstantiation()
{
$this->assertTrue(\Zend\Registry::isRegistered('Zend_View_Helper_Doctype'));
$doctype = \Zend\Registry::get('Zend_View_Helper_Doctype');
$this->assertTrue($doctype instanceof \ArrayObject);
$this->assertTrue(isset($doctype['doctype']));
$this->assertTrue(isset($doctype['doctypes']));
$this->assertTrue(is_array($doctype['doctypes']));
}

public function testDoctypeMethodReturnsObjectInstance()
{
$doctype = $this->helper->__invoke();
Expand Down Expand Up @@ -222,8 +208,7 @@ public function testStringificationReturnsDoctypeString()
{
$doctype = $this->helper->__invoke(Helper\Doctype::XHTML1_STRICT);
$string = $doctype->__toString();
$registry = \Zend\Registry::get('Zend_View_Helper_Doctype');
$this->assertEquals($registry['doctypes'][Helper\Doctype::XHTML1_STRICT], $string);
$this->assertEquals('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', $string);
}
}

11 changes: 3 additions & 8 deletions test/Helper/HeadLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
*/

namespace ZendTest\View\Helper;
use Zend\Registry,
Zend\View\Helper\Placeholder\Registry as PlaceholderRegistry,
use Zend\View\Helper\Placeholder\Registry as PlaceholderRegistry,
Zend\View\Helper,
Zend\View\Renderer\PhpRenderer as View,
Zend\View\Exception\ExceptionInterface as ViewException;
Expand Down Expand Up @@ -57,12 +56,8 @@ class HeadLinkTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
foreach (array(PlaceholderRegistry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
if (Registry::isRegistered($key)) {
$registry = Registry::getInstance();
unset($registry[$key]);
}
}
PlaceholderRegistry::unsetRegistry();
Helper\Doctype::unsetDoctypeRegistry();
$this->basePath = __DIR__ . '/_files/modules';
$this->view = new View();
$this->helper = new Helper\HeadLink();
Expand Down
Loading

0 comments on commit ef80e35

Please sign in to comment.