protected function render($template, $data = [])
 {
     foreach ($data as $key => $value) {
         $this->smarty->assign($key, $value);
     }
     return $this->smarty->fetch(__DIR__ . DS . "fixtures" . DS . $template);
 }
 /**
  * @param  \SplFileInfo[]   $templates
  * @param $resourcesPath
  * @param $moduleCode
  * @throws \Exception
  * @throws \SmartyException
  */
 protected function processModule($templates, $resourcesPath, $modulePath, $moduleCode)
 {
     foreach ($templates as $template) {
         $fileName = str_replace("__MODULE__", $moduleCode, $template->getFilename());
         $fileName = str_replace("FIX", "", $fileName);
         $relativePath = str_replace($resourcesPath, "", $template->getPath() . DS);
         $completeFilePath = $modulePath . $relativePath . DS . $fileName;
         $isFix = false !== strpos($template->getFilename(), "FIX");
         // Expect special rule for Module\Module
         $isModuleClass = $modulePath . $relativePath . DS . $moduleCode . ".php" === $completeFilePath;
         if ($isFix && !file_exists($completeFilePath) || !$isFix) {
             if ($isModuleClass && is_file($completeFilePath)) {
                 $caught = false;
                 try {
                     $reflection = new \ReflectionClass("{$moduleCode}\\{$moduleCode}");
                 } catch (\ReflectionException $e) {
                     $caught = true;
                     // The class is not valid
                 }
                 if (!$caught && $reflection->hasConstant("MESSAGE_DOMAIN")) {
                     continue;
                     // If the class already have the constant, don't override it
                 }
             }
             $fetchedTemplate = $this->parser->fetch($template->getRealPath());
             $this->writeFile($completeFilePath, $fetchedTemplate, true, true);
         }
     }
 }
 protected function internalRender($resourceType, $resourceContent, array $data)
 {
     foreach ($data as $key => $value) {
         $this->smarty->assign($key, $value);
     }
     return $this->smarty->fetch(sprintf("%s:%s", $resourceType, $resourceContent));
 }
Example #4
0
 public function generate(ModuleGenerateEvent $event)
 {
     $templates = $this->findInPath($event->getResourcesPath(), "/__TABLE__.*\\.php\$/");
     $this->parser->assign("moduleCode", $event->getModuleCode());
     $this->parser->assign("tables", $event->getEntities());
     foreach ($event->getEntities() as $entity) {
         $this->processPhp($entity, $templates, $event->getResourcesPath(), $event->getModuleCode(), $event->getModulePath());
     }
 }
 /**
  * @param  \TheliaStudio\Parser\Entity\Table $table
  * @param  \SplFileInfo[]                    $templates
  * @param $resourcesPath
  * @param $moduleCode
  * @throws \Exception
  * @throws \SmartyException
  */
 protected function processTemplate(Table $table, $templates, $resourcesPath, $moduleCode)
 {
     $this->parser->assign("table", $table);
     foreach ($templates as $template) {
         $fetchedTemplate = $this->parser->fetch($template->getRealPath());
         $fileName = str_replace("__TABLE__", str_replace("_", "-", $table->getRawTableName()), $template->getFilename());
         $relativePath = str_replace($resourcesPath, "", $template->getPath() . DS);
         $completeFilePath = THELIA_MODULE_DIR . $moduleCode . DS . $relativePath . DS . $fileName;
         $this->writeFile($completeFilePath, $fetchedTemplate, false, true);
     }
 }
 protected function generateTemplates($resourcesPath, $modulePath, $moduleCode)
 {
     $templates = $this->findInPath($resourcesPath, "/__CONFIG_FORM__.*\\.html/");
     $previousLeft = $this->parser->left_delimiter;
     $previousRight = $this->parser->right_delimiter;
     $this->parser->left_delimiter = '[{';
     $this->parser->right_delimiter = '}]';
     /** @var \SplFileInfo $template */
     foreach ($templates as $template) {
         $fetchedTemplate = $this->parser->fetch($template->getRealPath());
         $relativePath = str_replace($resourcesPath, '', $template->getRealPath());
         $relativePath = str_replace("__CONFIG_FORM__", strtolower($moduleCode), $relativePath);
         $fullPath = $modulePath . $relativePath;
         $this->writeFile($fullPath, $fetchedTemplate, false, true);
     }
     $this->parser->left_delimiter = $previousLeft;
     $this->parser->right_delimiter = $previousRight;
 }
