public function dump() { $finder = new Finder(); $twigNamespaces = $this->loader->getNamespaces(); foreach ($twigNamespaces as $ns) { if (count($this->loader->getPaths($ns)) > 0) { $iterator = $finder->files()->in($this->loader->getPaths($ns)); foreach ($iterator as $file) { /** @var SplFileInfo $file */ $resource = new TwigResource($this->loader, '@' . $ns . '/' . $file->getRelativePathname()); $this->lam->addResource($resource, 'twig'); } } } foreach ($this->lam->getNames() as $name) { $asset = $this->lam->get($name); $formula = $this->lam->getFormula($name); $debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->lam->isDebug(); $combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : null; if (null !== $combine ? !$combine : $debug) { foreach ($asset as $leaf) { $this->aw->writeAsset($leaf); } } else { $this->aw->writeAsset($asset); } } }
private function scanTemplates() { $templates = Filesystem::scanForFiles($this->getConfig()->getTwigBasePath(), '', 'twig'); foreach ($templates as $template) { $resource = new TwigResource($this->twig()->getLoader(), $template); $this->am->addResource($resource, 'twig'); } }
/** * Locates twig templates and adds their defined assets to the lazy asset manager */ public function addTwigAssets() { if (!$this->twig instanceof \Twig_Environment) { throw new \LogicException('Twig environment not set'); } $finder = new Finder(); $viewfinder = $this->viewfinder; if (count($viewfinder->getPaths()) > 0) { $iterator = $finder->files()->in($viewfinder->getPaths()); foreach ($iterator as $file) { $resource = new TwigResource($this->loader, $file->getRelativePathname()); $this->lam->addResource($resource, 'twig'); } } }
/** * Locates twig templates and adds their defined assets to the lazy asset manager */ public function addTwigAssets() { if (!$this->twig instanceof \Twig_Environment) { throw new \LogicException('Twig environment not set'); } $twigNamespaces = $this->loader->getNamespaces(); foreach ($twigNamespaces as $ns) { if (count($this->loader->getPaths($ns)) > 0) { $iterator = Finder::create()->files()->in($this->loader->getPaths($ns)); foreach ($iterator as $file) { $resource = new TwigResource($this->loader, '@' . $ns . '/' . $file->getRelativePathname()); $this->lam->addResource($resource, 'twig'); } } } }
/** * Render a single file from a template to an output path * * @param string $templateName Name of the template for Twig * @param string $outputPath Relative path to render to * @param array $context Twig context */ public function renderFile($templateName, $outputPath, array $context) { $template = $this->twig->loadTemplate($templateName); $this->manager->addResource(new TwigResource($this->twig->getLoader(), $templateName), 'twig'); $event = new RenderEvent($this, $template, $context); $this->dispatcher->dispatch(BrancherEvents::RENDER, $event); $rendered = $template->render($event->context); $this->filesystem->dumpFile("{$this->outputDir}/{$outputPath}", $rendered); }
/** * Collect asset resources defined in twig templates * * @param FilterResponseEvent $event */ public function onKernelResponse(FilterResponseEvent $event) { if (count($this->templates) === 0) { // Automatically find templates only if loader compatible if ($this->loader instanceof \Twig_Loader_Filesystem) { $twigNamespaces = $this->loader->getNamespaces(); foreach ($twigNamespaces as $ns) { if (count($this->loader->getPaths($ns)) > 0) { $iterator = Finder::create()->files()->in($this->loader->getPaths($ns)); /* @var $file \Symfony\Component\Finder\SplFileInfo */ foreach ($iterator as $file) { $resource = new TwigResource($this->loader, '@' . $ns . '/' . $file->getRelativePathname()); $this->manager->addResource($resource, 'twig'); } } } } } else { foreach ($this->templates as $template) { $resource = new TwigResource($this->loader, $template); $this->manager->addResource($resource, 'twig'); } } }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $app = $this->getSilexApp(); $twig = $app->getTwig(); $factory = $app->getAssets(); $assetManager = new LazyAssetManager($factory); $assetManager->setLoader('twig', new TwigFormulaLoader($twig)); if ($input->getOption('ignore-folders')) { $output->writeln('<comment>' . date('H:i:s') . '</comment> <info>Skipping folders...</info>'); } else { foreach ($app['assets.folders'] as $folder) { $source = $folder['source']; $target = $folder['target']; if (!is_dir($target)) { $output->writeln('<comment>' . date('H:i:s') . '</comment> <info>[dir+]</info> ' . $this->getAbsolutePath($target)); if (false === @mkdir($target, 0777, true)) { throw new \RuntimeException('Unable to create directory ' . $target); } } $directoryIterator = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS); $iterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $file) { $path = $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); if ($file->isDir()) { if (is_dir($path)) { continue; } $output->writeln('<comment>' . date('H:i:s') . '</comment> <info>[dir+]</info> ' . $this->getAbsolutePath($path)); if (false === @mkdir($path, 0777, true)) { throw new \RuntimeException('Unable to create directory ' . $path); } } else { if (is_file($path) && md5_file($path) == md5_file($file)) { continue; } $output->writeln('<comment>' . date('H:i:s') . '</comment> <info>[file+]</info> ' . $this->getAbsolutePath($path)); if (false === @file_put_contents($path, file_get_contents($file))) { throw new \RuntimeException('Unable to write file ' . $path); } } } } } $directoryIterator = new RecursiveDirectoryIterator($app['twig.path']); $iterator = new RecursiveIteratorIterator($directoryIterator); $templates = new RegexIterator($iterator, '/^.+\\.twig$/i', RegexIterator::GET_MATCH); foreach ($templates as $file) { $file = str_replace(rtrim($app['twig.path'], '/') . '/', null, $file[0]); $resource = new TwigResource($twig->getLoader(), $file); $assetManager->addResource($resource, 'twig'); } foreach ($app['twig.templates'] as $name => $file) { $resource = new TwigResource($twig->getLoader(), $name); $assetManager->addResource($resource, 'twig'); } $writer = new AssetWriter($app['assets.output_path']); foreach ($assetManager->getNames() as $name) { $asset = $assetManager->get($name); foreach (VarUtils::getCombinations($asset->getVars(), []) as $combination) { $asset->setValues($combination); $path = $app['assets.output_path'] . '/' . VarUtils::resolve($asset->getTargetPath(), $asset->getVars(), $asset->getValues()); if (!is_dir($dir = dirname($path))) { $output->writeln('<comment>' . date('H:i:s') . '</comment> <info>[dir+]</info> ' . $this->getAbsolutePath($dir)); if (false === @mkdir($dir, 0777, true)) { throw new \RuntimeException('Unable to create directory ' . $dir); } } $output->writeln('<comment>' . date('H:i:s') . '</comment> <info>[file+]</info> ' . $this->getAbsolutePath($path)); if (false === @file_put_contents($path, $asset->dump())) { throw new \RuntimeException('Unable to write file ' . $path); } } } }
use Assetic\AssetManager; use Assetic\AssetWriter; use Assetic\Extension\Twig\AsseticExtension; use Assetic\Extension\Twig\TwigFormulaLoader; use Assetic\Extension\Twig\TwigResource; use Assetic\Factory\AssetFactory; use Assetic\Factory\LazyAssetManager; use Assetic\Filter\LessFilter; use Assetic\FilterManager; use Symfony\Component\Finder\Finder; $loader = new Twig_Loader_Filesystem(VIEWS_PATH); $options = ['cache' => DEBUG_APP ? false : 'cache']; $twig = new Twig_Environment($loader, $options); $assetManager = new AssetManager(); $filterManager = new FilterManager(); $filterManager->set('less', new LessFilter(NODE_PATH, [NODE_MODULE_PATH])); $assetFactory = new AssetFactory('assets/'); $assetFactory->setDebug(false); $assetFactory->setAssetManager($assetManager); $assetFactory->setFilterManager($filterManager); $twig->addExtension(new AsseticExtension($assetFactory)); $lazyAssetManager = new LazyAssetManager($assetFactory); $lazyAssetManager->setLoader('twig', new TwigFormulaLoader($twig)); $finder = new Finder(); $finder->files()->in('assets')->exclude('css')->exclude('js')->exclude('images')->name("*.twig"); foreach ($finder as $template) { $resource = new TwigResource($loader, $template->getFileName()); $lazyAssetManager->addResource($resource, 'twig'); } $writer = new AssetWriter('.'); $writer->writeManagerAssets($lazyAssetManager);
public function register(Container $app) { $app['assetic.assets'] = $app['assetic.filters'] = $app['assetic.workers'] = array(); $app['assetic.asset_manager'] = function () use($app) { $am = new AssetManager(); if (isset($app['assetic.assets'])) { $assets = $app['assetic.assets']; if (!is_array($assets)) { $assets = array($assets); } foreach ($assets as $name => $asset) { if (!is_array($asset)) { // não collection, transformar em collection $asset = array($asset); } $col = new AssetCollection(); foreach ($asset as $a) { if (is_string($a)) { // é referencia $a = new AssetReference($am, $a); } if (!$a instanceof AssetInterface) { throw new \InvalidArgumentException("'assetic.assets' precisa ser um array de AssetInterface"); } $col->add($a); } $am->set($name, $col); } } return $am; }; $app['assetic.filter_manager'] = function () use($app) { $fm = new FilterManager(); if (isset($app['assetic.filters'])) { $filters = $app['assetic.filters']; if (!is_array($filters)) { $filters = array($filters); } foreach ($filters as $name => $filter) { $fm->set($name, $filter); } } return $fm; }; $app['assetic.factory'] = function () use($app) { $factory = new AssetFactory($app['assetic.dist_path']); $factory->setAssetManager($app['assetic.asset_manager']); $factory->setFilterManager($app['assetic.filter_manager']); $factory->setDebug(isset($app['debug']) ? $app['debug'] : false); $factory->setDefaultOutput($app['assetic.dist_path']); if (isset($app['assetic.workers']) && is_array($app['assetic.workers'])) { foreach ($app['assetic.workers'] as $worker) { $factory->addWorker($worker); } } return $factory; }; $app['assetic.lazy_asset_manager'] = function () use($app) { $am = new LazyAssetManager($app['assetic.factory']); if (isset($app['twig'])) { // carrega os assets pelo twig $am->setLoader('twig', new TwigFormulaLoader($app['twig'])); $loader = $app['twig.loader.filesystem']; $namespaces = $loader->getNamespaces(); foreach ($namespaces as $ns) { if (count($loader->getPaths($ns)) > 0) { $iterator = Finder::create()->files()->in($loader->getPaths($ns)); foreach ($iterator as $file) { $resource = new TwigResource($loader, '@' . $ns . '/' . $file->getRelativePathname()); $am->addResource($resource, 'twig'); } } } } return $am; }; $app['assetic.asset_writer'] = function () use($app) { return new AssetWriter($app['assetic.dist_path']); }; if (isset($app['twig'])) { $app['twig'] = $app->extend('twig', function ($twig, $app) { $functions = array('cssrewrite' => array('options' => array('combine' => true))); $twig->addExtension(new AsseticExtension($app['assetic.factory'], $functions)); return $twig; }); } }
/** * @{inheritDoc} */ public function load($name, LazyAssetManager $mng) { $resource = new TwigResource($this->loader, $name); $mng->addResource($resource, 'twig'); }
/** * Render the Twig template with Assetic manager * * If Twig Loader method is string, we can render view as string template and * set the params, else there is no need to declare params or view in this method. * * @param string $view * @param array $params * @return void */ public function render($view = "", $params = array()) { $this->_ci->benchmark->mark('AttireRender_start'); # Autoload url helper (required) $this->_ci->load->helper('url'); # Set additional config functions/global vars inside Attire environment $this->set_config_globals(); $this->set_config_functions(); # Add default view path $this->add_path(VIEWPATH, 'VIEWPATH'); # Twig environment (master of puppets) $twig =& $this->_environment; $escaper = new Twig_Extension_Escaper('html'); $twig->addExtension($escaper); $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump')); # Declare asset manager and add global paths $am = new AssetManager(); # Assets global paths if ($this->_bundle_path !== NULL) { $class = $this->_ci->router->fetch_class(); $method = $this->_ci->router->fetch_method(); $directory = $this->_ci->router->directory; $this->_bundle_path = rtrim($this->_bundle_path, '/') . '/'; $absolute_path = rtrim($this->_bundle_path . $directory . 'assets', '/'); $global_assets = array('module_js' => array('path' => "{$absolute_path}/js/{$class}/{$method}/*", 'type' => 'Assetic\\Asset\\GlobAsset'), 'module_css' => array('path' => "{$absolute_path}/css/{$class}/{$method}/*", 'type' => 'Assetic\\Asset\\GlobAsset'), 'global_css' => array('path' => "{$absolute_path}/css/{$class}/*", 'type' => 'Assetic\\Asset\\GlobAsset'), 'global_js' => array('path' => "{$absolute_path}/js/{$class}/*", 'type' => 'Assetic\\Asset\\GlobAsset')); foreach (array_merge($global_assets, $this->_assets) as $global => $params) { $class_name = $params['type']; $am->set($global, new $class_name($params['path'])); } } # Declare filters manager $fm = new FilterManager(); $fm->set('cssrewrite', new CssRewriteFilter()); $absolute_path = rtrim("{$this->_paths["theme"]}{$this->_theme}", '/') . '/assets'; # Declare assetic factory with filters and assets $factory = new AssetFactory($absolute_path); $factory->setAssetManager($am); $factory->setFilterManager($fm); $factory->setDebug($this->_debug); # Add assetic extension to factory $absolute_path = rtrim($this->_paths['assets'], '/'); $factory->setDefaultOutput($absolute_path); $twig->addExtension(new AsseticExtension($factory)); # This is too lazy, we need a lazy asset manager... $am = new LazyAssetManager($factory); $am->setLoader('twig', new TwigFormulaLoader($twig)); # Add the Twig resource (following the assetic documentation) $resource = new TwigResource($this->_loader, $this->_default_template . $this->_extension); $am->addResource($resource, 'twig'); # Write all assets files in the output directory in one or more files try { $writer = new AssetWriter($absolute_path); $writer->writeManagerAssets($am); } catch (\RuntimeException $e) { $this->_show_error($e->getMessage()); } # Set current lexer if (!empty($this->_current_lexer)) { $lexer = new Twig_Lexer($this->_environment, $this->_current_lexer); $twig->setLexer($lexer); } try { # Render all childs if (!empty($this->_childs)) { foreach ($this->_childs as $child => $params) { $this->_params['views'] = $this->_views; echo $twig->render($child, array_merge($params, $this->_params)); } # Remove childs after the use $this->_childs = array(); } elseif (strlen($view) <= 1 && $this->_loader instanceof Twig_Loader_String) { echo $twig->render($this->_default_template . $this->_extension, $params); } } catch (Twig_Error_Syntax $e) { $this->_show_error($e->getMessage()); } $this->_ci->benchmark->mark('AttireRender_end'); }