forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AsseticBundle] initial entry of assetic integration
- Loading branch information
1 parent
b5972f3
commit 1292925
Showing
52 changed files
with
1,952 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Symfony/Bundle/AsseticBundle/CacheWarmer/AssetWriterCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Bundle/AsseticBundle/CacheWarmer/PhpTemplatingAssetsCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Bundle/AsseticBundle/CacheWarmer/TwigAssetsCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
src/Symfony/Bundle/AsseticBundle/Controller/Controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.