Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

AsseticExtension : "path_to_assets" + factory "AutoRegistration" #9

Closed
gcrico opened this Issue Nov 22, 2011 · 3 comments

Comments

Projects
None yet
4 participants

gcrico commented Nov 22, 2011

Hi,

The AssetFactory's constructor first argument is a path to the "root" dir of the assets.
So we sould not use "path_to_web" as the first argument, but an other config option ("assetic.path_to_assets"?).

// SilexExtension/AsseticExtension.php

---            $factory = new AssetFactory($app['assetic.path_to_web'], $options['debug']);
+++            $factory = new AssetFactory($app['assetic.path_to_assets'], $options['debug']);
// Root directory for loading assets (when relative path are provided)
$app['assetic.path_to_assets'] = __DIR__ . '/assets';
// Location where to dump all generated files
$app['assetic.path_to_web']  = $app['web_dir'];

I wrote a special "Assetic Factory Worker" (see code below) that adds the asset created with the AssetFactory to the provided AssetManager.
So we can do :

$app['assetic']->addWorker(
    new \SilexExtension\AsseticAutoRegisterWorker($app['assetic.path_to_web'], $app['assetic.asset_manager'])
);

With this system, we can use the assetic twig extension functions quite easily, without declaring assets in the app, nor using any "FormulaLoader" on the templates.

Example :

{% stylesheets 'Twitter-bootstrap/lib/bootstrap.less' filter='less,?yui_css' output='/css/*.css' %}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

When the twig template is parsed, the AssetFactory#createAsset() is invoked, which calls the Worker.

Note that the asset is added to the asset manager (and will be generated) only at template compile time!

So it's quite fast, but one may need to disable twig cache during developpment (to be usable).

$app['twig.options']    = array(
    'cache' => $app['debug'] ? null : $app['cache_dir'] . '/twig',
    'debug' => $app['debug'],
);

Here is the AsseticAutoRegisterWorker class :

(The "path_to_web" and the "only if modified" stuff could be removed, to keep it simple.)

<?php
namespace SilexExtension;

use \Assetic\Factory\Worker\WorkerInterface;
use \Assetic\Asset\AssetInterface;
use \Assetic\AssetManager;

/**
 * This worker adds the asset created with the AssetFactory to the provided AssetManager.
 * (by example: by assetic twig extension functions).
 * 
 */
class AsseticAutoRegisterWorker implements WorkerInterface {
    static private $nameSequencer = 0;
    private $path_to_web;
    private $am;

    /**
     * @param string $path_to_web
     * @param string $factory
     */
    public function __construct($path_to_web, AssetManager $am) {
        $this->path_to_web = $path_to_web;
        $this->am = $am;
    }

    /**
     * Processes an Asset Collection.
     * @param AssetInterface $asset
     * @return null
     */
    public function process(AssetInterface $asset) {
        // get the real targetPath
        $targetPath = $this->path_to_web . '/' . $asset->getTargetPath();

        // add to AssetManager only if modified, comparing "LastModified".
        if (! file_exists($targetPath) || filemtime($targetPath) < $asset->getLastModified()) {
            //echo '<pre>UPDATING: '.htmlspecialchars(print_r($asset->getTargetPath(), true)).'</pre>';
            $name = 'AutoRegistered' . (++self::$nameSequencer);
            $this->am->set($name, $asset);
        }
    }
}

Sorry not being able to provide a "pull request". Git newbie. And I extracted the code above from a deep "fork in the hurry"...

Collaborator

sveneisenschmidt commented Jan 12, 2012

Can you please try to create a pull request and I'll merch it into another branch and test it.

zemd commented Jul 2, 2012

tried this code - it works great! thanks @gcrico

Collaborator

sveneisenschmidt commented Jul 2, 2012

Need a PR.

@ghost ghost assigned mheap Nov 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment