|
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\Factory\Resource; |
|
|
13 |
|
|
|
14 |
use Assetic\Factory\Resource\ResourceInterface; |
|
|
15 |
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
|
|
16 |
use Symfony\Component\Templating\Loader\LoaderInterface; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* A file resource. |
|
|
20 |
* |
|
|
21 |
* @author Kris Wallsmith <kris@symfony.com> |
|
|
22 |
*/ |
|
|
23 |
class FileResource implements ResourceInterface |
|
|
24 |
{ |
|
|
25 |
protected $loader; |
|
|
26 |
protected $bundle; |
|
|
27 |
protected $baseDir; |
|
|
28 |
protected $path; |
|
|
29 |
protected $template; |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* Constructor. |
|
|
33 |
* |
|
|
34 |
* @param LoaderInterface $loader The templating loader |
|
|
35 |
* @param string $bundle The current bundle name |
|
|
36 |
* @param string $baseDir The directory |
|
|
37 |
* @param string $path The file path |
|
|
38 |
*/ |
|
|
39 |
public function __construct(LoaderInterface $loader, $bundle, $baseDir, $path) |
|
|
40 |
{ |
|
|
41 |
$this->loader = $loader; |
|
|
42 |
$this->bundle = $bundle; |
|
|
43 |
$this->baseDir = $baseDir; |
|
|
44 |
$this->path = $path; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
public function isFresh($timestamp) |
|
|
48 |
{ |
|
|
49 |
return $this->loader->isFresh($this->getTemplate(), $timestamp); |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
public function getContent() |
|
|
53 |
{ |
|
|
54 |
return $this->loader->load($this->getTemplate())->getContent(); |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
public function __toString() |
|
|
58 |
{ |
|
|
59 |
return (string) $this->getTemplate(); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
protected function getTemplate() |
|
|
63 |
{ |
|
|
64 |
if (null === $this->template) { |
|
|
65 |
$this->template = self::createTemplateReference($this->bundle, substr($this->path, strlen($this->baseDir))); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
return $this->template; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
static private function createTemplateReference($bundle, $file) |
|
|
72 |
{ |
|
|
73 |
$parts = explode('/', strtr($file, '\\', '/')); |
|
|
74 |
$elements = explode('.', array_pop($parts)); |
|
|
75 |
|
|
|
76 |
return new TemplateReference($bundle, implode('/', $parts), $elements[0], $elements[1], $elements[2]); |
|
|
77 |
} |
|
|
78 |
} |