TemplateLoader.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. class PHPParser_TemplateLoader
  3. {
  4. protected $parser;
  5. protected $baseDir;
  6. protected $suffix;
  7. /**
  8. * Constructs a filesystem template loader.
  9. *
  10. * The templates are loaded from {baseDir}/{name}{suffix}.
  11. *
  12. * @param PHPParser_Parser $parser A PHP parser instance
  13. * @param string $baseDir The base directory to load templates from
  14. * @param string $suffix An optional suffix to append after the template name
  15. */
  16. public function __construct(PHPParser_Parser $parser, $baseDir, $suffix = '') {
  17. if (!is_dir($baseDir)) {
  18. throw new InvalidArgumentException(
  19. sprintf('The specified base directory "%s" does not exist', $baseDir)
  20. );
  21. }
  22. $this->parser = $parser;
  23. $this->baseDir = $baseDir;
  24. $this->suffix = $suffix;
  25. }
  26. /**
  27. * Loads the template with the specified name.
  28. *
  29. * @param string $name The name of template
  30. *
  31. * @return PHPParser_Template The loaded template
  32. */
  33. public function load($name) {
  34. $file = $this->baseDir . '/' . $name . $this->suffix;
  35. if (!is_file($file)) {
  36. throw new InvalidArgumentException(
  37. sprintf('The file "%s" does not exist', $file)
  38. );
  39. }
  40. return new PHPParser_Template($this->parser, file_get_contents($file));
  41. }
  42. }