Example #7
0
 /**
  * Prepare template assets
  *
  * @param TemplateDefinition $templateDefinition the template to process
  * @param string $assets_directory the assets directory in the template
  * @param \TheliaSmarty\Template\SmartyParser $smartyParser the current parser.
  */
 protected function prepareTemplateAssets(TemplateDefinition $templateDefinition, $assets_directory, SmartyParser $smartyParser)
 {
     // Get the registered template directories for the current template path
     $templateDirectories = $smartyParser->getTemplateDirectories($templateDefinition->getType());
     if (isset($templateDirectories[$templateDefinition->getName()])) {
         /* create assets foreach registered directory : main @ modules */
         foreach ($templateDirectories[$templateDefinition->getName()] as $key => $directory) {
             // This is the assets directory in the template's tree
             $tpl_path = $directory . DS . $assets_directory;
             $asset_dir_absolute_path = realpath($tpl_path);
             if (false !== $asset_dir_absolute_path) {
                 // If we're processing template assets (not module assets),
                 // we will use the $assets_directory as the assets parent dir.
                 if (SmartyParser::TEMPLATE_ASSETS_KEY == $key && !null !== $assets_directory) {
                     $assetsWebDir = SmartyParser::TEMPLATE_ASSETS_KEY . DS . $assets_directory;
                 } else {
                     $assetsWebDir = $key;
                 }
                 Tlog::getInstance()->addDebug("Preparing assets: source assets directory {$asset_dir_absolute_path}, " . "web assets dir base: " . $this->web_root . $this->path_relative_to_web_root . ", " . "template: " . $templateDefinition->getPath() . ", " . "web asset key: {$assetsWebDir} (key={$key})");
                 $this->assetsManager->prepareAssets($asset_dir_absolute_path, $this->web_root . $this->path_relative_to_web_root, $templateDefinition->getPath(), $key . DS . $assets_directory);
             }
         }
     }
 }
Example #8
0
 /**
  *
  * find placeholders in the path and replace them by the given value
  *
  * @param $params
  * @param $path
  * @param $smarty
  * @return array the placeholders found
  */
 protected function resolvePath(&$params, &$path, $smarty)
 {
     $placeholder = [];
     foreach ($params as $key => $value) {
         if (false !== strpos($path, "%{$key}")) {
             $placeholder["%{$key}"] = SmartyParser::theliaEscape($value, $smarty);
             unset($params[$key]);
         }
     }
     $path = strtr($path, $placeholder);
     $keys = array_keys($placeholder);
     array_walk($keys, function (&$item, $key) {
         $item = str_replace('%', '', $item);
     });
     return $keys;
 }
Example #9
0
 /**
  * Create message with HTML and TEXT body from template HTMl and TEXT fields
  * using a text and a html layout
  */
 public function testMessageWithTextAndHtmlBodyAndTextAndHtmlExtendableLayout()
 {
     $message = new ModelMessage();
     $message->setLocale('fr_FR');
     $message->setSubject("The subject");
     $message->setTextMessage('TEXT <template> & content v={$myvar}');
     $message->setHtmlMessage('HTML <template> & content v={$myvar}');
     $message->setHtmlLayoutFileName('layout6.html.tpl');
     $message->setTextLayoutFileName('layout6.text.tpl');
     $path = $this->templateHelper->getActiveMailTemplate()->getAbsolutePath();
     $this->parser->assign('myvar', 'my-value');
     file_put_contents($path . DS . 'layout6.html.tpl', 'HTML Layout 6: {block name="message-body"}{$message_body nofilter}{/block}');
     file_put_contents($path . DS . 'layout6.text.tpl', 'TEXT Layout 6: {block name="message-body"}{$message_body nofilter}{/block}');
     $instance = \Swift_Message::newInstance();
     $message->buildMessage($this->parser, $instance);
     $this->assertEquals("The subject", $instance->getSubject());
     $this->assertEquals("HTML Layout 6: HTML <template> & content v=my-value", $instance->getBody());
     $this->assertEquals("TEXT Layout 6: TEXT <template> & content v=my-value", $instance->getChildren()[0]->getBody());
 }
 /**
  * Initialize the smarty parser.
  *
  * The intl function is replaced, and locales are assigned.
  *
  * @throws \SmartyException
  */
 protected function initParser()
 {
     $this->parser->unregisterPlugin('function', 'intl');
     $this->parser->registerPlugin('function', 'intl', [$this, 'translate']);
     $this->parser->assign("locales", $this->locales);
 }
