Exemple #1
0
 protected function getAsseticFactory()
 {
     if (empty($this->config["input_path"][0])) {
         throw new \Exception('Empty input path');
     }
     $factory = new AssetFactory($this->config["input_path"][0], $this->config["debug"]);
     $factory->setDefaultOutput("");
     // add a FilterManager to the AssetFactory
     $fm = new FilterManager();
     $factory->setFilterManager($fm);
     // adding some filters to the filter manager
     if ($this->config["less_filter"]) {
         $lessphpFilter = new LessphpFilter();
         if ($this->lessPresets) {
             $lessphpFilter->setPresets($this->lessPresets);
         }
         $fm->set("less", $lessphpFilter);
     }
     if ($this->config["scss_filter"]) {
         $scssFilter = new ScssphpFilter();
         $fm->set("scss", $scssFilter);
     }
     $fm->set("min", new CssMinFilter());
     return $factory;
 }
Exemple #2
0
 private function getMinifiedAsset()
 {
     if (!(new SplFileInfo($this->asset->getPath()))->isWritable()) {
         throw new Exception("path " . $this->asset->getPath() . " is not writable");
     }
     $factory = new AssetFactory($this->asset->getPath());
     $factory->addWorker(new CacheBustingWorker());
     $factory->setDefaultOutput('*');
     $fm = new FilterManager();
     $fm->set('min', $this->filter);
     $factory->setFilterManager($fm);
     $asset = $factory->createAsset(call_user_func(function ($files) {
         $r = [];
         foreach ($files as $file) {
             $r[] = $file['fs'];
         }
         return $r;
     }, $this->files), ['min'], ['name' => $this->asset->getBasename()]);
     // only write the asset file if it does not already exist..
     if (!file_exists($this->asset->getPath() . DIRECTORY_SEPARATOR . $asset->getTargetPath())) {
         $writer = new AssetWriter($this->asset->getPath());
         $writer->writeAsset($asset);
         // TODO: write some code to garbage collect files of a certain age?
         // possible alternative, modify CacheBustingWorker to have option
         // to append a timestamp instead of a hash
     }
     return $asset;
 }
    public function testNoFilterManager()
    {
        $this->setExpectedException('LogicException', 'There is no filter manager.');

        $factory = new AssetFactory('.');
        $factory->createAsset(array('foo'), array('foo'));
    }
 /**
  * {@inheritDoc}
  */
 public function parse(\Twig_Token $token)
 {
     $inputs = $this->assets;
     $filters = [];
     $attributes = ['output' => $this->output, 'var_name' => 'asset_url', 'vars' => []];
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
             $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $this->parseValue($stream, false)))));
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
             $attributes['output'] = $this->parseValue($stream, false);
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) {
             $attributes['debug'] = $this->parseValue($stream);
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'combine')) {
             $attributes['combine'] = $this->parseValue($stream);
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse([$this, 'testEndTag'], true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $nameUnCompress = $this->factory->generateAssetName($inputs['compress'][0], $filters, $attributes);
     $nameCompress = substr(sha1(serialize($inputs['uncompress'][0]) . 'oro_assets'), 0, 7);
     return new OroAsseticNode(['compress' => $this->factory->createAsset($inputs['compress'][0], $filters, $attributes + ['name' => $nameCompress, 'debug' => false]), 'un_compress' => $this->factory->createAsset($inputs['uncompress'][0], [], $attributes + ['name' => $nameUnCompress, 'debug' => true])], ['un_compress' => $nameUnCompress, 'compress' => $nameCompress], $filters, $inputs, $body, $attributes, $token->getLine(), $this->getTag());
 }
Exemple #5
0
 public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 {
     $children = array();
     $includePaths = $this->includePaths;
     if (null !== $loadPath && !in_array($loadPath, $includePaths)) {
         array_unshift($includePaths, $loadPath);
     }
     if (empty($includePaths)) {
         return $children;
     }
     foreach (CssUtils::extractImports($content) as $reference) {
         if ('.css' === substr($reference, -4)) {
             continue;
         }
         // the reference may or may not have an extension or be a partial
         if (pathinfo($reference, PATHINFO_EXTENSION)) {
             $needles = array($reference, $this->partialize($reference));
         } else {
             $needles = array($reference . '.scss', $this->partialize($reference) . '.scss');
         }
         foreach ($includePaths as $includePath) {
             foreach ($needles as $needle) {
                 if (file_exists($file = $includePath . '/' . $needle)) {
                     $child = $factory->createAsset($file, array(), array('root' => $includePath));
                     $children[] = $child;
                     $child->load();
                     $children = array_merge($children, $this->getChildren($factory, $child->getContent(), $includePath));
                 }
             }
         }
     }
     return $children;
 }
 /**
  * Constructor
  * 
  * @param AssetFactory   $factory The AssetFactory 
  * @param CacheInterface $cache   The Cache Manager
  * @param AssetManager   $manager The Asset Manager
  * 
  * @return void 
  */
 public function __construct(AssetFactory $factory = null, CacheInterface $cache = null, $cacheDir = null, AssetManager $manager = null)
 {
     $this->factory = $factory;
     $this->manager = $manager;
     $this->cache = $cache;
     $this->cacheDirectory = $cacheDir;
     $this->factory->setAssetManager($this->getAssetManager());
 }
 public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 {
     $hash = md5($content);
     if ($this->cache->has($hash)) {
         return $factory->createAsset($this->cache->get($hash), array(), array('root' => $loadPath));
     } else {
         return parent::getChildren($factory, $content, $loadPath);
     }
 }
Exemple #8
0
 /**
  * @param $rootPath
  * @param $debug
  * @return AssetFactory
  */
 private function getAssetFactory($rootPath, $debug)
 {
     $filterManager = new FilterManager();
     $filterManager->set('yui_js', new JsCompressorFilter(dirname(__DIR__) . '/../optimizers/yuicompressor/yuicompressor-2.4.7.jar'));
     $assetFactory = new AssetFactory($rootPath, $debug);
     $assetFactory->setFilterManager($filterManager);
     $assetFactory->setAssetManager(new AssetManager());
     return $assetFactory;
 }
 public function process(AssetInterface $asset, AssetFactory $factory)
 {
     $path = $asset->getTargetPath();
     $ext = pathinfo($path, PATHINFO_EXTENSION);
     $lastModified = $factory->getLastModified($asset);
     if (null !== $lastModified) {
         $path = substr_replace($path, "{$lastModified}.{$ext}", -1 * strlen($ext));
         $asset->setTargetPath($path);
     }
 }
 protected function initialize(InputInterface $input, OutputInterface $output)
 {
     $this->basePath = $input->getArgument('write_to') ?: $this->getConfig()->getWebBasePath();
     $this->verbose = $input->getOption('verbose');
     $assetFactory = new AssetFactory($this->getConfig()->getAssetsBasePath(), $this->getConfig()->isDebug());
     $loader = new TwigFormulaLoader($this->twig());
     $this->am = new LazyAssetManager($assetFactory);
     $assetFactory->setAssetManager($this->am);
     $this->am->setLoader('twig', $loader);
 }
 protected function setUp()
 {
     parent::setUp();
     $this->am = $this->getMock('Assetic\\AssetManager');
     $this->fm = $this->getMock('Assetic\\FilterManager');
     $factory = new AssetFactory(__DIR__ . '/templates');
     $factory->setAssetManager($this->am);
     $factory->setFilterManager($this->fm);
     $this->engine->addExtension(new AsseticExtensionForTest($factory));
     $this->loader = new SmartyFormulaLoader($this->engine);
 }
 protected function factoryAf()
 {
     $uglify = new UglifyJs2Filter("/usr/bin/uglifyjs", "/usr/bin/node");
     $uglify->setCompress(true);
     $uglify->setMangle(true);
     $uglify->setCompress(true);
     $factory = new AssetFactory(__DIR__ . "/../");
     $filterManager = new FilterManager();
     $filterManager->set("uglify", $uglify);
     $factory->setFilterManager($filterManager);
     return $factory;
 }
Exemple #13
0
 protected function getHash(AssetInterface $asset, AssetFactory $factory)
 {
     $hash = hash_init('sha1');
     hash_update($hash, $factory->getLastModified($asset));
     if ($asset instanceof AssetCollectionInterface) {
         foreach ($asset as $i => $leaf) {
             $sourcePath = $leaf->getSourcePath();
             hash_update($hash, $sourcePath ?: $i);
         }
     }
     return substr(hash_final($hash), 0, 7);
 }
 /**
  * Builds the AssetFactory object
  *
  * @return \Assetic\Factory\AssetFactory
  */
 public function build()
 {
     $assetManager = new AssetManager();
     $filterManager = new FilterManager();
     foreach ($this->filters as $filterName => $filter) {
         $filterManager->set($filterName, $filter);
     }
     $assetsFactory = new AssetFactory($this->configurationHandler->webDir());
     $assetsFactory->setAssetManager($assetManager);
     $assetsFactory->setFilterManager($filterManager);
     return $assetsFactory;
 }
 protected function configureAssetTools()
 {
     $fm = new FilterManager();
     $fm->set('less', new LessphpFilter());
     $fm->set('cssrewrite', new AssetCssUriRewriteFilter());
     $fm->set('cssmin', new CssMinFilter());
     $fm->set('jscompile', new CompilerApiFilter());
     //$fm->set('jscompilejar', new CompilerJarFilter(storage_path('jar/compiler.jar')));
     $factory = new AssetFactory(Config::get('asset-manager::asset.paths.asset_path'));
     $factory->setFilterManager($fm);
     $factory->setDebug(Config::get('app.debug'));
     $this->assetFactory = $factory;
 }
Exemple #16
0
 protected function getAsseticFactory()
 {
     if (empty($this->config["input_path"][0])) {
         throw new \Exception("Empty input path");
     }
     $factory = new AssetFactory($this->config["input_path"][0], $this->config["debug"]);
     $factory->setDefaultOutput("");
     // add a FilterManager to the AssetFactory
     $fm = new FilterManager();
     $factory->setFilterManager($fm);
     $fm->set("jshrink", new JShrinkFilter());
     return $factory;
 }
Exemple #17
0
 public function renderThemeAssets()
 {
     $conf = (array) $this->getThemeAssets();
     $factory = new Factory\AssetFactory($conf['root_path']);
     $factory->setAssetManager($this->getAssetManager());
     $factory->setFilterManager($this->getFilterManager());
     $factory->setDebug($this->configuration->isDebug());
     $mobileDetect = $this->serviceManager->get('dxMobileDetect');
     $isTablet = $mobileDetect->isTablet();
     $assetName = 'assets';
     $filterName = 'filters';
     $optionName = 'options';
     $outputName = 'output';
     if ($mobileDetect->isMobile()) {
         $assetName = $assetName . 'Mobile';
         $filterName = $filterName . 'Mobile';
         $optionName = $optionName . 'Mobile';
         $outputName = $outputName . 'Mobile';
     }
     if ($mobileDetect->isTablet()) {
         $assetName = $assetName . 'Tablet';
         $filterName = $filterName . 'Tablet';
         $optionName = $optionName . 'Tablet';
         $outputName = $outputName . 'Mobile';
     }
     $collections = (array) $conf['collections'];
     foreach ($collections as $name => $options) {
         $assets = isset($options[$assetName]) ? $options[$assetName] : isset($options['assets']) ? $options['assets'] : array();
         $filtersx = isset($options[$filterName]) ? $options[$filterName] : isset($options['filters']) ? $options['filters'] : array();
         $options = isset($options[$optionName]) ? $options[$optionName] : isset($options['options']) ? $options['options'] : array();
         $options['output'] = isset($options[$outputName]) ? $options[$outputName] : isset($options['output']) ? $options['output'] : $name;
         $filters = $this->initFilters($filtersx);
         /** @var $asset \Assetic\Asset\AssetCollection */
         $asset = $factory->createAsset($assets, $filters, $options);
         # allow to move all files 1:1 to new directory
         # its particulary usefull when this assets are images.
         if (isset($options['move_raw']) && $options['move_raw']) {
             foreach ($asset as $key => $value) {
                 $name = md5($value->getSourceRoot() . $value->getSourcePath());
                 $value->setTargetPath($value->getSourcePath());
                 $value = $this->cache($value);
                 $this->assetManager->set($name, $value);
             }
         } else {
             $asset = $this->cache($asset);
             $this->assetManager->set($name, $asset);
         }
     }
     $writer = new AssetWriter($this->configuration->getWebPath());
     $writer->writeManagerAssets($this->assetManager);
 }
 protected function setUp()
 {
     if (!class_exists('Twig_Environment')) {
         $this->markTestSkipped('Twig is not installed.');
     }
     $this->am = $this->getMock('Assetic\\AssetManager');
     $this->fm = $this->getMock('Assetic\\FilterManager');
     $factory = new AssetFactory(__DIR__ . '/templates');
     $factory->setAssetManager($this->am);
     $factory->setFilterManager($this->fm);
     $twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addExtension(new AsseticExtension($factory, array('some_func' => array('filter' => 'some_filter', 'options' => array('output' => 'css/*.css')))));
     $this->loader = new TwigFormulaLoader($twig);
 }
 protected function setUp()
 {
     if (!class_exists('Twig_Environment')) {
         $this->markTestSkipped('Twig is not installed.');
     }
     $this->am = $this->getMock('Assetic\\AssetManager');
     $this->fm = $this->getMock('Assetic\\FilterManager');
     $this->valueSupplier = $this->getMock('Assetic\\ValueSupplierInterface');
     $this->factory = new AssetFactory(__DIR__ . '/templates');
     $this->factory->setAssetManager($this->am);
     $this->factory->setFilterManager($this->fm);
     $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__ . '/templates'));
     $this->twig->addExtension(new AsseticExtension($this->factory, array(), $this->valueSupplier));
 }
