/** * @param ScriptContainer $container * @param string|number $place * @return callable */ protected function getInjector($container, $place) { $insert = null; switch ($place) { case self::PLACE_APPEND: $insert = function ($jsFile, $type, $attrs) use($container) { $container->appendFile($jsFile, $type, $attrs); }; break; case self::PLACE_PREPEND: $insert = function ($jsFile, $type, $attrs) use($container) { $container->prependFile($jsFile, $type, $attrs); }; break; default: if (is_numeric($place)) { $insert = function ($jsFile, $type, $attrs) use($container, &$place) { $container->offsetSetFile($place, $jsFile, $type, $attrs); $place++; }; } break; } if (!is_callable($insert)) { throw new \RuntimeException('Failed to make JS files injector.'); } return $insert; }
public function testConditionalScriptNoIEWidthSpace() { $this->helper->setAllowArbitraryAttributes(true); $this->helper->appendFile('/js/foo.js', 'text/javascript', array('conditional' => '! IE')); $test = $this->helper->toString(); $this->assertContains('<!--[if ! IE]><!--><', $test); $this->assertContains('<!--<![endif]-->', $test); }
public function js($fileName) { if ($this->_headScript != null && !empty($fileName)) { $files = is_array($fileName) ? $fileName : array($fileName); foreach ($files as $file) { $parts = pathinfo($file); $ext = empty($parts["extension"]) ? "js" : $parts["extension"]; $dir = isset($parts["dirname"]) && $parts["dirname"] != "." ? $parts["dirname"] . "/" : ""; $file = "/js/" . $dir . $parts["filename"] . "." . $ext; if (file_exists(getcwd() . "/public" . $file)) { $this->_headScript->appendFile($file); } } } }
/** * Creates an instance of \Zend\View\Helper\Headscript * * - injects the MvcEvent instance * * @param ServiceLocatorInterface $serviceLocator * @return HeadScript * @see \Zend\ServiceManager\FactoryInterface::createService() */ public function createService(ServiceLocatorInterface $serviceLocator) { $helper = new HeadScript(); $services = $serviceLocator->getServiceLocator(); $config = $services->get('Config'); if (!isset($config['view_helper_config']['headscript'])) { return $helper; } $config = $config['view_helper_config']['headscript']; $routeMatch = $services->get('Application')->getMvcEvent()->getRouteMatch(); $routeName = $routeMatch ? $routeMatch->getMatchedRouteName() : ''; $basepath = $serviceLocator->get('basepath'); foreach ($config as $routeStart => $specs) { if (!is_int($routeStart)) { if (0 !== strpos($routeName, $routeStart)) { continue; } } else { $specs = array($specs); } if (is_string($specs)) { $helper->appendScript('// if you are missing the script ' . $specs . ' look up your config and enclose it in an array'); continue; } foreach ($specs as $spec) { if (is_string($spec)) { $helper->appendFile($basepath($spec)); continue; } if ($helper::SCRIPT != $spec[0]) { $spec[1] = $basepath($spec[1]); } call_user_func_array($helper, $spec); } } return $helper; }
/** * Sets a javascript asset into the HeadScript or InlineScript * @param HeadScript $element * @param array $js */ private function setJavascript(HeadScript $element, array $js = array()) { $jsPath = $this->options->getJs()->getPath(); if (isset($js["options"])) { $element->appendFile($jsPath . "/" . $js["name"], "text/javascript", $js["options"]); } else { $element->appendFile($jsPath . "/" . $js["name"]); } }