-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsseticControllerSubstitute.php
More file actions
45 lines (32 loc) · 1.2 KB
/
AsseticControllerSubstitute.php
File metadata and controls
45 lines (32 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Kyoushu\FoundationBundle;
use Assetic\Factory\LazyAssetManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AsseticControllerSubstitute{
private $am;
public function __construct(LazyAssetManager $am){
$this->am = $am;
}
public function render($name, $pos = null){
if (!$this->am->has($name)) {
throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
}
$asset = $this->am->get($name);
if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
}
$response = new Response();
$response->setExpires(new \DateTime());
$response->setContent( $asset->dump() );
return $response;
}
private function findAssetLeaf(\Traversable $asset, $pos){
$i = 0;
foreach ($asset as $leaf) {
if ($pos == $i++) {
return $leaf;
}
}
}
}