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); } } }
/** * Watches the asset manager for changes. * * This method includes an infinite loop the continuously polls the asset * manager for changes. * * @param InputInterface $input The command input * @param OutputInterface $output The command output */ private function watch(InputInterface $input, OutputInterface $output) { $cache = sys_get_temp_dir() . '/assetic_watch_' . substr(sha1($this->basePath), 0, 7); if ($input->getOption('force') || !file_exists($cache)) { $previously = array(); } else { $previously = unserialize(file_get_contents($cache)); if (!is_array($previously)) { $previously = array(); } } $error = ''; while (true) { try { foreach ($this->am->getNames() as $name) { if ($this->checkAsset($name, $previously)) { $this->dumpAsset($name, $output, $previously); } } // reset the asset manager $this->am->clear(); $this->am->load(); \Dev::write_file($cache, serialize($previously)); $error = ''; } catch (\Exception $e) { if ($error != ($msg = $e->getMessage())) { $output->writeln('<error>[error]</error> ' . $msg); $error = $msg; } } usleep($input->getOption('period') * 1000000); } }
/** * Watches a asset manager for changes. * * This method includes an infinite loop the continuously polls the asset * manager for changes. * * @param LazyAssetManager $am The asset manager * @param string $basePath The base directory to write to * @param OutputInterface $output The command output * @param Boolean $debug Debug mode */ protected function watch(LazyAssetManager $am, $basePath, OutputInterface $output, $debug = false) { $refl = new \ReflectionClass('Assetic\\AssetManager'); $prop = $refl->getProperty('assets'); $prop->setAccessible(true); $cache = sys_get_temp_dir() . '/assetic_watch_' . substr(sha1($basePath), 0, 7); if (file_exists($cache)) { $previously = unserialize(file_get_contents($cache)); } else { $previously = array(); } $error = ''; while (true) { try { foreach ($am->getNames() as $name) { if ($asset = $this->checkAsset($am, $name, $previously)) { $this->dumpAsset($asset, $basePath, $output); } } // reset the asset manager $prop->setValue($am, array()); if ($debug) { $am->load(); } file_put_contents($cache, serialize($previously)); $error = ''; sleep(1); } catch (\Exception $e) { if ($error != ($msg = $e->getMessage())) { $output->writeln('<error>[error]</error> ' . $msg); $error = $msg; } } } }
/** * @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); } } } }