smarty_internal_resource_stream.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource Stream
  4. *
  5. * Implements the streams as resource for Smarty template
  6. *
  7. * @package Smarty
  8. * @subpackage TemplateResources
  9. * @author Uwe Tews
  10. * @author Rodney Rehm
  11. */
  12. /**
  13. * Smarty Internal Plugin Resource Stream
  14. *
  15. * Implements the streams as resource for Smarty template
  16. *
  17. * @link http://php.net/streams
  18. * @package Smarty
  19. * @subpackage TemplateResources
  20. */
  21. class Smarty_Internal_Resource_Stream extends Smarty_Resource_Recompiled {
  22. /**
  23. * populate Source Object with meta data from Resource
  24. *
  25. * @param Smarty_Template_Source $source source object
  26. * @param Smarty_Internal_Template $_template template object
  27. * @return void
  28. */
  29. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
  30. {
  31. $source->filepath = str_replace(':', '://', $source->resource);
  32. $source->uid = false;
  33. $source->content = $this->getContent($source);
  34. $source->timestamp = false;
  35. $source->exists = !!$source->content;
  36. }
  37. /**
  38. * Load template's source from stream into current template object
  39. *
  40. * @param Smarty_Template_Source $source source object
  41. * @return string template source
  42. * @throws SmartyException if source cannot be loaded
  43. */
  44. public function getContent(Smarty_Template_Source $source)
  45. {
  46. $t = '';
  47. // the availability of the stream has already been checked in Smarty_Resource::fetch()
  48. $fp = fopen($source->filepath, 'r+');
  49. if ($fp) {
  50. while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
  51. $t .= $current_line;
  52. }
  53. fclose($fp);
  54. return $t;
  55. } else {
  56. return false;
  57. }
  58. }
  59. /**
  60. * modify resource_name according to resource handlers specifications
  61. *
  62. * @param Smarty $smarty Smarty instance
  63. * @param string $resource_name resource_name to make unique
  64. * @return string unique resource name
  65. */
  66. protected function buildUniqueResourceName(Smarty $smarty, $resource_name)
  67. {
  68. return get_class($this) . '#' . $resource_name;
  69. }
  70. }
  71. ?>