source: trunk/include/smarty/libs/sysplugins/smarty_internal_resource_stream.php @ 23384

Last change on this file since 23384 was 23384, checked in by rvelices, 11 years ago

smarty 3 - first pass for tests

File size: 2.3 KB
Line 
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/**
14* Smarty Internal Plugin Resource Stream
15*
16* Implements the streams as resource for Smarty template
17*
18* @link http://php.net/streams
19* @package Smarty
20* @subpackage TemplateResources
21*/
22class Smarty_Internal_Resource_Stream extends Smarty_Resource_Recompiled {
23
24    /**
25    * populate Source Object with meta data from Resource
26    *
27    * @param Smarty_Template_Source   $source    source object
28    * @param Smarty_Internal_Template $_template template object
29    * @return void
30    */
31    public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
32    {
33        if(strpos($source->resource, '://') !== false) {
34            $source->filepath = $source->resource;
35        } else {
36            $source->filepath = str_replace(':', '://', $source->resource);
37        }
38        $source->uid = false;
39        $source->content = $this->getContent($source);
40        $source->timestamp = false;
41        $source->exists = !!$source->content;
42    }
43
44    /**
45    * Load template's source from stream into current template object
46    *
47    * @param Smarty_Template_Source $source source object
48    * @return string template source
49    * @throws SmartyException if source cannot be loaded
50    */
51    public function getContent(Smarty_Template_Source $source)
52    {
53        $t = '';
54        // the availability of the stream has already been checked in Smarty_Resource::fetch()
55        $fp = fopen($source->filepath, 'r+');
56        if ($fp) {
57            while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
58                $t .= $current_line;
59            }
60            fclose($fp);
61            return $t;
62        } else {
63            return false;
64        }
65    }
66
67    /**
68    * modify resource_name according to resource handlers specifications
69    *
70    * @param Smarty $smarty        Smarty instance
71    * @param string $resource_name resource_name to make unique
72    * @return string unique resource name
73    */
74    protected function buildUniqueResourceName(Smarty $smarty, $resource_name)
75    {
76        return get_class($this) . '#' . $resource_name;
77    }
78}
Note: See TracBrowser for help on using the repository browser.