John's Top PECL Picks

1,812 views

Published on

A presentation I gave at PHP Quebec, 2006 on some of the less known and very powerful PECL extensions available for PHP

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
1,812
On SlideShare
0
From Embeds
0
Number of Embeds
48
Actions
Shares
0
Downloads
0
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

John's Top PECL Picks

  1. 1. John’s Top PECL Picks John Coggeshall
  2. 2. Welcome to the session <ul><li>Welcome to the session! </li></ul><ul><li>Who am I: </li></ul><ul><ul><li>John Coggeshall </li></ul></ul><ul><ul><li>Lead, North American Professional Services </li></ul></ul><ul><ul><li>PHP 5 Core Contributor </li></ul></ul><ul><ul><li>Author: PHP 5 Unleashed </li></ul></ul><ul><ul><li>Member of Zend’s Education Advisory Board </li></ul></ul>May 28, 2009 #
  3. 3. What is PECL? <ul><li>PECL: PHP Extension Community Library </li></ul><ul><ul><li>A collection of C level PHP extensions for PHP </li></ul></ul><ul><ul><li>A spin off from PEAR </li></ul></ul><ul><ul><ul><li>PHP Extension and Application Repository </li></ul></ul></ul>
  4. 4. What is PECL? <ul><li>Historically, the PHP community looked to PECL as a way to manage the release process for extensions </li></ul><ul><ul><li>Allow each extension developer to maintain their extension outside of the core PHP </li></ul></ul><ul><ul><li>Marginally successful in that regard </li></ul></ul><ul><li>Practically, PECL is a collection of PHP extensions which do not have consensus in the core distribution </li></ul><ul><ul><li>Less oversight into code quality / completeness </li></ul></ul><ul><ul><li>More like Perl’s CPAN </li></ul></ul>
  5. 5. Using PECL extensions <ul><li>Using PECL extensions is fairly straightforward </li></ul><ul><ul><li>A few options are available </li></ul></ul><ul><li>Option 1: Use the pecl tool </li></ul><ul><ul><li>$ pecl install fileinfo </li></ul></ul><ul><ul><li>Downloads the extension, configure and compiles it for use on your architecture </li></ul></ul><ul><ul><li>Not always available </li></ul></ul><ul><li>Option 2: Compile it yourself </li></ul><ul><ul><li>A more advanced approach </li></ul></ul><ul><ul><li>Complicates acquiring / installing the extension </li></ul></ul><ul><ul><li>Should work in almost every case </li></ul></ul>
  6. 6. Compiling PECL from source <ul><li>When the PECL tool is unavailable, you can install PECL extensions by.. </li></ul><ul><ul><li>Downloading the extension’s latest release </li></ul></ul><ul><ul><li>Extracting the tarball </li></ul></ul><ul><ul><li>Running phpize </li></ul></ul><ul><ul><ul><li>Creates a configuration script just for this extension </li></ul></ul></ul><ul><ul><li>Compiling the extension </li></ul></ul><ul><ul><ul><li>Creates a shared library </li></ul></ul></ul><ul><ul><li>Enabling using the extension php.ini directive </li></ul></ul><ul><ul><ul><li>i.e. extension=fileinfo.so </li></ul></ul></ul>
  7. 7. Compiling PECL from source <ul><li>Compiling example: </li></ul>$ tar –zxvf mypeclext.tgz $ cd mypeclext/ $ phpize $ ./configure $ make $ make install
  8. 8. What about Windows? <ul><li>You can use PECL in PHP installations running on Windows as well </li></ul><ul><ul><li>Actually, it’s much easier </li></ul></ul><ul><li>Just download the extension from the PHP web site (pre-compiled) </li></ul><ul><ul><li>pecl4win.php.net </li></ul></ul><ul><li>Once downloaded just copy to your extensions directory and enable from php.ini </li></ul>
  9. 9. My Favorite PECL Extensions
  10. 10. About my picks <ul><li>My PECL Picks are based on a number of criteria, which may or may not agree with yours </li></ul><ul><ul><li>Direct experience using them </li></ul></ul><ul><ul><li>Quality of code / Trusted Developers </li></ul></ul><ul><ul><li>Interesting Emerging Technologies </li></ul></ul><ul><li>For the most part my selections have to do with data manipulation </li></ul><ul><ul><li>Everyone knows about compiler caches and debuggers already </li></ul></ul>
  11. 11. Fileinfo Extension <ul><li>Very often when uploading files you want to verify what is being uploaded </li></ul><ul><ul><li>Verifying extension isn’t enough </li></ul></ul><ul><ul><li>Most browsers lie about mime type based on the file extension </li></ul></ul><ul><li>Fileinfo to the rescue </li></ul><ul><ul><li>Detect MIME types for files based on their content </li></ul></ul><ul><ul><li>Uses a “magic” database for determining the type </li></ul></ul>
  12. 12. Fileinfo Extension <ul><li>Using Fileinfo is straightforward: </li></ul>$info = new finfo(FILEINFO_MIME); echo “The Mime Type is: “ . $info->file(‘/tmp/myfile.mpg’); <ul><li>If your “magic” library isn’t in the standard directory you can pass the path as the second constructor parameter: </li></ul>$info = new finfo(FILEINFO_MIME, “/path/to/file/magic”);
  13. 13. Phar Extension <ul><li>What’s Phar? </li></ul><ul><ul><li>PHP Archive Files </li></ul></ul><ul><ul><li>Similar to Java .JAR files </li></ul></ul><ul><li>Effectively a virtual file system tailored to PHP </li></ul><ul><li>Based on the PEAR PHP implementation and specification </li></ul><ul><li>Can store any type of resource you might need for your PHP applications </li></ul>
  14. 14. Creating Phars <ul><li>Once you have the Phar extension installed, the first order of business is creating a new Phar file </li></ul><ul><li>Two ways to create Phars </li></ul><ul><ul><li>Using streams: </li></ul></ul>file_put_contents(‘phar:///full/path/to/application.phar/file.php’, ‘<?php print “Hello World!”; ?>’); <ul><ul><li>Or by using a Phar object directly: </li></ul></ul>$phar = new Phar(‘/full/path/to/application.phar’, 0, ‘ application.phar’); $phar[‘file.php’] = ‘<?php print “Hello World!”; ?>’;
  15. 15. Creating Phars <ul><li>When creating Phar files using the object approach a number of tools are available </li></ul><ul><ul><li>Compression on a per-resource basis </li></ul></ul>$phar[‘realbigfile.data’]->setCompressedGZ(); <ul><ul><li>You can also assign meta-data to any resource in the archive, as well as the archive itself… </li></ul></ul>$phar[‘images/myimage.png’]->setMetaData(array(‘mime’ => ‘image/png’)); $phar->setMetadata(array(‘bootstrap’ => ‘index.php’));
  16. 16. Using Phar files <ul><li>Using Phar files is identical to using normal PHP scripts in many ways </li></ul><ul><ul><li>You can simply include a .phar file: </li></ul></ul>Include_once ‘library.phar’; <ul><li>You can also use stream-access to load specific resources from the Phar: </li></ul>Include_once ‘phar://library.phar/myfiles/file.php’; <ul><li>Note: Phar archives cannot work against remote resources </li></ul>
  17. 17. Using Phar files <ul><li>When a Phar file is opened, you can use the object as an array to manipulate the archive: </li></ul><?php $phar = new Phar(‘/path/to/application.phar’, 0, ‘myapp.phar’); // Get a PharFileInfo instance // uses phar://myapp.phar/file.php $phar_info = $phar[‘file.php’]; // Create a new file (or overwrite) called file.php with the contents $contents $phar[‘file.php’] = $contents; // Check to see if a file exists within a phar If(isset($phar[‘file.php’]))… // Erase a file from the phar unset($phar[‘file.php’]);
  18. 18. Using Phar files <ul><li>Phars can be executed directly creating a bootstrap file.. </li></ul>$phar->setMetadata(array(‘bootstrap’ => ‘index.php’)); [/home/john]$ php application.phar <?php /* index.php */ $p = new Phar(__FILE__); $m = $p->getMetaData(); require &quot;phar://&quot; . __FILE__ . &quot;/&quot; . $m[&quot;bootstrap&quot;]; __HALT_COMPILER(); ?>
  19. 19. XDiff <ul><li>XDiff is a very useful extension for working with different versions of a file </li></ul><ul><ul><li>Very similar to the UNIX diff command </li></ul></ul><ul><ul><li>Can be used to determine the differences between two versions of the same file </li></ul></ul><ul><ul><li>Create Patches from one file to the next </li></ul></ul><ul><ul><li>Can be combined with the likes of fileinfo/phar to create robust package management and upgrading </li></ul></ul>
  20. 20. Using XDiff <ul><li>Creating Diffs is easy </li></ul><?php xdiff_file_diff(“input1.txt”, “input2.txt”, “output.txt”); <ul><li>Which produces an output.txt with… </li></ul>@@ -1,1 +1,1 @@ -Hello +Hello, World! <ul><li>Each parameter is a stream, and xdiff_string_diff() is also available </li></ul>
  21. 21. Patching using XDiff <ul><li>Just like normal UNIX diff, the output created can be used to apply changes to the original file </li></ul>@@ -1,1 +1,1 @@ -Hello +Hello, World! <?php $patch = file_get_contents(‘mypatch.diff’); $original = file_get_contents(‘myfile.txt’); $patched = xdiff_string_patch($original, $patch); file_put_contents(‘myfilepatched.txt’, $patched);
  22. 22. More XDiff Goodies <ul><li>It’s worthwhile to mention you can create diffs on binary data as easily as text </li></ul><ul><ul><li>xdiff_file_diff_binary() </li></ul></ul><ul><ul><li>xdiff_file_patch_binary() </li></ul></ul><ul><li>Using the in-memory diffs you can store incremental changes on just about anything </li></ul><ul><ul><li>Store database schema changes, or store diffs in the database </li></ul></ul><ul><ul><li>Combine with Phar to create update / rollback packages </li></ul></ul>
  23. 23. Zip extension <ul><li>The PECL Zip extension is a replacement for the zip extension previously shipped with PHP </li></ul><ul><ul><li>Much improved from a functional and usability standpoint </li></ul></ul><ul><ul><li>Allows for the easy creation, and reading of Zip archive files </li></ul></ul><ul><ul><li>Object oriented interface </li></ul></ul><ul><ul><li>(Very) Actively maintained </li></ul></ul>
  24. 24. Using Zip: Creating Archives <ul><li>You can use Zip to create archives easily </li></ul><ul><ul><li>ZipArchive::addFile() to add files </li></ul></ul><ul><ul><li>ZipArchive::addFromString() to add from PHP variables </li></ul></ul><?php $zip = new ZipArchive(); $zip->open(‘myarchive.zip’, ZIPARCHIVE::CREATE); $zip->addFile(‘/path/to/myfile.dat’, ‘newname.dat’); $zip->addFromString(‘myfile.txt’, ‘This is my file…’); print “Total files: “ . $zip->numFiles; $zip->close(); ?>
  25. 25. Using Zip: Reading Archives <ul><li>You can read archives using the Zip extension in a few ways </li></ul><ul><ul><li>Using Streams </li></ul></ul><ul><ul><li>Using the API </li></ul></ul><ul><li>Stream example: </li></ul>$uncompressed = file_get_contents(‘zip://myarchive.zip#myfile.txt’);
  26. 26. Using Zip: Reading Archives <ul><li>You can also use the API of the ZipArchive class to extract files as well </li></ul><?php $zip = new ZipArchive(); $zip->open(‘myarchive.zip’); $zip->extractTo(‘mydirectory’); $zip->close(); <?php $zip = new ZipArchive(); $zip->open(‘myarchive.zip’); $zip->extractTo(‘.’, array(‘file1.txt’, ‘file2.txt’)); $zip->close();
  27. 27. SSH2 Extension <ul><li>The SSH2 extension is a rather useful little tool that allows you to make connections to servers using the SSH transport </li></ul><ul><ul><li>PHP-controlled SSH shells </li></ul></ul><ul><ul><li>Using SFTP/SCP to transmit files back and forth between servers </li></ul></ul><ul><ul><li>Much more! </li></ul></ul>
  28. 28. Using SSH2 <ul><li>Using SSH2 in its basic forms is a fairly straightforward process </li></ul><?php $connect = ssh2_connect(‘coggeshall.org’, 22); ssh2_auth_password($connect, ‘username’, ‘password’); $result = ssh2_exec($connect, ‘/usr/local/bin/php –i’); while(!feof($result)) { print fgets($result, 4096); } ?> <ul><li>Note that $result is a stream which can be read from using any stream access in PHP </li></ul>
  29. 29. Using SSH2: Secure Copy <ul><li>One of the most useful bits of the ssh2 extension is the ability to secure copy files from PHP </li></ul><ul><li>Very useful when transferring sensitive files to servers at run time </li></ul><?php $connect = ssh2_connect(‘coggeshall.org’, 22); ssh2_auth_password($connect, ‘username’, ‘password’); // Sending a file ssh2_scp_send($connect, ‘/myfile.txt’, ‘/remotefile.txt’, 0755); // Copying a file from a remote location ssh2_scp_recv($connect, ‘/remotefile.txt’, ‘localfile.txt’); ?>
  30. 30. Some final thoughts <ul><li>So, for now those are my PECL picks </li></ul><ul><ul><li>I’m sure they will change over time! </li></ul></ul><ul><li>The important thing isn’t which extensions from PECL you use, but more you know to use them! </li></ul><ul><ul><li>Many people never even heard of PECL before </li></ul></ul><ul><li>You have to be careful though, some extensions aren’t ready for prime time </li></ul>
  31. 31. Tips on determining readiness <ul><li>When looking through PECL packages, how do you know something is of decent quality? </li></ul><ul><ul><li>Many previously core extensions have been moved to PECL verbaitum </li></ul></ul><ul><ul><ul><li>Ext/dio for example </li></ul></ul></ul><ul><ul><li>Look for core PHP devs as maintainers </li></ul></ul><ul><ul><ul><li>Most of these extensions will, even in beta, be quality code </li></ul></ul></ul><ul><ul><li>Look for actively maintained extensions </li></ul></ul><ul><ul><ul><li>If it hasn’t left beta in two years, or only has one release, be careful </li></ul></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><ul><ul><li>Extensions that are documented are likely working as-advertised </li></ul></ul></ul><ul><ul><li>PHP Bugs </li></ul></ul><ul><ul><ul><li>Check to see how many outstanding bugs there are for the extension </li></ul></ul></ul><ul><ul><li>Google! </li></ul></ul><ul><ul><ul><li>See if anyone else has been using it with any success </li></ul></ul></ul>
  32. 32. Questions? Thank you!

×