/**
  * 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 getLastModified()
 {
     $source = VarUtils::resolve($this->source, $this->getVars(), $this->getValues());
     if (!is_file($source)) {
         throw new \RuntimeException(sprintf('The source file "%s" does not exist.', $source));
     }
     return filemtime($source);
 }
 public function load(FilterInterface $additionalFilter = null)
 {
     $content = @file_get_contents(VarUtils::resolve($this->sourceUrl, $this->getVars(), $this->getValues()));
     if (false === $content && !$this->ignoreErrors) {
         throw new \RuntimeException(sprintf('Unable to load asset from URL "%s"', $this->sourceUrl));
     }
     $this->doLoad($content, $additionalFilter);
 }
Example #4
0
 public function testVariableInPath()
 {
     $globasset = new GlobAsset(__DIR__ . '/*.php', array(), null, array('testvar'));
     $globasset->setTargetPath('{testvar}_*.php');
     $globasset->setValues(array('testvar' => 'works'));
     foreach ($globasset as $asset) {
         $target = VarUtils::resolve($asset->getTargetPath(), $asset->getVars(), $asset->getValues());
         $this->assertContains('works', $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());
         }
     }
 }
Example #6
0
 private function loadResourceFromRepo()
 {
     $path = VarUtils::resolve($this->path, $this->getVars(), $this->getValues());
     // Lazily load the resource. If the resource needs to be fetched from
     // the database, we only want to fetch it when really necessary.
     $resource = $this->repo->get($path);
     if (!$resource instanceof BodyResource) {
         throw new RuntimeException(sprintf('The loaded resource is not a BodyResource. Got: %s', is_object($resource) ? get_class($resource) : gettype($resource)));
     }
     $this->resource = $resource;
 }
Example #7
0
 /**
  * Initializes the collection based on the glob(s) passed in.
  */
 private function initialize()
 {
     foreach ($this->globs as $glob) {
         $glob = VarUtils::resolve($glob, $this->getVars(), $this->getValues());
         if (false !== ($paths = glob($glob))) {
             foreach ($paths as $path) {
                 if (is_file($path)) {
                     $this->add(new FileAsset($path, array(), $this->getSourceRoot()));
                 }
             }
         }
     }
     $this->initialized = true;
 }
Example #8
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()
            );
        }
    }
Example #9
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;
 }
Example #10
0
 protected function dumpManagerAssets(AssetManager $am)
 {
     foreach ($am->getNames() as $name) {
         $asset = $am->get($name);
         if ($am instanceof LazyAssetManager) {
             $formula = $am->getFormula($name);
         }
         $dump = true;
         if ($this->watching) {
             // watching mode
             $dump = false;
             $resolved = VarUtils::resolve($asset->getTargetPath(), $asset->getVars(), $asset->getValues());
             $dest = $this->pathToWeb . '/' . $resolved;
             if (file_exists($dest)) {
                 $destmtime = filemtime($dest);
             } else {
                 $destmtime = 0;
             }
             // compare source and destination mtime
             if ($asset->getLastModified() > $destmtime) {
                 if ($this->commandOutput) {
                     $this->commandOutput->writeln("Dumping {$dest}");
                 }
                 $dump = true;
             }
         }
         if ($dump) {
             $this->writer->writeAsset($asset);
             if (!isset($formula[2])) {
                 continue;
             }
             $debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $am->isDebug();
             $combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : null;
             if (null !== $combine ? !$combine : $debug) {
                 foreach ($asset as $leaf) {
                     $this->writer->writeAsset($leaf);
                 }
             }
         }
     }
 }
Example #11
0
 /**
  * Initializes the collection based on the glob(s) passed in.
  */
 private function initialize()
 {
     foreach ($this->globs as $glob) {
         $glob = VarUtils::resolve($glob, $this->getVars(), $this->getValues());
         // handle recursive globs
         $paths = false;
         if ($this->recursive) {
             $paths = $this->rglob($glob, GLOB_NOSORT);
         } else {
             $paths = glob($glob, GLOB_NOSORT);
         }
         if (false !== $paths) {
             foreach ($paths as $path) {
                 if (is_file($path)) {
                     $asset = new FileAsset($path, array(), $this->getSourceRoot(), null, $this->getVars());
                     $asset->setValues($this->getValues());
                     $this->add($asset);
                 }
             }
         }
     }
     $this->initialized = true;
 }
Example #12
0
 private function getAssetVarCombinations(AssetInterface $asset)
 {
     return VarUtils::getCombinations($asset->getVars(), $this->getContainer()->getParameter('assetic.variables'));
 }
Example #13
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);
 }
 private function parseRelativeInput($input, $currentDir, array $roots, array $vars, array $values)
 {
     // If the Puli resource relative to the current directory exists,
     // prefer to return that
     $inputWithoutVars = VarUtils::resolve($input, $vars, $values);
     if (null !== $currentDir && $this->repo->contains(Path::makeAbsolute($inputWithoutVars, $currentDir))) {
         $asset = $this->createPuliAsset(Path::makeAbsolute($input, $currentDir), $vars);
         $asset->setValues($values);
         return $asset;
     }
     // Otherwise check whether the relative path can be found in the roots
     foreach ($roots as $root) {
         if (is_file(Path::makeAbsolute($inputWithoutVars, $root))) {
             $absolute = Path::makeAbsolute($input, $root);
             $asset = $this->createFileAsset($absolute, $root, $input, $vars);
             $asset->setValues($values);
             return $asset;
         }
     }
     throw new RuntimeException(sprintf('The asset "%s" could not be found. Searched the Puli directory %s ' . 'and the file system directories %s.', $inputWithoutVars, $currentDir, implode(', ', $roots)));
 }
 /**
  * @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);
 }
 /**
  * @param AssetInterface[] $assets
  * @param array $combination
  * @param array|null $nodeInputs
  * @return array
  */
 protected function collectInputs(array $assets, array $combination, array $nodeInputs = null)
 {
     $inputs = array();
     foreach ($assets as $index => $asset) {
         if ($asset instanceof AssetCollectionInterface) {
             $inputs = array_merge($inputs, $this->collectInputs($asset->all(), $combination));
         } else {
             $sourcePath = $asset->getSourcePath();
             if ($sourcePath === null) {
                 if ($nodeInputs === null || !isset($nodeInputs[$index])) {
                     throw new \RuntimeException('Source not set in asset and cannot be guessed');
                 }
                 $assetPath = $nodeInputs[$index];
             } else {
                 $sourcePath = VarUtils::resolve($sourcePath, array_keys($combination), $combination);
                 $assetPath = $asset->getSourceRoot() . DIRECTORY_SEPARATOR . $sourcePath;
             }
             $inputs[] = $assetPath;
         }
     }
     return $inputs;
 }
 /**
  * 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;
 }
Example #18
0
 private function loadResourcesFromRepo()
 {
     $glob = VarUtils::resolve($this->glob, $this->getVars(), $this->getValues());
     // Lazily load the resources. If the resources are stored in a database,
     // we only want to fetch them if really necessary.
     foreach ($this->repo->find($glob) as $resource) {
         // Ignore non-file resources
         if (!$resource instanceof FileResource) {
             continue;
         }
         $this->add(new PuliResourceAsset($resource));
     }
     $this->loaded = true;
 }
 /**
  * @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());
 }