Exemplo n.º 1
0
 /**
  * @param string $name
  * @return string
  * @throws \Twig_Error_Loader
  */
 protected function findTemplate($name)
 {
     // normalize name
     $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
     if (isset($this->cache[$name])) {
         return $this->cache[$name];
     }
     $this->validateName($name);
     $namespace = '__main__';
     if (isset($name[0]) && '@' == $name[0]) {
         if (false === ($pos = strpos($name, '/'))) {
             throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
         }
         $namespace = substr($name, 1, $pos - 1);
         $name = substr($name, $pos + 1);
     }
     if (!isset($this->paths[$namespace])) {
         throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
     }
     if ($namespace == '__main__' && ($fallbackName = $this->fallbackResolver->resolve($name))) {
         return $this->cache[$fallbackName] = $fallbackName;
     }
     foreach ($this->paths[$namespace] as $path) {
         if (is_file($path . '/' . $name)) {
             return $this->cache[$name] = $path . '/' . $name;
         }
     }
     throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
 }
Exemplo n.º 2
0
 /**
  * Find template file
  *
  * @see Twig_LoaderInterface::findTemplate()
  */
 public function findTemplate($name)
 {
     $template = $this->resolver->resolve($name);
     if (false === $template) {
         throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $name));
     }
     return $template;
 }
Exemplo n.º 3
0
 /**
  * Get tab specific template path if present
  *
  * @param String $baseTemplate BaseTemplate
  * @param String $tab          Tab
  *
  * @return String
  */
 public function __invoke($baseTemplate, $tab = null)
 {
     if ($tab === null) {
         return $baseTemplate;
     }
     $tab = strtolower($tab);
     $customTemplate = str_replace('.phtml', '', $baseTemplate) . '-' . $tab;
     return $this->resolver->resolve($customTemplate) !== false ? $customTemplate : $baseTemplate;
 }
Exemplo n.º 4
0
 /**
  * Retrieve template name or template resolver
  *
  * @param  null|string $name
  * @return string|Resolver
  */
 public function resolver($name = null)
 {
     if (null === $this->__templateResolver) {
         $this->setResolver(new TemplatePathStack());
     }
     if (null !== $name) {
         return $this->__templateResolver->resolve($name, $this);
     }
     return $this->__templateResolver;
 }
Exemplo n.º 5
0
 public function canRender($nameOrModel)
 {
     if ($nameOrModel instanceof ModelInterface) {
         $nameOrModel = $nameOrModel->getTemplate();
     }
     $tpl = $this->resolver->resolve($nameOrModel);
     $ext = pathinfo($tpl, PATHINFO_EXTENSION);
     if ($tpl && $ext == $this->getSuffix()) {
         return true;
     }
     return false;
 }
Exemplo n.º 6
0
 /**
  * Processes a view script and returns the output.
  *
  * @param  string|ModelInterface   $nameOrModel The script/resource process, or a view model
  * @param  null|array|\ArrayAccess $values      Values to use during rendering
  * @return string The script output.
  * @throws \LogicException
  */
 public function render($nameOrModel, $values = null)
 {
     $name = $nameOrModel;
     if ($nameOrModel instanceof ModelInterface) {
         $name = $this->resolver->resolve($nameOrModel->getTemplate(), $this);
         $values = (array) $nameOrModel->getVariables();
     }
     if (array_key_exists('helper', $values)) {
         throw new \LogicException('Variable $helper is reserved for Zend helpers and can\'t be passed to view.');
     }
     $values['helper'] = $this->helpers;
     return $this->engine->renderToString($name, $values);
 }
Exemplo n.º 7
0
 /**
  * {@inheritDoc}
  */
 public function load(array $options = [])
 {
     if (!isset($options['image'])) {
         throw new Exception\InvalidArgumentException('Option "image" is required.');
     }
     $x = isset($options['x']) ? $options['x'] : 0;
     $y = isset($options['y']) ? $options['y'] : 0;
     $path = $this->resolver->resolve($options['image']);
     if (!$path) {
         throw new Exception\RuntimeException(sprintf('Could not resolve %s', $options['image']));
     }
     $image = $this->imagine->open($path);
     return new PasteFilter($image, $x, $y);
 }