Exemple #20
0
 /**
  * {@inheritDoc}
  */
 public function init(Application $app)
 {
     $app['assets'] = $app->share(function () use($app) {
         $assetManager = new AssetManager();
         $filterManager = new FilterManager();
         $filterManager->set('less', new LessphpFilter());
         $filterManager->set('cssmin', new CssMinFilter());
         $filterManager->set('jsmin', new JSqueezeFilter());
         $factory = new AssetFactory($app['assets.path']);
         $factory->setAssetManager($assetManager);
         $factory->setFilterManager($filterManager);
         $factory->setDefaultOutput('misc/*');
         return $factory;
     });
 }
 /**
  * @param \Twig_NodeInterface $body
  * @param array $filters
  * @param array $attributes
  * @param int $lineno
  * @return AsseticNode
  */
 protected function createDebugAsseticNode(\Twig_NodeInterface $body, array $filters, array $attributes, $lineno)
 {
     $inputs = $this->assetsConfiguration->getCssFiles(true);
     $name = $this->assetFactory->generateAssetName($inputs, $filters, $attributes);
     $asset = $this->assetFactory->createAsset($inputs, $filters, $attributes + array('name' => $name));
     return new DebugAsseticNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $this->getTag());
 }
Exemple #22
0
 public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 {
     $loadPaths = $this->loadPaths;
     if (null !== $loadPath) {
         $loadPaths[] = $loadPath;
     }
     if (empty($loadPaths)) {
         return array();
     }
     $children = array();
     foreach (LessUtils::extractImports($content) as $reference) {
         if ('.css' === substr($reference, -4)) {
             // skip normal css imports
             // todo: skip imports with media queries
             continue;
         }
         if ('.less' !== substr($reference, -5)) {
             $reference .= '.less';
         }
         foreach ($loadPaths as $loadPath) {
             if (file_exists($file = $loadPath . '/' . $reference)) {
                 $coll = $factory->createAsset($file, array(), array('root' => $loadPath));
                 foreach ($coll as $leaf) {
                     $leaf->ensureFilter($this);
                     $children[] = $leaf;
                     goto next_reference;
                 }
             }
         }
         next_reference:
     }
     return $children;
 }
