|
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\Controller; |
|
|
13 |
|
|
|
14 |
use Assetic\Asset\AssetCache; |
|
|
15 |
use Assetic\Asset\AssetInterface; |
|
|
16 |
use Assetic\Factory\LazyAssetManager; |
|
|
17 |
use Assetic\Cache\CacheInterface; |
|
|
18 |
use Symfony\Component\HttpFoundation\Request; |
|
|
19 |
use Symfony\Component\HttpFoundation\Response; |
|
|
20 |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* Serves assets. |
|
|
24 |
* |
|
|
25 |
* @author Kris Wallsmith <kris@symfony.com> |
|
|
26 |
*/ |
|
|
27 |
class AsseticController |
|
|
28 |
{ |
|
|
29 |
protected $request; |
|
|
30 |
protected $am; |
|
|
31 |
protected $cache; |
|
|
32 |
|
|
|
33 |
public function __construct(Request $request, LazyAssetManager $am, CacheInterface $cache) |
|
|
34 |
{ |
|
|
35 |
$this->request = $request; |
|
|
36 |
$this->am = $am; |
|
|
37 |
$this->cache = $cache; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
public function render($name, $pos = null) |
|
|
41 |
{ |
|
|
42 |
if (!$this->am->has($name)) { |
|
|
43 |
throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name)); |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
$asset = $this->am->get($name); |
|
|
47 |
if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) { |
|
|
48 |
throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos)); |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
$response = $this->createResponse(); |
|
|
52 |
|
|
|
53 |
// last-modified |
|
|
54 |
if (null !== $lastModified = $asset->getLastModified()) { |
|
|
55 |
$date = new \DateTime(); |
|
|
56 |
$date->setTimestamp($lastModified); |
|
|
57 |
$response->setLastModified($date); |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
// etag |
|
|
61 |
if ($this->am->hasFormula($name)) { |
|
|
62 |
$formula = $this->am->getFormula($name); |
|
|
63 |
$formula['last_modified'] = $lastModified; |
|
|
64 |
$response->setETag(md5(serialize($formula))); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
if ($response->isNotModified($this->request)) { |
|
|
68 |
return $response; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
$response->setContent($this->cachifyAsset($asset)->dump()); |
|
|
72 |
|
|
|
73 |
return $response; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
protected function createResponse() |
|
|
77 |
{ |
|
|
78 |
return new Response(); |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
protected function cachifyAsset(AssetInterface $asset) |
|
|
82 |
{ |
|
|
83 |
return new AssetCache($asset, $this->cache); |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
private function findAssetLeaf(\Traversable $asset, $pos) |
|
|
87 |
{ |
|
|
88 |
$i = 0; |
|
|
89 |
foreach ($asset as $leaf) { |
|
|
90 |
if ($pos == $i++) { |
|
|
91 |
return $leaf; |
|
|
92 |
} |
|
|
93 |
} |
|
|
94 |
} |
|
|
95 |
} |