Exemplo n.º 8
0
 /**
  * @param $name
  * @return array
  * @throws \Twig_Error_Loader
  */
 protected function findTemplate($name)
 {
     $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
     if (isset($this->cache[$name])) {
         return $this->cache[$name];
     }
     if (is_file($name)) {
         return $this->cache[$name] = $name;
     } else {
         if ($fallbackName = $this->fallbackResolver->resolve($name)) {
             return $this->cache[$fallbackName] = $fallbackName;
         }
     }
     throw new Twig_Error_Loader(sprintf('Unable to find template "%s".', $name));
 }
Exemplo n.º 9
0
 /**
  * Listen to dispatch event
  *
  * Retrieves "page" parameter from route matches. If none found, assumes
  * a 404 status code and page.
  *
  * Checks to see if the retrieved page can be resolved by the resolver. If
  * not, assumes a 404 code and page.
  *
  * Otherwise, returns a view model with a template matching the page from
  * this module.
  * 
  * @param  MvcEvent $e 
  * @return ViewModel
  */
 public function onDispatch(MvcEvent $e)
 {
     $model = new ViewModel();
     $matches = $e->getRouteMatch();
     $page = $matches->getParam('page', false);
     if (!$page) {
         return $this->return404Page($model, $e->getResponse());
     }
     $page = 'page-controller/' . $page;
     if (!$this->resolver->resolve($page)) {
         return $this->return404Page($model, $e->getResponse());
     }
     $model->setTemplate($page);
     $e->setResult($model);
     return $model;
 }
Exemplo n.º 10
0
 public function canRender($nameOrModel)
 {
     if ($nameOrModel instanceof ModelInterface) {
         $nameOrModel = $nameOrModel->getTemplate();
     }
     $tpl = $this->resolver->resolve($nameOrModel);
     return strlen($tpl) != 0;
 }
Exemplo n.º 11
0
 /**
  * Get the template directories
  *
  * @return TemplatePath[]
  */
 public function getPaths()
 {
     $paths = [];
     foreach ($this->resolver->getPaths() as $path) {
         $namespace = array_key_exists($path, $this->paths) ? $this->paths[$path] : null;
         $paths[] = new TemplatePath($path, $namespace);
     }
     return $paths;
 }
Exemplo n.º 12
0
 /**
  * {@inheritDoc}
  */
 public function resolve($name, RendererInterface $renderer = null)
 {
     $plugin = array($renderer, 'plugin');
     if (!is_callable($plugin)) {
         return false;
     }
     $helper = call_user_func($plugin, 'view_model');
     if (!$helper instanceof ViewModelHelper) {
         return false;
     }
     $currentModel = $helper->getCurrent();
     if (!$currentModel instanceof ModelInterface) {
         return false;
     }
     $currentTemplate = $currentModel->getTemplate();
     $position = strrpos($currentTemplate, self::NS_SEPARATOR);
     if (!$position) {
         return false;
     }
     return $this->resolver->resolve(substr($currentTemplate, 0, $position) . self::NS_SEPARATOR . $name, $renderer);
 }
Exemplo n.º 13
0
 /**
  * {@inheritDoc}
  */
 public function render($nameOrModel, $values = null)
 {
     /** @var \Zend\View\Helper\ViewModel $viewModelHelper */
     $viewModelHelper = $this->helperPluginManager->get('viewModel');
     // Because templates can be rendered recursively, we need to save the current context
     $previousViewModel = $viewModelHelper->getCurrent();
     $template = $this->resolver->resolve($nameOrModel->getTemplate());
     if (!$template) {
         throw new Exception\RuntimeException(sprintf('%s: Unable to render template "%s"; resolver could not resolve to a file', __METHOD__, $nameOrModel->getTemplate()));
     }
     // We need to save and restore the previous variables, because the same renderer can be used inside
     // multiple contexts
     $viewModelHelper->setCurrent($nameOrModel);
     $this->templateVariables = $nameOrModel->getVariables();
     $result = (include $template);
     // Restore the previous context
     $this->templateVariables = $previousViewModel->getVariables();
     $viewModelHelper->setCurrent($previousViewModel);
     return $result;
 }
