The performance of your application depends heavily on the number and size of assets on each page. Even your blazingly fastest Symfony2 application can be bogged down by bloated Javascript and CSS files. This session will give you a basic introduction to PHP's new asset management framework, Assetic, and explore how it integrates with Symfony2 for a pleasant, common sense developer experience.
1. Introducing Assetic
Asset Management for PHP 5.3
Kris Wallsmith February 9, 2011
2. @kriswallsmith
• Symfony core team member
• Doctrine contributor
• Symfony Guru at
• 10+ years experience with PHP and web development
• Open source evangelist and international speaker
3. OpenSky connects you with innovators,
trendsetters and tastemakers.You choose
the ones you like and each week they
invite you to their private online sales.
21. # /path/to/web/js/core.php
$core = new AssetCollection(array(
new FileAsset('/path/to/jquery.js'),
new GlobAsset('/path/to/js/core/*.js'),
));
$core->load(); many files into one: fewer HTTP requests
Merge
header('Content-Type: text/javascript');
echo $core->dump();
22. # /path/to/web/js/core.php
$core = new AssetCollection(array(
new FileAsset('/path/to/jquery.js'),
new GlobAsset('/path/to/js/core/*.js'),
), array(
new YuiCompressorJsFilter('/path/to/yui.jar'),
));
$core->load();merged asset: less data over the wire
Compress the
header('Content-Type: text/javascript');
echo $core->dump();
34. # /path/to/web/css/styles.php
$styles = new AssetCollection(array(
new AssetCollection(
array(new FileAsset('/path/to/main.sass')),
array(new SassFilter())
),
new FileAsset('/path/to/more.css'),
));
header('Content-Type: text/css');
echo $styles->dump();
35. # /path/to/web/css/styles.php
$styles = new AssetCollection(array(
new AssetCollection(
array(new FileAsset('/path/to/main.sass')),
array(new SassFilter())
),
new FileAsset('/path/to/more.css'),
), array(
new YuiCompressorCss('/path/to/yui.jar'),
));
Lazy! The filesystem isn't touched until now
header('Content-Type: text/css');
echo $styles->dump();
39. $am = new AssetManager();
$am->set('jquery',
new FileAsset('/path/to/jquery.js'));
40. $plugin = new AssetCollection(array(
new AssetReference($am, 'jquery'),
new FileAsset('/path/to/jquery.plugin.js'),
));
41. jQuery will only be included once
$core = new AssetCollection(array(
$jquery,
$plugin1,
$plugin2,
));
header('text/javascript');
echo $core->dump();
43. $yui = new YuiCompressorJs();
$yui->setNomunge(true);
$fm = new FilterManager();
$fm->set('yui_js', $yui);
44. jQuery will only be compressed once
$jquery = new FileAsset('/path/to/core.js');
$jquery->ensureFilter($fm->get('yui_js'));
$core = new AssetCollection(array(
$jquery,
new GlobAsset('/path/to/js/core/*.js'),
));
$core->ensureFilter($fm->get('yui_js'));
46. # /path/to/asset_factory.php
$fm = new FilterManager();
$fm->set('coffee', new CoffeeScriptFilter());
$fm->set('closure', new GoogleClosureCompilerApi());
$factory = new AssetFactory('/path/to/web');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
65. # /path/to/web/css/styles.php
$styles = new AssetCache(new AssetCollection(
array(new FileAsset('/path/to/main.sass')),
array(new SassFilter())
), new FilesystemCache('/path/to/cache'));
Run the filters once and cache the content
echo $styles->dump();