kriswallsmith / assetic

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

Twig Assetic LazyAssetManager file output problem? #870

Open bejutassle opened 6 years ago

bejutassle commented 6 years ago

Hello to everyone,

I am preparing a php library that I have developed individually. I use assig to compress twig, css and js files for the theme system in the library.

If you will be responsible for knowledgeable developers; Static file output problem does not occur when using GlobAsset or FileAsset. I get an error when I use twig with filter assetic. Two I've been working on this for days, but I have not found the solution.

I share your source code with you, I hope you can help.

Template.php

    static::$fileSystem = new Twig_Loader_Filesystem();
    static::$tpl = new Twig_Environment(static::$fileSystem, $configArr);

    $am = new AssetManager();
    $fm = new FilterManager();
    $fm->set('cssrewrite', new CssRewriteFilter());

    static::$factory = new AssetFactory('C:/WinNMP/WWW/bulk/css/');
    static::$factory->setAssetManager($am);
    static::$factory->setFilterManager($fm);
    static::$factory->setDebug(true);
    static::$factory->addWorker(new AssetVersionWorker('1.0.0'));
    //static::$factory->addWorker(new CacheBustingWorker());

    $am = new LazyAssetManager(static::$factory);

    // enable loading assets from twig templates, caching the formulae
    $am->setLoader('twig', new TwigCachingFormulaLoader(static::$tpl));
    $templates = array($dir.self::HtmlDir.'/content.twig');
    // loop through all your templates
    foreach ($templates as $template) {
        $resource = new TwigResource(static::$fileSystem, $template);
        $am->addResource($resource, 'twig');
    }

    $writer = new PersistentAssetWriter(Config::get('cache.assets.frontend'));
    $writer->writeManagerAssets($am);

    static::$tpl->addExtension(new AsseticExtension(static::$factory));

header.twig

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
{% block head %}
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{title}}</title>
    <meta name="language" content="tr" />
    <meta name="keywords" content="{{settings.settingKeywords}}" />
    <meta name="description" content="{{settings.settingDescription}}" />
    <meta name="robots" content="index, follow, odp" />
    <meta name="googlebot" content="index, follow, odp" />
    <base href="{{settings.siteHostAdress}}/"/>
    <link rel="shortcut icon" href="{{PIP('favicon.ico', 'site')}}">
    {% block stylesheets %}
          <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed:300,300i,400,400i,700,700i" rel="stylesheet">
          <link href="https://fonts.googleapis.com/css?family=Anton" rel="stylesheet">
          {% stylesheets 
          'C:/WinNMP/WWW/bulk/css/*'
          filter='cssrewrite' 
          output='css/all.css'
          %}
            <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
          {% endstylesheets %}

    {% endblock %}
    <script type="text/javascript">
    var settings = {{ settingsJson|json_encode() }};
    </script>
{% endblock %}
bejutassle commented 6 years ago

This is the error:

Fatal error: Uncaught exception 'LogicException' with message 'Unable to register extension "assetic" as extensions have already been initialized.' in C:\WinNMP\WWW\bulk\Libs\vendor\twig\twig\lib\Twig\Environment.php:937 Stack trace: #0 C:\WinNMP\WWW\bulk\Kernel\Core\Template.php(118): Twig_Environment->addExtension(Object(Assetic\Extension\Twig\AsseticExtension)) #1 C:\WinNMP\WWW\bulk\Kernel\Interview\Frontview.php(94): Core\Template::addPath('c:/winnmp/www/b...') #2 C:\WinNMP\WWW\bulk\App\Interview\Frontface\PageNotFound.php(13): Interview\Frontview->__construct() #3 C:\WinNMP\WWW\bulk\Kernel\Rewrite.php(58): Frontface\PageNotFound->__construct() #4 C:\WinNMP\WWW\bulk\Kernel\App.php(26): Rewrite::requestMap() #5 C:\WinNMP\WWW\bulk\index.php(31): App::Run() #6 {main} thrown in C:\WinNMP\WWW\bulk\Libs\vendor\twig\twig\lib\Twig\Environment.php on line 937