Exemple #23
0
 public function setAsset($asset)
 {
     $assets = $this->assetFactory->createAsset($asset)->all();
     $asset = $assets[0];
     $absolutePath = $asset->getSourceRoot() . DIRECTORY_SEPARATOR . $asset->getSourcePath();
     $pathInfo = pathinfo($absolutePath);
     list($width, $height, $imageType) = getimagesize($absolutePath);
     $this->absolutePath = $absolutePath;
     $this->width = $width;
     $this->height = $height;
     $this->imageType = $imageType;
     $this->ratio = $width / $height;
     $this->fileName = $pathInfo['filename'];
     $this->extension = $pathInfo['extension'];
     return $this;
 }
Exemple #24
0
 protected function getFilter($name)
 {
     if (!$this->getFilterManager()) {
         $this->setFilterManager($this->container->get('assetic.filter_manager'));
     }
     return parent::getFilter($name);
 }
 /**
  * {@inheritDoc}
  */
 public function load(ResourceInterface $resource)
 {
     $formulae = array();
     $filenames = preg_split("/\n+/", $resource->getContent());
     foreach ($filenames as $filename) {
         if (is_file($filename)) {
             $name = $this->factory->generateAssetName($filename, array());
             $output = $this->mapping->getModulePath($filename);
             if ($output) {
                 // @see \Assetic\Factory\AssetFactory::createAsset()
                 $formulae[$name] = array($filename, array(), array('output' => $output));
             }
         }
     }
     return $formulae;
 }