Example #11
0
 /**
  * Process a {forhook rel="hookname"} ... {/forhook}
  *
  * The forhook iterates over the results return by a hookblock :
  *
  * {hookblock name="product.additional"}
  *      {forhook rel="product.additional"}
  *          <div id="{$id}">
  *              <h2>{$title}</h2>
  *              <p>{$content}</p>
  *          </div>
  *      {/forhook}
  * {/hookblock}
  *
  * @param array        $params
  * @param string       $content
  * @param \TheliaSmarty\Template\SmartyParser $smarty
  * @param bool         $repeat
  *
  * @throws \InvalidArgumentException
  * @return string                    the generated content
  */
 public function processForHookBlock($params, $content, $smarty, &$repeat)
 {
     $rel = $this->getParam($params, 'rel');
     if (null == $rel) {
         throw new \InvalidArgumentException($this->translator->trans("Missing 'rel' parameter in forHook arguments"));
     }
     /** @var FragmentBag $fragments */
     $fragments = null;
     // first call
     if ($content === null) {
         if (!array_key_exists($rel, $this->hookResults)) {
             throw new \InvalidArgumentException($this->translator->trans("Related hook name '%name' is not defined.", ['%name' => $rel]));
         }
         $fragments = $this->hookResults[$rel];
         $fragments->rewind();
         if ($fragments->isEmpty()) {
             $repeat = false;
         }
     } else {
         $fragments = $this->hookResults[$rel];
         $fragments->next();
     }
     if ($fragments->valid()) {
         /** @var Fragment $fragment */
         $fragment = $fragments->current();
         // On first iteration, save variables that may be overwritten by this hook
         if (!isset($this->varstack[$rel])) {
             $saved_vars = array();
             $varlist = $fragment->getVars();
             foreach ($varlist as $var) {
                 $saved_vars[$var] = $smarty->getTemplateVars($var);
             }
             $this->varstack[$rel] = $saved_vars;
         }
         foreach ($fragment->getVarVal() as $var => $val) {
             $smarty->assign($var, $val);
         }
         // continue iteration
         $repeat = true;
     }
     // end
     if (!$repeat) {
         // Restore previous variables values before terminating
         if (isset($this->varstack[$rel])) {
             foreach ($this->varstack[$rel] as $var => $value) {
                 $smarty->assign($var, $value);
             }
             unset($this->varstack[$rel]);
         }
     }
     if ($content !== null) {
         if ($fragments->isEmpty()) {
             $content = "";
         }
         return $content;
     }
     return '';
 }
Example #12
0
 /**
  * Check that views for the front routes exists.
  */
 public function testTargetFrontViewsExists()
 {
     /** @var Request $request */
     $request = $this->getContainer()->get('request');
     /** @var EventDispatcherInterface $eventDispatcher */
     $eventDispatcher = $this->getContainer()->get('event_dispatcher');
     // we are not going to do any actual rendering, so a mock ParserContext should be enough
     /** @var ParserContext $parserContext */
     $parserContext = $this->getMockBuilder('Thelia\\Core\\Template\\ParserContext')->disableOriginalConstructor()->getMock();
     $templateHelper = new TheliaTemplateHelper();
     $parser = new SmartyParser($request, $eventDispatcher, $parserContext, $templateHelper);
     $parser->setTemplateDefinition($templateHelper->getActiveFrontTemplate());
     $frontRouterFileLoader = $this->routerFileLoaders[static::$routingFilesPathFront];
     foreach (static::$routingFiles[static::$routingFilesPathFront] as $fileName) {
         $router = new Router($frontRouterFileLoader, $fileName);
         /** @var Route $route */
         foreach ($router->getRouteCollection() as $route) {
             if (null === ($view = $route->getDefault('_view'))) {
                 continue;
             }
             $this->assertTrue($parser->templateExists($view . '.html'), "Front view '{$view}' does not exist.");
         }
     }
 }
Example #13
0
 protected function render($template)
 {
     return $this->smarty->fetch(__DIR__ . DS . "fixtures" . DS . $template);
 }