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