Skip to content

Commit

Permalink
[AsseticBundle] initial entry of assetic integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith authored and fabpot committed Feb 13, 2011
1 parent b5972f3 commit 1292925
Show file tree
Hide file tree
Showing 52 changed files with 1,952 additions and 0 deletions.
1 change: 1 addition & 0 deletions autoload.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $loader->registerNamespaces(array(
'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine-dbal/lib',
'Doctrine' => __DIR__.'/vendor/doctrine/lib',
'Zend' => __DIR__.'/vendor/zend/library',
'Assetic' => __DIR__.'/vendor/assetic/src',
));
$loader->registerPrefixes(array(
'Swift_' => __DIR__.'/vendor/swiftmailer/lib/classes',
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Bundle/AsseticBundle/AsseticBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\AsseticBundle;

use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\AssetManagerPass;
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\FilterManagerPass;
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\TemplatingPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Assetic integration.
*
* @author Kris Wallsmith <[email protected]>
*/
class AsseticBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
{
parent::registerExtensions($container);

$container->addCompilerPass(new AssetManagerPass());
$container->addCompilerPass(new FilterManagerPass());
$container->addCompilerPass(new TemplatingPass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\AsseticBundle\CacheWarmer;

use Assetic\AssetManager;
use Assetic\AssetWriter;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;

class AssetWriterCacheWarmer extends CacheWarmer
{
protected $am;
protected $writer;

public function __construct(AssetManager $am, AssetWriter $writer)
{
$this->am = $am;
$this->writer = $writer;
}

public function warmUp($cacheDir)
{
$this->writer->writeManagerAssets($this->am);
}

public function isOptional()
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\AsseticBundle\CacheWarmer;

use Symfony\Bundle\AsseticBundle\Templating\FormulaLoader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Component\HttpKernel\Kernel;

class PhpTemplatingAssetsCacheWarmer extends CacheWarmer
{
protected $kernel;
protected $loader;

public function __construct(Kernel $kernel, FormulaLoader $loader)
{
$this->kernel = $kernel;
$this->loader = $loader;
}

public function warmUp($cacheDir)
{
$formulae = array();
foreach ($this->kernel->getBundles() as $name => $bundle) {
if (is_dir($dir = $bundle->getPath().'/Resources/views/')) {
$finder = new Finder();
$finder->files()->name('*.php')->in($dir);
foreach ($finder as $file) {
$formulae += $this->loader->load($name.':'.substr($file->getPath(), strlen($dir)).':'.$file->getBasename());
}
}
}

if (is_dir($dir = $this->kernel->getRootDir().'/views/')) {
$finder = new Finder();
$finder->files()->name('*.php')->in($dir);
foreach ($finder as $file) {
$formulae += $this->loader->load(':'.substr($file->getPath(), strlen($dir)).':'.$file->getBasename());
}
}

$this->writeCacheFile($cacheDir.'/assetic_php_templating_assets.php', '<?php return '.var_export($formulae, true).';');
}

public function isOptional()
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\AsseticBundle\CacheWarmer;

use Assetic\Extension\Twig\FormulaLoader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Component\HttpKernel\Kernel;

class TwigAssetsCacheWarmer extends CacheWarmer
{
protected $kernel;
protected $loader;

public function __construct(Kernel $kernel, FormulaLoader $loader)
{
$this->kernel = $kernel;
$this->loader = $loader;
}

public function warmUp($cacheDir)
{
$formulae = array();
foreach ($this->kernel->getBundles() as $name => $bundle) {
if (is_dir($dir = $bundle->getPath().'/Resources/views/')) {
$finder = new Finder();
$finder->files()->name('*.twig')->in($dir);
foreach ($finder as $file) {
$formulae += $this->loader->load($name.':'.substr($file->getPath(), strlen($dir)).':'.$file->getBasename());
}
}
}

if (is_dir($dir = $this->kernel->getRootDir().'/views/')) {
$finder = new Finder();
$finder->files()->name('*.twig')->in($dir);
foreach ($finder as $file) {
$formulae += $this->loader->load(':'.substr($file->getPath(), strlen($dir)).':'.$file->getBasename());
}
}

$this->writeCacheFile($cacheDir.'/assetic_twig_assets.php', '<?php return '.var_export($formulae, true).';');
}

public function isOptional()
{
return false;
}
}
56 changes: 56 additions & 0 deletions src/Symfony/Bundle/AsseticBundle/Command/DumpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\AsseticBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Dumps assets to the filesystem.
*
* @author Kris Wallsmith <[email protected]>
*/
class DumpCommand extends Command
{
protected function configure()
{
$this
->setName('assetic:dump')
->setDescription('Dumps all assets to the filesystem')
->addArgument('base_dir', InputArgument::OPTIONAL, 'The base directory')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$baseDir = $input->getArgument('base_dir')) {
$baseDir = $this->container->getParameter('assetic.document_root');
}

$am = $this->container->get('assetic.asset_manager');
foreach ($am->all() as $name => $asset) {
$output->writeln('<info>[asset]</info> '.$name);
$asset->load();

$target = $baseDir . '/' . $asset->getTargetUrl();
if (!is_dir($dir = dirname($target))) {
$output->writeln('<info>[dir+]</info> '.$dir);
mkdir($dir);
}

$output->writeln('<info>[file+]</info> '.$asset->getTargetUrl());
file_put_contents($target, $asset->dump());
}
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Bundle/AsseticBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\AsseticBundle\Controller;

use Assetic\AssetManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Serves assets.
*
* @author Kris Wallsmith <[email protected]>
*/
class Controller
{
protected $request;
protected $response;
protected $am;

public function __construct(Request $request, Response $response, AssetManager $am)
{
$this->request = $request;
$this->response = $response;
$this->am = $am;
}

public function render($name)
{
if (!$this->am->has($name)) {
throw new NotFoundHttpException('Asset Not Found');
}

$asset = $this->am->get($name);

// validate if-modified-since
if (null !== $lastModified = $asset->getLastModified()) {
$date = new \DateTime();
$date->setTimestamp($lastModified);
$this->response->setLastModified($date);

if ($this->response->isNotModified($this->request)) {
return $this->response;
}
}

$this->response->setContent($asset->dump());

return $this->response;
}
}
Loading

0 comments on commit 1292925

Please sign in to comment.