AsseticLoader.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\Routing;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Factory\LazyAssetManager;
  13. use Symfony\Bundle\AsseticBundle\Config\AsseticResource;
  14. use Symfony\Component\Config\Loader\Loader;
  15. use Symfony\Component\Config\Resource\FileResource;
  16. use Symfony\Component\Routing\Route;
  17. use Symfony\Component\Routing\RouteCollection;
  18. /**
  19. * Loads routes for all assets.
  20. *
  21. * Assets should only be served through the routing system for ease-of-use
  22. * during development.
  23. *
  24. * For example, add the following to your application's routing_dev.yml:
  25. *
  26. * _assetic:
  27. * resource: .
  28. * type: assetic
  29. *
  30. * In a production environment you should use the `assetic:dump` command to
  31. * create static asset files.
  32. *
  33. * @author Kris Wallsmith <kris@symfony.com>
  34. */
  35. class AsseticLoader extends Loader
  36. {
  37. protected $am;
  38. public function __construct(LazyAssetManager $am)
  39. {
  40. $this->am = $am;
  41. }
  42. public function load($routingResource, $type = null)
  43. {
  44. $routes = new RouteCollection();
  45. // resources
  46. foreach ($this->am->getResources() as $resources) {
  47. if (!$resources instanceof \Traversable) {
  48. $resources = array($resources);
  49. }
  50. foreach ($resources as $resource) {
  51. $routes->addResource(new AsseticResource($resource));
  52. }
  53. }
  54. // routes
  55. foreach ($this->am->getNames() as $name) {
  56. $asset = $this->am->get($name);
  57. $formula = $this->am->getFormula($name);
  58. $this->loadRouteForAsset($routes, $asset, $name);
  59. $debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug();
  60. $combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : !$debug;
  61. // add a route for each "leaf" in debug mode
  62. if (!$combine) {
  63. $i = 0;
  64. foreach ($asset as $leaf) {
  65. $this->loadRouteForAsset($routes, $leaf, $name, $i++);
  66. }
  67. }
  68. }
  69. return $routes;
  70. }
  71. /**
  72. * Loads a route to serve an supplied asset.
  73. *
  74. * The fake front controller that {@link UseControllerWorker} adds to the
  75. * target URL will be removed before set as a route pattern.
  76. *
  77. * @param RouteCollection $routes The route collection
  78. * @param AssetInterface $asset The asset
  79. * @param string $name The name to use
  80. * @param integer $pos The leaf index
  81. */
  82. private function loadRouteForAsset(RouteCollection $routes, AssetInterface $asset, $name, $pos = null)
  83. {
  84. $defaults = array(
  85. '_controller' => 'assetic.controller:render',
  86. 'name' => $name,
  87. 'pos' => $pos,
  88. );
  89. // remove the fake front controller
  90. $pattern = str_replace('_controller/', '', $asset->getTargetPath());
  91. if ($format = pathinfo($pattern, PATHINFO_EXTENSION)) {
  92. $defaults['_format'] = $format;
  93. }
  94. $route = '_assetic_'.$name;
  95. if (null !== $pos) {
  96. $route .= '_'.$pos;
  97. }
  98. $routes->add($route, new Route($pattern, $defaults));
  99. }
  100. public function supports($resource, $type = null)
  101. {
  102. return 'assetic' == $type;
  103. }
  104. }