/**
  * Performs the asset dump.
  *
  * @param AssetInterface  $asset  An asset
  * @param OutputInterface $stdout The command output
  *
  * @throws RuntimeException If there is a problem writing the asset
  */
 private function doDump(AssetInterface $asset, OutputInterface $stdout)
 {
     $combinations = VarUtils::getCombinations($asset->getVars(), $this->getContainer()->getParameter('assetic.variables'));
     foreach ($combinations as $combination) {
         $asset->setValues($combination);
         // resolve the target path
         $target = rtrim($this->basePath, '/') . '/' . $asset->getTargetPath();
         $target = str_replace('_controller/', '', $target);
         $target = VarUtils::resolve($target, $asset->getVars(), $asset->getValues());
         if (!is_dir($dir = dirname($target))) {
             $stdout->writeln(sprintf('<comment>%s</comment> <info>[dir+]</info> %s', date('H:i:s'), $dir));
             if (false === @mkdir($dir, 0777, true)) {
                 throw new \RuntimeException('Unable to create directory ' . $dir);
             }
         }
         $stdout->writeln(sprintf('<comment>%s</comment> <info>[file+]</info> %s', date('H:i:s'), $target));
         if (OutputInterface::VERBOSITY_VERBOSE <= $stdout->getVerbosity()) {
             if ($asset instanceof AssetCollectionInterface) {
                 foreach ($asset as $leaf) {
                     $root = $leaf->getSourceRoot();
                     $path = $leaf->getSourcePath();
                     $stdout->writeln(sprintf('        <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
                 }
             } else {
                 $root = $asset->getSourceRoot();
                 $path = $asset->getSourcePath();
                 $stdout->writeln(sprintf('        <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
             }
         }
         if (false === @file_put_contents($target, $asset->dump())) {
             throw new \RuntimeException('Unable to write file ' . $target);
         }
     }
 }
 public function writeAsset(AssetInterface $asset)
 {
     foreach (VarUtils::getCombinations($asset->getVars(), $this->values) as $combination) {
         $asset->setValues($combination);
         $path = $this->dir . '/' . VarUtils::resolve($asset->getTargetPath(), $asset->getVars(), $asset->getValues());
         if (!is_dir($path) && (!file_exists($path) || filemtime($path) <= $asset->getLastModified())) {
             static::write($path, $asset->dump());
         }
     }
 }
 public function replace(Node $node, TemplateContext $context)
 {
     // we only overwrite assetic nodes
     if (!$node instanceof AsseticNode) {
         return null;
     }
     // we leave images as they are
     $tagName = $node->getNodeTag();
     if (!in_array($tagName, array('stylesheets', 'javascripts'), true)) {
         return null;
     }
     // we only handle collection of file assets
     $asset = $node->getAttribute('asset');
     if (!$asset instanceof AssetCollectionInterface) {
         throw new \RuntimeException('Unexpected asset type: ' . get_class($asset));
     }
     // skip assetic nodes that has some (not-ignored) filters defined
     $additionalFilters = array_diff($node->getAttribute('filters'), $this->ignoredFilters);
     if (count($additionalFilters) > 0) {
         $context->addNotice(sprintf('Skipping %s in %s as filter is used (%s). Remove filter and re-run if you want to migrate this tag', $tagName, $context->getTemplateName(), implode(' ', $additionalFilters)));
         return null;
     }
     $count = $context->getAttribute(self::ATTRIBUTE_NODE_COUNT);
     $count = $count !== null ? $count : 0;
     $context->setAttribute(self::ATTRIBUTE_NODE_COUNT, $count + 1);
     $postfix = $count === 0 ? '' : '-' . $count;
     $templateName = $context->getTemplateName();
     $assetPath = str_replace('/Resources/views/', '/' . $this->pathInBundle . '/', $templateName);
     $assetPath = preg_replace('/(\\.html)?\\.twig$/', '', $assetPath) . $postfix . '.js';
     $variables = $asset->getVars();
     if (count($variables) === 0) {
         $expression = $this->dumpAssets($node, $asset, $tagName, $assetPath);
     } else {
         $combinations = VarUtils::getCombinations($variables, $this->possibleVariables);
         $expression = null;
         foreach ($combinations as $combination) {
             $combinationAssetPath = substr($assetPath, 0, -2) . implode('.', $combination) . '.js';
             if ($expression === null) {
                 // first combination will be in "else" part (if no other combinations will match)
                 $expression = $this->dumpAssets($node, $asset, $tagName, $combinationAssetPath, $combination);
             } else {
                 // we recursively build condition like this:
                 //     matchesCombination ? webpack_asset(combinationPath) : [recursion]
                 $expression = $this->buildCombinationCondition($combination) . "\n" . '? ' . $this->dumpAssets($node, $asset, $tagName, $combinationAssetPath, $combination) . "\n" . ': (' . "\n" . $this->indent($expression) . "\n" . ')';
             }
         }
     }
     $body = $node->getNode('body');
     // we find and replace "asset_url" to our expression
     return $this->replaceAssetUrl($body, $body, $expression);
 }
示例#4
0
    public function writeAsset(AssetInterface $asset)
    {
        foreach (VarUtils::getCombinations($asset->getVars(), $this->values) as $combination) {
            $asset->setValues($combination);

            static::write(
                $this->dir.'/'.VarUtils::resolve(
                    $asset->getTargetPath(),
                    $asset->getVars(),
                    $asset->getValues()
                ),
                $asset->dump()
            );
        }
    }
示例#5
0
 /**
  * Checks if an asset should be dumped.
  *
  * @param string $name        The asset name
  * @param array  &$previously An array of previous visits
  *
  * @return Boolean Whether the asset should be dumped
  */
 private function checkAsset($name, array &$previously)
 {
     $formula = $this->am->hasFormula($name) ? serialize($this->am->getFormula($name)) : null;
     $asset = $this->am->get($name);
     $combinations = VarUtils::getCombinations($asset->getVars(), $this->getApplication()->getContainer()->getParameter('assetic.variables'));
     $mtime = 0;
     foreach ($combinations as $combination) {
         $asset->setValues($combination);
         $mtime = max($mtime, $this->am->getLastModified($asset));
     }
     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;
 }
示例#6
0
 private function getAssetVarCombinations(AssetInterface $asset)
 {
     return VarUtils::getCombinations($asset->getVars(), $this->getContainer()->getParameter('assetic.variables'));
 }
示例#7
0
 /**
  * Not used.
  *
  * This method is provided for backward compatibility with certain versions
  * of AsseticBundle.
  */
 private function getCombinations(array $vars)
 {
     return VarUtils::getCombinations($vars, $this->values);
 }
 /**
  * Performs the asset dump.
  *
  * @param AssetInterface $asset An asset
  * @param OutputInterface $stdout The command output
  * @return array
  */
 private function getGulpConfig(AssetInterface $asset, OutputInterface $stdout)
 {
     $combinations = VarUtils::getCombinations($asset->getVars(), $this->getContainer()->getParameter('assetic.variables'));
     // see http://stackoverflow.com/questions/30133597/is-there-a-way-to-merge-less-with-css-in-gulp
     // for a method of merging css and less
     $assets = [];
     foreach ($combinations as $combination) {
         $asset->setValues($combination);
         // resolve the target path
         $target = rtrim($this->basePath, '/') . '/' . $asset->getTargetPath();
         $target = str_replace('_controller/', '', $target);
         $target = VarUtils::resolve($target, $asset->getVars(), $asset->getValues());
         if (!is_dir($dir = dirname($target))) {
             $stdout->writeln(sprintf('<comment>%s</comment> <info>[dir+]</info> %s', date('H:i:s'), $dir));
             if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
                 throw new \RuntimeException('Unable to create directory ' . $dir);
             }
         }
         $stdout->writeln(sprintf('<comment>%s</comment> <info>[file+]</info> %s', date('H:i:s'), $target));
         $gulpAsset = new GulpAsset();
         $gulpAsset->setDestination($target);
         $asset = $this->resolveAsset($asset);
         $this->gulpAsset($gulpAsset, $asset, $stdout);
         $assets[] = $gulpAsset->getArrayConfig();
     }
     return $assets;
 }
 /**
  * @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);
             }
         }
     }
 }
 private function getAssetVarCombinations(AssetInterface $asset)
 {
     return VarUtils::getCombinations($asset->getVars(), array());
 }
 /**
  * @dataProvider getCombinationTests
  */
 public function testGetCombinations($vars, $expected)
 {
     $actual = VarUtils::getCombinations($vars, array('locale' => array('en', 'de', 'fr'), 'browser' => array('ie', 'firefox', 'other'), 'gzip' => array('gzip', '')));
     $this->assertEquals($expected, $actual);
 }