Exemple #26
0
 /**
  * Add assets from array to this collection.
  *
  * @param array $assets
  * @throws Exceptions\UnexpectedValueException
  */
 public function addAssets(array $assets)
 {
     // \0 is hack. NULL do not work because is internally converted to "" and str_pos(..., "") throw error
     $sourceRoot = $this->getSourceRoot() !== NULL ? $this->getSourceRoot() : "";
     $assetFactory = new AssetFactory($sourceRoot);
     foreach ($assets as $asset) {
         if (is_string($asset)) {
             $this->add($assetFactory->createAsset($asset));
         } else {
             if ($asset instanceof AssetInterface) {
                 $this->add($asset);
             } else {
                 throw new UnexpectedValueException("Unexpected asset type. Expected string or AssetInterface. Given " . (is_object($asset) ? get_class($asset) : gettype($asset)));
             }
         }
     }
 }
Exemple #27
0
 protected function parseInput($input)
 {
     // expand bundle notation
     if ('@' == $input[0] && false !== strpos($input, '/')) {
         $input = $this->kernel->locateResource($input);
     }
     return parent::parseInput($input);
 }
    protected function setUp()
    {
        if (!class_exists('Twig_Environment')) {
            $this->markTestSkipped('Twig is not installed.');
        }

        $this->am = $this->getMock('Assetic\\AssetManager');
        $this->fm = $this->getMock('Assetic\\FilterManager');

        $factory = new AssetFactory(__DIR__.'/templates');
        $factory->setAssetManager($this->am);
        $factory->setFilterManager($this->fm);

        $twig = new \Twig_Environment();
        $twig->addExtension(new AsseticExtension($factory));

        $this->loader = new TwigFormulaLoader($twig);
    }
 /**
  * Builds assetic attributes from parameters extracted from template
  * source.
  *
  * @param string $blockName Smarty block name
  * @param array  $params    Block attributes
  *
  * @return An array with assetic attributes ready to append to $formulae.
  */
 protected function buildFormula($blockName, array $params = array())
 {
     // inject the block name into the $params array
     $params[AsseticExtension::OPTION_SMARTY_BLOCK_NAME] = $blockName;
     list($inputs, $filters, $options) = $this->extension->buildAttributes($params);
     $asset = $this->factory->createAsset($inputs, $filters, $options);
     $options['output'] = $asset->getTargetPath();
     unset($options[AsseticExtension::OPTION_SMARTY_BLOCK_NAME]);
     return array($options['name'] => array($inputs, $filters, $options));
 }
Exemple #30
0
 /**
  * {@inheritDoc}
  */
 public function createAsset($inputs = array(), $filters = array(), array $options = array())
 {
     // Fixes problem with generated output
     if (!is_array($inputs)) {
         $inputs = array($inputs);
     }
     // set output the same as output if only one input
     if (count($inputs) === 1 && (!isset($options['output']) || $options['output'] === "*")) {
         $options['output'] = reset($inputs);
     }
     return parent::createAsset($inputs, $filters, $options);
 }