Skip to content

issue with CssRewriteFilter #584

Closed
wants to merge 1 commit into from

2 participants

@renarsvilnis

i have situation where i have a php controller assigned to url http://my.domain.com/cms/assets/core_css.php which returns a a combined css file. When i wanted to rewrite CSS urls for one of the matching the new relative urls, it didn't seem to work.

My asset folder is in the root directory.

Controller:

public function action_core_css()
    {
// check for enviroment
        $this->debug = \Fuel::$env == \FUEL::PRODUCTION ? false : true;

        $this->frontendDepend = 'assets'.DS.'frontend-depend'.DS;
        $this->frontendApp = 'assets'.DS.'frontend-app'.DS;

        $this->adminDepend = 'assets'.DS.'admin-depend'.DS;
        $this->adminApp = 'assets'.DS.'admin-app'.DS;

        $ac = new AssetCollection(array(
            new FileAsset($this->adminDepend.'bootstrap'.DS.'css'.DS.'bootstrap.css'),
            new FileAsset($this->adminDepend.'jquery.fileupload'.DS.'css'.DS.'blueimp-gallery.min.css'),
            new FileAsset($this->adminDepend.'jquery.fileupload'.DS.'css'.DS.'jquery.fileupload-ui.css'),
            new FileAsset($this->adminApp.'css'.DS.'warp-uuberadmin-strap.css'),
            new FileAsset($this->adminApp.'css'.DS.'warp-uuberadmin-style.css'),
        ));
        foreach ($ac as $leaf) {
            $leaf->setTargetPath('ubercms/assets/core_css/');
        }
       // $ac->setTargetPath('ubercms/assets/core_css/');

        $am = new AssetManager();
        $am->set('ubercms_core_css', $ac);

        $fm = new FilterManager();
        $fm->set('css_min', new CssMinFilter());
        $fm->set('css_url', new CssRewriteFilter());


        $factory = new AssetFactory('assets');
        $factory->setAssetManager($am);
        $factory->setFilterManager($fm);
        $factory->setDebug($this->debug);
        $factory->addWorker(new CacheBustingWorker());

        // connect asset with filter
        $css = $factory->createAsset(array(
            '@ubercms_core_css',
        ), array(
            'css_url',
            '?css_min' // ignore if dev
        ));

        // save cache
        // assetic takes care of checking if asset is already there
        $content = new AssetCache(
          $css,
          new FilesystemCache('assets'.DS.'cache')
        );

        header('Content-Type: text/css');
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $content->getLastModified()).' GMT');

        echo $content->dump();
    }

created CSS urls where:

'../../fonts/glyphicons-halflings-regular.eot

instead of:

'../../assets/admin-depend/bootstrap/fonts/glyphicons-halflings-regular.eot

I looked at the cssRewriteFilter source code and found that i didn't have correct target paths. So i added this to the code:

 foreach ($ac as $leaf) {
      $leaf->setTargetPath('ubercms/assets/core_css/');
 }

Next i found, that my in the cssRewriteFilter change $sourcePath to $sourceBase in few places because i $sourceBase is the path to the file and the $sourcePath is name of the file plus the extension. I ended up creating a working code. As i only picked up assetic few days ago, i am not sure, if it's my lack of knowledge or is it a bug.

@renarsvilnis renarsvilnis CssRewriteFilter
i have situation where i have a php controller assigned to url http://my.domain.com/cms/assets/core_css.php which returns a a combined css file. When i wanted to rewrite CSS urls for one of the matching the new relative urls, it didn't seem to work.

My asset folder is in the root directory.

Controller:
```
public function action_core_css()
    {
// check for enviroment
        $this->debug = \Fuel::$env == \FUEL::PRODUCTION ? false : true;

        $this->frontendDepend = 'assets'.DS.'frontend-depend'.DS;
        $this->frontendApp = 'assets'.DS.'frontend-app'.DS;

        $this->adminDepend = 'assets'.DS.'admin-depend'.DS;
        $this->adminApp = 'assets'.DS.'admin-app'.DS;

        $ac = new AssetCollection(array(
            new FileAsset($this->adminDepend.'bootstrap'.DS.'css'.DS.'bootstrap.css'),
            new FileAsset($this->adminDepend.'jquery.fileupload'.DS.'css'.DS.'blueimp-gallery.min.css'),
            new FileAsset($this->adminDepend.'jquery.fileupload'.DS.'css'.DS.'jquery.fileupload-ui.css'),
            new FileAsset($this->adminApp.'css'.DS.'warp-uuberadmin-strap.css'),
            new FileAsset($this->adminApp.'css'.DS.'warp-uuberadmin-style.css'),
        ));
        foreach ($ac as $leaf) {
            $leaf->setTargetPath('ubercms/assets/core_css/');
        }
       // $ac->setTargetPath('ubercms/assets/core_css/');

        $am = new AssetManager();
        $am->set('ubercms_core_css', $ac);

        $fm = new FilterManager();
        $fm->set('css_min', new CssMinFilter());
        $fm->set('css_url', new CssRewriteFilter());


        $factory = new AssetFactory('assets');
        $factory->setAssetManager($am);
        $factory->setFilterManager($fm);
        $factory->setDebug($this->debug);
        $factory->addWorker(new CacheBustingWorker());

        // connect asset with filter
        $css = $factory->createAsset(array(
            '@ubercms_core_css',
        ), array(
            'css_url',
            '?css_min' // ignore if dev
        ));

        // save cache
        // assetic takes care of checking if asset is already there
        $content = new AssetCache(
          $css,
          new FilesystemCache('assets'.DS.'cache')
        );

        header('Content-Type: text/css');
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $content->getLastModified()).' GMT');

        echo $content->dump();
    }
```

created CSS urls where:
```
'../../fonts/glyphicons-halflings-regular.eot
```
instead of:
```
'../../assets/admin-depend/bootstrap/fonts/glyphicons-halflings-regular.eot
```

I looked at the cssRewriteFilter source code and found that i didn't have correct target paths. So i added this to the code:
```
 foreach ($ac as $leaf) {
      $leaf->setTargetPath('ubercms/assets/core_css/');
 }
```
Next i found, that my in the cssRewriteFilter change $sourcePath to $sourceBase in few places because i $sourceBase is the path to the file and the $sourcePath is name of the file plus the extension. I ended up creating a working code. As i only picked up assetic few days ago, i am not sure, if it's my lack of knowledge or is it a bug.
d3b1653
@stof
Collaborator
stof commented Oct 14, 2014

This change is wrong (and running tests can tell it directly).

I suspect that your issue is related to a wrong usage of the asset source path vs the asset source root. The source root is not the path to the asset file, but to the document root (i.e. the path prepended to the asset path to build a filesystem path rather than a web path)

@stof stof closed this Oct 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.