AsseticLoader.php 3.4 KB

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