Exemplo n.º 14
0
 /**
  * retrieve resolved template path
  *
  * @param string $template
  * @return string
  */
 private function resolveTemplate($template)
 {
     $template = str_replace(getcwd(), '', $this->viewResolver->resolve($template));
     return $template;
 }
Exemplo n.º 15
0
 /**
  * Can the template be rendered?
  *
  * A template can be rendered if the attached resolver can resolve the given
  * template name.
  * @param $name
  * @return bool
  */
 public function canRender($name)
 {
     $resolvedName = $this->resolver->resolve($name);
     return false !== $resolvedName;
 }
Exemplo n.º 16
0
 public function getLoader($loadCore = true)
 {
     if ($this->_coreScriptName === NULL) {
         if ($this->hasMvcEvent()) {
             $routeMatch = $this->mvcEvent->getRouteMatch();
             if ($routeMatch) {
                 $this->_coreScriptName .= '_' . str_replace('\\', '_', $routeMatch->getParam('controller'));
                 $this->_coreScriptName .= '_' . $routeMatch->getParam('action', 'index');
             }
         } else {
             $this->_coreScriptName = "jsloader";
         }
     }
     if ($loadCore) {
         $init = $this->loadClass(self::DEFAULT_PREFIX . ".Init", $this->_coreAppVars);
         //userVars werden in ein extra inline-Script geladen, da sie unabhänig vom Cache sein müssen
         $userVarsString = 'jQuery(function () {';
         foreach ($this->_userVars as $userKey => $userValue) {
             if ($userKey != '$ACTION') {
                 $userVarsString .= $this->_environment . '.vars["' . $userKey . '"] = ' . json_encode($userValue) . ';';
             }
         }
         $stageSetterString = '$CL.setStaging("' . $this->_stage . '");';
         $postInitString = '$CL.loadStack = ' . json_encode($this->_postInitLoadStack) . ';';
         $basePathHelper = $this->locator->get('ViewHelperManager')->get('basepath');
         if ($basePathHelper->__invoke() != "") {
             $indexPhp = "";
             if ($this->hasMvcEvent()) {
                 $uri = $this->mvcEvent->getRequest()->getUri()->toString();
             } else {
                 $uri = $_SERVER['REQUEST_URI'];
             }
             if (strpos($uri, 'index.php') !== false) {
                 $indexPhp = '/index.php';
             }
             $postInitString .= '$CL.basePath = "' . $basePathHelper->__invoke() . $indexPhp . '";';
         }
         //$CL.init() wird vor dem Controller eingebunden
         $userVarsString .= $stageSetterString . $postInitString . $init;
         //controller wird initialisiert
         if (array_key_exists('$ACTION', $this->_userVars)) {
             $userVarsString .= 'if (typeof ' . $this->_environment . '.controller != "undefined") {';
             $userVarsString .= 'if (typeof ' . $this->_environment . '.controller.' . $this->_userVars['$ACTION'] . 'Action == "function")';
             $userVarsString .= $this->_environment . '.controller.' . $this->_userVars['$ACTION'] . 'Action ()';
             $userVarsString .= '}';
         }
         //ende Document ready
         $userVarsString .= '});';
         $userScript = $this->_setScriptTag($userVarsString, "_core_userVars");
     } else {
         $userScript = "";
     }
     $locale = $this->translator->getLocale();
     if (!file_exists($this->_coreTmpPath . "/" . $this->_coreScriptName . '_' . $locale . ".js") || $this->_stage == "development") {
         if ($loadCore) {
             $this->_setRequiredVars();
             $core = $this->loadClass(self::DEFAULT_PREFIX . ".Core", $this->_coreAppVars);
         } else {
             $core = "";
         }
         //last class in stack should be added first
         $preInitLoadStack = new \Zend\Stdlib\ArrayStack($this->_preInitLoadStack);
         foreach ($preInitLoadStack as $jsClass) {
             $this->_addToInternalStack($jsClass);
         }
         //add client site translator to override $CL.translate()
         //and add $CL.translatePlural() and $CL.loadTranslation()
         if ($this->useClientTranslation) {
             $this->_addToInternalStack('Cl.Core.Translator');
         }
         $preInitContent = $this->_internalLoadStackToStr();
         if ($this->cacheLoadedClasses) {
             file_put_contents($this->getCacheFilePath(), serialize($this->_loadedClasses));
         }
         switch ($this->_coreMode) {
             case "src":
                 include_once 'JSMin.php';
                 //Datei lesend und schreibend öffnen, Zeiger wird an Anfang der Datei gesetzt
                 $fp = fopen($this->_coreTmpPath . "/" . $this->_coreScriptName . '_' . $locale . ".js", "w+");
                 //Datei für exklusiven Schreibzugriff sperren
                 flock($fp, 2);
                 fwrite($fp, \JSMin::minify($core . $preInitContent));
                 //Dateisperre wird wieder aufgehoben
                 flock($fp, 3);
                 fclose($fp);
                 break;
             case "scriptTag":
                 return $this->_setScriptTag($core . $preInitContent, "_Core") . $userScript;
                 break;
             default:
                 throw new \Jsloader\Javascript\Exception("unsupported core mode provided: " . $this->_coreMode);
         }
     }
     $templatesScript = "";
     if (count($this->templates) && (!file_exists($this->_coreTmpPath . '/' . $this->_coreScriptName . '_templates_' . $locale . '.js') || $this->_stage == "development")) {
         $fp = fopen($this->_coreTmpPath . "/" . $this->_coreScriptName . "_templates_" . $locale . ".js", "w+");
         //Datei für exklusiven Schreibzugriff sperren
         flock($fp, 2);
         if ($loadCore) {
             $tplStr = '__TEMPLATES__ = {};';
         } else {
             $tplStr = "";
         }
         foreach ($this->templates as $key => $tpl) {
             $tplPath = $this->viewResolver->resolve($tpl);
             if ($tplPath) {
                 $tpl = file_get_contents($tplPath);
             }
             foreach ($this->templateParsers as $parserKey) {
                 $parser = $this->locator->get($parserKey);
                 $tpl = $parser->parse($tpl);
             }
             $tplStr .= '__TEMPLATES__["' . $key . '"] = ' . json_encode($tpl) . ';';
         }
         fwrite($fp, $tplStr);
         //Dateisperre wird wieder aufgehoben
         flock($fp, 3);
         fclose($fp);
     }
     if (count($this->templates)) {
         $templatesScript = $this->_setScriptSrc($this->_coreScriptName . '_templates_' . $locale . '.js', "_templates");
     }
     if ($this->useClientTranslation && $loadCore && (!file_exists($this->_coreTmpPath . '/' . $this->_coreScriptName . '_translations_' . $locale . '.js') || $this->_stage == "development")) {
         $translationStr = '__TRANSLATIONS__ = ' . json_encode($this->getTranslator()->getMessages('default', $locale));
         $fp = fopen($this->_coreTmpPath . "/" . $this->_coreScriptName . "_translations_" . $locale . ".js", "w+");
         //Datei für exklusiven Schreibzugriff sperren
         flock($fp, 2);
         fwrite($fp, $translationStr);
         //Dateisperre wird wieder aufgehoben
         flock($fp, 3);
         fclose($fp);
     } else {
         $translationStr = "";
     }
     $translationsScript = "";
     if ($this->useClientTranslation && $loadCore) {
         $translationsScript = $this->_setScriptSrc($this->_coreScriptName . '_translations_' . $locale . '.js', "_translations");
     }
     return $templatesScript . $translationsScript . $this->_setScriptSrc($this->_coreScriptName . '_' . $locale . ".js", "_Core") . $userScript;
 }