kriswallsmith / assetic

Asset Management for PHP
MIT License
3.75k stars 555 forks source link

HttpAsset and Google Fonts #859

Open webbird opened 7 years ago

webbird commented 7 years ago

I've encountered an issue when using Bootstrap 4 and Bootswatch Themes. Some themes do an @import loading Google fonts.

Warning: file_get_contents(https://fonts.googleapis.com/css?family=Lato:400,700,400italic): failed to open stream: Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte. in \vendor\kriswallsmith\assetic\src\Assetic\Asset\HttpAsset.php on line 57

XDebug Call Stack:

17 0.5928 4719352 Assetic\AssetWriter->writeManagerAssets( ) ...\Assets.php:103
18 0.5928 4719624 Assetic\AssetWriter->writeAsset( ) ...\AssetWriter.php:53
19 0.5928 4728384 Assetic\Asset\AssetCollection->dump( ) ...\AssetWriter.php:68
20 0.6084 4902648 Assetic\Asset\BaseAsset->dump( ) ...\AssetCollection.php:151
21 0.6084 4902648 Assetic\Asset\FileAsset->load( ) ...\BaseAsset.php:103
22 0.6084 5095224 Assetic\Asset\BaseAsset->doLoad( ) ...\FileAsset.php:64
23 0.6084 5095496 Assetic\Filter\FilterCollection->filterLoad( ) ...\BaseAsset.php:94
24 0.6084 5095496 Assetic\Filter\CssImportFilter->filterLoad( ) ...\FilterCollection.php:62
25 0.6084 5096016 Assetic\Filter\BaseCssFilter->filterImports( ) ...\CssImportFilter.php:92
26 0.6084 5096016 Assetic\Util\CssUtils::filterImports( ) ...\BaseCssFilter.php:44
27 0.6084 5096504 Assetic\Util\CssUtils::filterCommentless( ) ...\CssUtils.php:76
28 0.6084 5290304 call_user_func:{Assetic\Util\CssUtils.php:109} ( ) ...\CssUtils.php:109
29 0.6084 5290304 Assetic\Util\CssUtils::Assetic\Util{closure}( ) ...\CssUtils.php:109
30 0.6084 5290304 preg_replace_callback ( ) ...\CssUtils.php:75
31 0.6084 5290896 Assetic\Filter\CssImportFilter->Assetic\Filter{closure}( ) ...\CssUtils.php:75
32 0.6084 5305896 Assetic\Asset\BaseAsset->dump( ) ...\CssImportFilter.php:85
33 0.6084 5305896 Assetic\Asset\HttpAsset->load( ) ...\BaseAsset.php:103
34 0.6084 5305896 file_get_contents ( ) ...\HttpAsset.php:57

I am going to change / extend the code to use curl instead... has anyone already done it?

webbird commented 7 years ago

This works for me:

public function load(FilterInterface $additionalFilter = null)
{
    if(function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, VarUtils::resolve($this->sourceUrl, $this->getVars(), $this->getValues()));
        curl_setopt($ch, CURLOPT_REFERER, VarUtils::resolve($this->sourceUrl, $this->getVars(), $this->getValues()));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, '<proxy>');
        curl_setopt($ch, CURLOPT_PROXYPORT, '<port>');
        $content = curl_exec($ch);
        curl_close($ch);
    } else {
        $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);
}

As I am behind a proxy, I'd like to add appropriate options. Where should they be placed? AssetFactory.php? Function setProxy()?