Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

User-land streams in PHP 4.3

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Apr 17, 2003 11:24 pm    Post subject: User-land streams in PHP 4.3 Reply with quote

I was wondering if anyone has looked at PHP's new user-land streams ( http://www.php.net/manual/en/ref.stream.php ). They offer several compelling advantadges:

- work with all interal PHP functions
- support user-land filters (both pre and post)
- compliant with PHP's internal resource handling (ensuring clean handling of opened resources)

I was thinking that the Smarty core might be able to be streamlined by replacing the current Smarty resource handling with a wrapper around the new streams interface. One benefit of this is that several streams are already provided by PHP (file, http, https, ftp, ftps) and others are likely to follow. Smarty could even use introspection via stream_get_wrappers to automatically generate Smarty resource wrappers for existing stream types.

Ideas?

xo boots


Last edited by boots on Thu May 01, 2003 11:40 pm; edited 1 time in total
Back to top
View user's profile Send private message
Tom Sommer
Administrator


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Fri Apr 18, 2003 1:15 am    Post subject: Reply with quote

This would raise the PHP requirement to 4.3.0
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
plockaby
Guest





PostPosted: Fri Apr 18, 2003 5:13 am    Post subject: Reply with quote

Tom Sommer wrote:
This would raise the PHP requirement to 4.3.0


Smarty currently has code in it that makes it run faster if you have php 4.2.0 or above, but if you don't have it, you can still use it. Other than code bloat, there is little reason why you can't use the faster code when it's available and the slower code when it's not.


Last edited by plockaby on Wed May 05, 2010 10:49 pm; edited 1 time in total
Back to top
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 18, 2003 11:41 am    Post subject: Re: User-land streams in PHP 4.3 Reply with quote

boots wrote:

...

I was thinking that the Smarty core might be able to be streamlined by replacing the current Smarty resource handling with a wrapper around the new streams interface. One benefit of this is that several streams are already provided by PHP (file, http, https, ftp, ftps) and others are likely to follow. Smarty could even use introspection via stream_get_wrappers to automatically generate Smarty resource wrappers for existing stream types.

...


smarty's resources are a bit more high-level than the user-space-streams are.
an important thing for smarty for example is the resource's modification-time. something like that is not provided by streams AFAIK. vice versa: smarty has no nead to seek inside a resource and fread() chunks out of it. it wants the whole template or none of it. but you have to handle such cases if you want to implement a stream_wrapper.

just my thoughts
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 20, 2003 2:32 am    Post subject: Reply with quote

Quote:
smarty's resources are a bit more high-level than the user-space-streams are.
an important thing for smarty for example is the resource's modification-time. something like that is not provided by streams AFAIK. vice versa: smarty has no nead to seek inside a resource and fread() chunks out of it. it wants the whole template or none of it. but you have to handle such cases if you want to implement a stream_wrapper.


Good points. I still think there is room here and I intend to make some experiments, though perhaps less ambitious than my original post suggested.

One thing that I think is promising is that the streams API at least provides a consistent way for smarty to automate support for PHP and user provided url-type streams. In other words, if url-type streams already exist in the userland environment, it should be possible to have smarty automatically detect and provide them without the use of custom resources.

There is still the issue of modified time and to that the stream_get_meta_data function seems to provide an opportunity for introspection of the stream. Someone even posted a stream_last_modified userland function on the english help page. http://www.php.net/manual/en/function.stream-get-meta-data.php

My guess is that as the streams interface is still young and that we will see improvements in PHP5.

I like the high-level, simple and unassuming nature of the Smarty resource interface. It is clean and easy to implement new resources with. On the otherhand, there could be some utility to providing both read and (optional) write capabilities in the interface. Having smarty being able to write could lead to interesting (though possibly impractical) uses. One such capability would be to be able to write compiled sources to something other than the filesystem.

I will post my results when I have time to experiment, hopefully not too long after the holidays.

xo boots
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 20, 2003 10:59 am    Post subject: initial results... Reply with quote

I did a bit more poking around. It looks like much of the streams API is still only in CVS and many interesting parts (like the introspection features and stream filters) are PHP5 CVS only. Unfortunately, stream wrappers look like overkill and for now I'm only looking at them because few other choices are available, even with PHP 4.3.

Stream filters can probably be used to hook data into smarty but because they are PHP5, I'll have to give them a miss for now.

I still have to give it all a better look, but in the meantime, here is a little novelty resource that delegates stream requests via PHP 4.3's get_file_contents. It has simplistic timestamp support, but does differentiate plainfile and other types of streams.

resource.stream.php
[php:1:c1d0dcb29d]<?php

/*
* Smarty plugin
* -------------------------------------------------------------
* Type: resource
* Name: stream
* Purpose: delegate to a PHP stream
* Note: requires PHP 4.3.0+
* -------------------------------------------------------------
*/
function smarty_resource_stream_source($tpl_name, &$tpl_source, &$smarty)
{
if (isset($tpl_name)) {
$tpl_source = file_get_contents($tpl_name);
return true;
}
return false;
}
function smarty_resource_stream_timestamp($tpl_name, &$tpl_source, &$smarty)
{
if (isset($tpl_name)) {
if (!($fp = fopen($tpl_name, 'r'))) {
return false;
} else {
$meta = stream_get_meta_data($fp);
if ($meta['wrapper_type'] == 'plainfile') {
$tpl_source = filemtime($tpl_name);
} else {
for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) {
if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')) {
$modtime = substr($meta['wrapper_data'][$j], 15);
break;
}
}
$tpl_source = isset($modtime) ? strtotime($modtime) : time();
}
fclose($fp);
return true;
}
}
return false;
}
function smarty_resource_stream_secure($tpl_name, &$smarty)
{
// assume all templates are secure
return true;
}
function smarty_resource_stream_trusted($tpl_name, &$smarty)
{
// not used for templates
}
?>[/php:1:c1d0dcb29d]


sample
[php:1:c1d0dcb29d]
$smarty->display('stream:http://mysite/mytemplate.tpl');
[/php:1:c1d0dcb29d]

I only tested http, but the other stream types (https, ftp, ftps) should conceivably work. On my Win2k box, paths like 'stream:c:/tpldir/mytemplate.tpl' also seem to work. I know that's trivial, though Embarassed.

I haven't bothered to test its efficiency Wink.


Last edited by boots on Thu Nov 27, 2003 9:15 am; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 02, 2003 12:55 am    Post subject: Reply with quote

I haven't yet tested if it affects the resource plugin I posted (shouldn't for non-userland streams), but after more tests, I found some issues with user-land streams, at least in 4.3 on Win2k.

The drop-dead is that they leak memory, Apache will eventually fault. Plus they are very slow--I think that PHP's memory and function calling mechanisms are to blame and if so, it won't get better until they get better. And its not great. Including a file 100x from the filesystem vs a global variable user-land stream is about 5x faster. Strangely, running the var stream test immediately followed by the filesystem test causes the filesystem include performance to slow about 4x. Not nice. Those tests aren't even with a PHP cache (nb: user-land streams compare even worse with a PHP cache).

So that's it for now.

EDIT: Come to think of it, it could be the socket interface...
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP