public function testAssetFormRegistration()
 {
     $app = new Application();
     $app->register(new AsseticExtension(), array('assetic.class_path' => __DIR__ . '/../../../../vendor/assetic/src', 'assetic.path_to_web' => sys_get_temp_dir(), 'assetic.assets' => $app->protect(function ($am) {
         $asset = new \Assetic\Asset\FileAsset(__FILE__);
         $asset->setTargetPath(md5(__FILE__));
         $am->set('test_asset', $asset);
     })));
     $app->get('/', function () use($app) {
         return 'AsseticExtensionTest';
     });
     $request = Request::create('/');
     $response = $app->handle($request);
     $this->assertTrue($app['assetic.asset_manager']->has('test_asset'));
     $this->assertInstanceOf('Assetic\\Asset\\FileAsset', $app['assetic.asset_manager']->get('test_asset'));
     $this->assertTrue(file_exists(sys_get_temp_dir() . '/' . md5(__FILE__)));
 }
 public function testAssetFormRegistration()
 {
     $app = new Application();
     $app->register(new AsseticServiceProvider());
     $app['assetic.path_to_web'] = sys_get_temp_dir();
     $app['assetic.asset_manager'] = $app->share($app->extend('assetic.asset_manager', function ($am, $app) {
         $asset = new \Assetic\Asset\FileAsset(__FILE__);
         $asset->setTargetPath(md5(__FILE__));
         $am->set('test_asset', $asset);
         return $am;
     }));
     $app->get('/', function () {
         return 'AsseticExtensionTest';
     });
     $request = Request::create('/');
     $response = $app->handle($request);
     $this->assertTrue($app['assetic.asset_manager']->has('test_asset'));
     $this->assertInstanceOf('Assetic\\Asset\\FileAsset', $app['assetic.asset_manager']->get('test_asset'));
     $this->assertTrue(file_exists(sys_get_temp_dir() . '/' . md5(__FILE__)));
 }
Example #3
0
 public function initAssets()
 {
     $options = $this->getOptions();
     $defaultWoptions = array('combined' => true, 'leaves' => false);
     $woptions = array();
     $am = $this->getAssetManager();
     $fm = $this->getFilterManager();
     $fassets = array();
     if (isset($options['parser'])) {
         foreach ($options['parser'] as $parser) {
             $filters = array();
             foreach ($parser['files'] as $key => $file) {
                 $filters[$key] = array();
                 if (isset($file['filters'])) {
                     foreach ($file['filters'] as $f) {
                         if (substr($f, 0, 1) == '?') {
                             $fn = substr($f, 1);
                             $init = (bool) (!$parser['debug']);
                         } else {
                             $fn = $f;
                             $init = true;
                         }
                         if ($init) {
                             if ($fm->has($fn)) {
                                 $filters[$key][] = $fm->get($fn);
                             }
                         }
                     }
                 }
             }
             $diterator = new \RecursiveDirectoryIterator($parser['directory']);
             $riterator = new \RecursiveIteratorIterator($diterator, \RecursiveIteratorIterator::SELF_FIRST);
             foreach ($riterator as $file) {
                 if (isset($parser['blacklist'])) {
                     foreach ($parser['blacklist'] as $bl) {
                         if (preg_match($bl, $file->getPathName())) {
                             continue;
                         }
                     }
                 }
                 foreach ($parser['files'] as $key => $fopts) {
                     $ppinfo = pathinfo($fopts['output']);
                     if ($file->isFile() && preg_match($fopts['pattern'], $file->getFilename())) {
                         $pinfo = pathinfo($file);
                         $pinfo['dirname'] = str_ireplace($parser['directory'], $ppinfo['dirname'], $pinfo['dirname']);
                         $pinfo['extension'] = $ppinfo['extension'];
                         $fasset = new \Assetic\Asset\FileAsset($file->getPathName(), $filters[$key]);
                         $fasset->setTargetUrl($pinfo['dirname'] . '/' . $pinfo['filename'] . '.' . $pinfo['extension']);
                         $fassets[] = $fasset;
                     }
                 }
             }
         }
     }
     $f = $this->getAssetFactory();
     if (isset($options['collections'])) {
         foreach ($options['collections'] as $name => $coll) {
             $woptions[$name] = isset($coll['write']) ? $coll['write'] : array();
             $woptions[$name] = array_merge($defaultWoptions, $woptions[$name]);
             $coll['options']['name'] = str_ireplace("{APPLICATION_REVISION}", APPLICATION_REVISION, $coll['options']['name']);
             $asset = $f->createAsset($coll['inputs'], $coll['filters'], $coll['options']);
             if ($coll['cache']) {
                 $asset = new \Assetic\Asset\AssetCache($asset, $this->getAssetCache());
             }
             $am->set($name, $asset);
         }
     }
     if (isset($options['fileAssets'])) {
         foreach ($options['fileAssets'] as $name => $fileAsset) {
             $asset = new \Assetic\Asset\FileAsset($fileAsset['path']);
             $asset->setTargetUrl($fileAsset['targetUrl']);
             if (isset($fileAsset['filters'])) {
                 foreach ($fileAsset['filters'] as $filter) {
                     $f = $this->getFilterManager()->get($filter);
                     $asset->ensureFilter($f);
                 }
             }
             $am->set($name, $asset);
         }
     }
     $am = $this->getAssetManager();
     $writer = new \Assetic\AssetWriter(APPLICATION_PATH . '/../public');
     foreach ($fassets as $fasset) {
         if (file_exists(APPLICATION_PATH . '/../public' . '/' . $fasset->getTargetUrl())) {
             $amod = filemtime(APPLICATION_PATH . '/../public' . '/' . $fasset->getTargetUrl());
             if ($fasset->getLastModified() <= $amod) {
                 continue;
             }
         }
         $writer->writeAsset($fasset);
     }
     foreach ($am->getNames() as $name) {
         $asset = $am->get($name);
         if (file_exists(APPLICATION_PATH . '/../public' . '/' . $asset->getTargetUrl())) {
             $amod = filemtime(APPLICATION_PATH . '/../public' . '/' . $asset->getTargetUrl());
             if ($asset->getLastModified() <= $amod) {
                 continue;
             }
         }
         $writeOptions = $woptions[$name];
         if ($writeOptions['combined']) {
             $writer->writeAsset($asset);
         }
         if ($writeOptions['leaves']) {
             foreach ($asset as $leaf) {
                 $writer->writeAsset($leaf);
             }
         }
     }
 }