/**
  * Returns an URL for the supplied asset.
  *
  * @param AssetInterface $asset   An asset
  * @param array          $options An array of options
  *
  * @return string An echo-ready URL
  */
 protected function getAssetUrl(AssetInterface $asset, array $options = array())
 {
     try {
         return $this->routingExtension->getPath('_assetic_' . $options['name']);
     } catch (\Exception $e) {
         throw RuntimeException::createFromPrevious($e);
     }
 }
 /**
  * Warms up the cache.
  *
  * @param string $cacheDir The cache directory
  */
 public function warmUp($cacheDir)
 {
     $engine = $this->container->get('templating.engine.smarty');
     $logger = $this->container->has('logger') ? $this->container->get('logger') : null;
     foreach ($this->finder->findAllTemplates() as $template) {
         try {
             $engine->compileTemplate($template, false);
         } catch (\Exception $e) {
             // problem during compilation, log it and give up
             $e = SmartyBundleRuntimeException::createFromPrevious($e, $template);
             if ($logger) {
                 $logger->warn(sprintf('Failed to compile Smarty template "%s": "%s"', (string) $template, $e->getMessage()));
             }
         }
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $finder = $this->getContainer()->get('smarty.templating.finder');
     $engine = $this->getContainer()->get('templating.engine.smarty');
     $bundleName = $input->getArgument('bundle');
     $verbose = $input->getOption('verbose');
     if ($bundleName && 0 === strpos($bundleName, '@')) {
         $bundle = $finder->getBundle(trim($bundleName, '@'));
         $templates = $finder->findTemplatesInBundle($bundle);
     } else {
         $templates = $finder->findAllTemplates();
     }
     $totalCtime = 0;
     $count = array('ok' => 0, 'failed' => 0);
     foreach ($templates as $template) {
         try {
             $startTime = microtime(true);
             $tpl = $engine->compileTemplate($template, false);
             if ($tpl instanceof \Smarty_Internal_Template) {
                 $ctime = microtime(true) - $startTime;
                 $totalCtime += $ctime;
                 $source = $tpl->source;
                 $compiled = $tpl->compiled;
                 if ($verbose) {
                     $output->writeln(sprintf("Compiled <info>%s</info>\n(into \"%s\") <comment>in %f secs</comment>", $source->resource, $compiled->filepath, $ctime));
                 }
                 $count['ok']++;
             } else {
                 throw new \RuntimeException('Unable to create a Smarty_Internal_Template instance');
             }
         } catch (\Exception $e) {
             $e = SmartyBundleRuntimeException::createFromPrevious($e, $template);
             // problem during compilation, log it and give up
             if ($verbose) {
                 $output->writeln("");
             }
             $output->writeln(sprintf("<error>ERROR: Failed to compile Smarty template \"%s\"</error>\n-> %s\n", (string) $template, $e->getMessage()));
             $count['failed']++;
         }
     }
     $output->write("\n<comment>Summary:</comment>\n");
     if ($count['ok'] > 0) {
         $output->write(sprintf("- Successfully compiled <info>%s</info> files.\n", $count['ok']));
     }
     if ($count['failed'] > 0) {
         $output->write(sprintf("- Failed to compile <error>%d</error> files.\n", $count['failed']));
     }
     $output->write(sprintf("- Total compilation time: <comment>%f secs</comment>.\n", $totalCtime));
 }
Example #4
0
 /**
  * Renders a template.
  *
  * @param mixed $name       A template name
  * @param array $parameters An array of parameters to pass to the template
  *
  * @return string The evaluated template as a string
  *
  * @throws \InvalidArgumentException if the template does not exist
  * @throws \RuntimeException         if the template cannot be rendered
  */
 public function render($name, array $parameters = array())
 {
     $template = $this->load($name);
     $this->registerFilters();
     $this->registerPlugins();
     // attach the global variables
     $parameters = array_replace($this->getGlobals(), $parameters);
     /**
      * Assign variables/objects to the templates.
      *
      * Description
      *  void assign(mixed var);
      *  void assign(string varname, mixed var, bool nocache);
      *
      * You can explicitly pass name/value pairs, or associative arrays
      * containing the name/value pairs.
      *
      * If you pass the optional third nocache parameter of TRUE, the
      * variable is assigned as nocache variable. See {@link http://www.smarty.net/docs/en/caching.cacheable.tpl#cacheability.variables} for details.
      *
      * Too learn more see {@link http://www.smarty.net/docs/en/api.assign.tpl}
      */
     $this->smarty->assign($parameters);
     /**
      * This returns the template output instead of displaying it. Supply a
      * valid template resource type and path. As an optional second
      * parameter, you can pass a $cache id, see the caching section for more
      * information.
      *
      * As an optional third parameter, you can pass a $compile_id. This is
      * in the event that you want to compile different versions of the same
      * template, such as having separate templates compiled for different
      * languages. You can also set the $compile_id variable once instead of
      * passing this to each call to this function.
      *
      * Too learn more see {@link http://www.smarty.net/docs/en/api.fetch.tpl}
      */
     try {
         return $this->smarty->fetch($template);
     } catch (\SmartyException $e) {
         throw RuntimeException::createFromPrevious($e, $template);
     } catch (\ErrorException $e) {
         throw RuntimeException::createFromPrevious($e, $template);
     }
 }