/** * Tests for the __invoke() method */ public function testInvoke() { $escapeHtmlAttr = $this->_getHelper('escapeHtmlAttr'); // Start tests with no doctype - assume HTML4. $doctype = new \Zend\View\Helper\Doctype(); $helper = new \Library\View\Helper\HtmlTag($escapeHtmlAttr, $doctype); // Empty br element, non-inline $this->assertEquals("<br>\n", $helper('br')); // Empty br element, inline $this->assertEquals('<br>', $helper('br', null, null, true)); // Empty br element with escaped attribute, inline $this->assertEquals('<br attribute="value"value">', $helper('br', null, array('attribute' => 'value"value'), true)); // Element with integer content '0' (tests sideeffects from PHP's type juggling), inline $this->assertEquals('<element>0</element>', $helper('element', 0, null, true)); // Empty string as content (tests sideeffects from PHP's type juggling), regardless of type $this->assertEquals('<br></br>', $helper('br', '', null, true)); // Empty command element (HTML5 only) should get closing tag $this->assertEquals("<command></command>\n", $helper('command')); // Test empty HTML5 elements $doctype->setDoctype('HTML5'); $helper = new \Library\View\Helper\HtmlTag($escapeHtmlAttr, $doctype); $this->assertEquals("<command>\n", $helper('command')); $this->assertEquals("<br>\n", $helper('br')); $this->assertEquals("<a></a>\n", $helper('a')); // Empty XHTML Elements $doctype->setDoctype('XHTML11'); $helper = new \Library\View\Helper\HtmlTag($escapeHtmlAttr, $doctype); $this->assertEquals("<command />\n", $helper('command')); $this->assertEquals("<br />\n", $helper('br')); $this->assertEquals("<a />\n", $helper('a')); }
/** * Instantiates and configures the renderer's helper manager * * @return ViewHelperManager */ public function getHelperManager() { if ($this->helperManager) { return $this->helperManager; } $this->helperManager = new ViewHelperManager(); // Setup additional helpers $map = array(); if (isset($this->config['helper_map'])) { $map = $this->config['helper_map']; } if (!in_array('Zend\\Form\\View\\HelperConfiguration', $map)) { array_unshift($map, 'Zend\\Form\\View\\HelperConfiguration'); } foreach ($map as $key => $service) { if ((!is_string($key) || is_numeric($key)) && class_exists($service)) { $config = new $service(); if (!$config instanceof ConfigurationInterface) { throw new Exception\RuntimeException(sprintf('Invalid helper configuration map provided; received "%s", expected class implementing %s', $service, 'Zend\\ServiceManager\\ConfigurationInterface')); } $config->configureServiceManager($this->helperManager); continue; } $this->helperManager->setInvokableClass($key, $service); } // Seed with service manager if ($this->services instanceof ServiceManager) { $this->helperManager->addPeeringServiceManager($this->services, ServiceManager::SCOPE_PARENT); } // Configure URL view helper with router $this->helperManager->setFactory('Zend\\View\\Helper\\Url', function ($sm) { $urlHelper = new \Zend\View\Helper\Url(); $urlHelper->setRouter($sm->get('Router')); return $urlHelper; }); $this->helperManager->setAlias('url', 'Zend\\View\\Helper\\Url'); $config = $this->config; // Configure basePath view helper with base path from configuration, if available $this->helperManager->setFactory('Zend\\View\\Helper\\BasePath', function ($sm) use($config) { $basePathHelper = new \Zend\View\Helper\BasePath(); if (isset($config['base_path'])) { $basePath = $config['base_path']; } else { $basePath = $sm->get('Request')->getBasePath(); } $basePathHelper->setBasePath($basePath); return $basePathHelper; }); $this->helperManager->setAlias('basepath', 'Zend\\View\\Helper\\BasePath'); // Configure doctype view helper with doctype from configuration, if available $this->helperManager->setFactory('Zend\\View\\Helper\\Doctype', function ($sm) use($config) { $doctypeHelper = new \Zend\View\Helper\Doctype(); if (isset($config['doctype'])) { $doctypeHelper->setDoctype($config['doctype']); } return $doctypeHelper; }); $this->helperManager->setAlias('doctype', 'Zend\\View\\Helper\\Doctype'); $this->services->setService('ViewHelperManager', $this->helperManager); $this->services->setAlias('ViewHelperBroker', 'ViewHelperManager'); $this->services->setAlias('Zend\\View\\HelperPluginManager', 'ViewHelperManager'); return $this->helperManager; }
/** * Instantiates and configures the renderer's helper broker * * @return ViewHelperBroker */ public function getHelperBroker() { if ($this->helperBroker) { return $this->helperBroker; } $this->helperBroker = new ViewHelperBroker(); $this->helperBroker->setClassLoader($this->getHelperLoader()); // Seed with service manager $this->helperBroker->setServiceLocator($this->services); // Configure URL view helper with router $this->services->setFactory('Zend\View\Helper\Url', function($sm) { $urlHelper = new \Zend\View\Helper\Url; $urlHelper->setRouter($sm->get('Router')); return $urlHelper; }); $config = $this->config; // Configure basePath view helper with base path from configuration, if available $this->services->setFactory('Zend\View\Helper\BasePath', function($sm) use($config) { $basePathHelper = new \Zend\View\Helper\BasePath; if (isset($config['base_path'])) { $basePath = $config['base_path']; } else { $basePath = $sm->get('Request')->getBasePath(); } $basePathHelper->setBasePath($basePath); return $basePathHelper; }); // Configure doctype view helper with doctype from configuration, if available $this->services->setFactory('Zend\View\Helper\Doctype', function($sm) use($config) { $doctypeHelper = new \Zend\View\Helper\Doctype; if (isset($config['doctype'])) { $doctypeHelper->setDoctype($config['doctype']); } return $doctypeHelper; }); $this->services->setService('ViewHelperBroker', $this->helperBroker); $this->services->setAlias('Zend\View\HelperBroker', 'ViewHelperBroker'); return $this->helperBroker; }