PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/AsseticBundle/Factory/Resource/FileResource.php

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