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); } } }
public function get($name) { if (!$this->fresh) { $this->loadCacheFiles(); } return parent::get($name); }
/** * Writes an asset. * * If the application or asset is in debug mode, each leaf asset will be * dumped as well. * * @param string $name An asset name * @param OutputInterface $output The command output */ private function dumpAsset($name, OutputInterface $output, array &$previously = array()) { $formula = $this->am->getFormula($name); if ($this->debug) { if (isset($formula[2]['debug']) && !$formula[2]['debug'] && $this->am->isDebug()) { return; } } $asset = $this->am->get($name); // start by dumping the main asset $this->doDump($asset, $output); // dump each leaf if debug if (isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug()) { foreach ($asset as $leaf) { $key = serialize($leaf); $mtime = $leaf->getLastModified(); if (isset($previously[$key])) { $changed = $previously[$key]['mtime'] != $mtime; } else { $changed = true; } $previously[$key] = array('mtime' => $mtime); if ($changed) { $this->doDump($leaf, $output); } } } if (!$this->debug) { // dump the main asset $this->doDump($asset, $output); } }
/** * Writes an asset. * * If the application or asset is in debug mode, each leaf asset will be * dumped as well. * * @param string $name An asset name * @param OutputInterface $output The command output */ private function dumpAsset($name, OutputInterface $output) { $asset = $this->am->get($name); $formula = $this->am->getFormula($name); if ($this->getConfig()->getEnv() != 'dev') { $this->doDump($asset, $output); } // dump each leaf if debug if (isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug()) { foreach ($asset as $leaf) { $this->doDump($leaf, $output); } } }
/** * Checks if an asset should be dumped. * * @param LazyAssetManager $am The asset manager * @param string $name The asset name * @param array &$previously An array of previous visits * * @return AssetInterface|Boolean The asset if it should be dumped */ protected function checkAsset(LazyAssetManager $am, $name, array &$previously) { $formula = $am->hasFormula($name) ? serialize($am->getFormula($name)) : null; $asset = $am->get($name); $mtime = $asset->getLastModified(); if (isset($previously[$name])) { $changed = $previously[$name]['mtime'] != $mtime || $previously[$name]['formula'] != $formula; } else { $changed = true; } $previously[$name] = array('mtime' => $mtime, 'formula' => $formula); return $changed ? $asset : false; }
/** * @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); } } } }