Friday afternoon toying: eZ Components as phar
PHP 5.3 will have a new cool feature: phar . A phar is to PHP what a jar is to Java. I spent a little time to see how easy it would be to make our latest eZ Components release into a workable phar.
First of all, a phar can be build from a directory structure with a few functions only:
<?php
$phar = new Phar(
'ezcomponents-2008.1.phar', 0,
'ezcomponents-2008.1.phar' );
$phar->buildFromDirectory(
dirname(__FILE__) . '/ezcomponents-2008.1',
'/\.php$/');
$phar->compressFiles( Phar::GZ );
$phar->stopBuffering();
?>
This build script will create a phar from the directory contents in "ezcomponents-2008.1", but only include the PHP files (See http://php.net/phar.buildfromdirectory). We compress all the files and with stopBuffering we write the file to disk. With the following code, we can now use the phar in our application:
require 'ezcomponents-2008.1.phar';
It is also possible to run a bit of code when including the phar. You do this, by adding a stub to the phar. To do so, we include the following code just before the compressFiles() call:
$stub = <<<ENDSTUB <?php Phar::mapPhar( 'ezcomponents-2008.1.phar' ); require 'phar://ezcomponents-2008.1.phar/Base/src/base.php'; spl_autoload_register( array( 'ezcBase', 'autoload' ) ); __HALT_COMPILER(); ENDSTUB; $phar->setStub( $stub );
After we re-create the phar with "php -dphar.readonly=0 build.php". The new phar once required will now setup the autoload mechanism of the eZ Components. The following script demonstrates that it actually works:
<?php
require 'ezcomponents-2008.1.phar';
$f = ezcFeed::parse( 'http://derickrethans.nl/rss.xml' );
foreach ( $f->item as $item )
{
echo $item->title, "\n";
}
?>
Conclusion: phar is cool!
Comments
As phar as I can see: Quite cool! :)
Inspired by your experiment, I tried Phar on PHPUnit: http://phpfi.com/327410
Good stuff :-)
Nice inspiration, works for PHP_Depend like a charm :-)
Are there any speed penalties?
This is interesting, and I'm betting is has a number of great uses. However this does add overhead. No matter how impressive the benchmarks are there will be overhead. If there is none I would be amazed, and would have to look into that closely.
I need every single clock cycle on my servers to serve requests as quickly as I can using as few resources as I can. So this is not for me. Plus, I'm not looking for easy of installation, since I don't have a problem unzipping or untarring a file.
But now we have one more custom archive format. And like jar it can be executed directly. I always thought jar(aka zip) was created because disk driver were $1,000 per 1GB at the time. But, anyways I'm not trying to save disk space, since PHP uses next to nothing. I'm not distributing to outside customers. And I need speed, not file system abstraction. Does this really come down to an executable "diskdoubler"?
Clearly this is not for my situation.
Where is it good? Distributing applications to people who have trouble with unzip or tar or WinZip for that matter. I guess and install programmer would have solved that, but no matter. This person would also not need to be concerned about performance, at least not on the same level I am. And they wouldn't mind the tiny bit of abstraction from file system.
Does that mean it's good for the small timers with not much traffic who cannot install some PHP files? Or is it a time saver, at the expense of performance?
Sum:
1 step forward, and 2 steps back?
Or
If it ain't broke, fix it 'til is is?
Or
Java-ism?
It's a dandy util, who's place in the fast paced web services world is yet to be determined.
@James, take a look at http://marc.info/?l=php-internals&m=121394601217048&w=2
Actually there may benefit as there are much less file input/output operations within a library.
I've tried something different, dynamically saturating a Phar inside the autoload function. After a few requests, all autoload attempts will be directly "answered" by the phar file. I've not benchmarked that yet, but the subjective speed improvement is tremendous since the number of i/o calls is reduced next to nothing.
I've tested Phar on my framework. Memory usage was greater when using the phar'ed version, and the performance difference was negligible... hopefully PHP 5.4 brings some enhancements to Phar.
Life Line
I've finished reading Children of Memory, the third book in the series.
Another interesting take on forms of intelligent life.
A fourth one is going to get released later this year.
Updated a post_box, a beauty shop, and a restaurant; Confirmed 2 clothes shops, 2 pet shops, and a restaurant
I walked 5.9km in 1h40m39s
Updated a bicycle_parking
Updated 2 waste_baskets
I walked 7.9km in 1h37m12s
Created 3 waste_baskets; Updated 3 bus_stops, 2 benches, and 2 waste_baskets
I walked 8.1km in 1h25m53s
I walked 1.2km in 9m31s
I walked 9.4km in 1h39m05s
Merge branch 'xdebug_3_5'
Merged pull request #1071
Fixed issue #2411: Native Path Mapping is not applied to the initial …
Created 2 waste_baskets; Updated 3 waste_baskets, 2 benches, and 2 other objects; Deleted a waste_basket
I walked 7.9km in 1h45m36s
RE: https://phpc.social/@phpc_tv/116274041642323081
Now that phpc.tv and phpc.social are part of the same umbrella, I've upped my yearly contributions to their Open Collective: https://opencollective.com/phpcommunity/projects/phpc-social
Merge branch 'xdebug_3_5'
Merged pull request #1070
I walked 7.2km in 1h10m26s
Fixed issue #2405: Handle minimum path in .xdebug directory discovery
I've published a new blog post: "Human Creations", on the difference in content generation by LLMs, and the creation of text, art and code by humans.
You can find it at https://derickrethans.nl/human-creations.html or at @blog
I walked 7.8km in 1h38m32s
RE: https://phpc.social/@afilina/116274024588235234
It's good to see that more and more people are realising that the Web can be for-good, without all the enshittification.
That's why I'm happy to see endeavours like phpc.tv springing up, and helping out where I can.
Taking back the control of how the Web is for people, by people, without big tech making it all shit.
Created a waste_basket; Updated 5 crossings and a bicycle_parking
I walked 10.7km in 2h35m10s


Shortlink
This article has a short URL available: https://drck.me/fatecap-6dl