Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • CWJ 09
« Lessons in Phar
SQLYog – A superficial review »

Packaging Zend Framework As A Phar Revisited

Dear Reader,

Ok, after I posted my original post on packaging the Zend Framework with Phar and my package.php, I had some additional thoughts. Also, reader John Douglass sent in some code to include. John, I’ve included most of your concepts but I changed up a lot of your code. Nothing personal but since we are using PHP 5.3, we might as well use the GetOpt() function.

So here is the latest version. probably my last version of this particular piece of code. I still believe that a standardized phar packager could be written, possibly using phing but I don’t have time to do it right now. If you get around to it, make sure you drop me a line so I can link to it.

There is no new functionality here, we just added a few cli options so things aren’t hard coded. Also, since this is a cli application, I added a help section. If you don’t do things right, it will tell you what it’s expecting. To see the help, just run the program without any parameters.

Thanks John for your ideas and for sending in code.

Until next time,
(l)(k)(bunny)
=C=

< ?php
/**
* package.php
* Create a Zend Framework phar
*
* @author Cal Evans <cal@calevans.com>
* @author John Douglass <john .douglass@oit.gatech.edu>
*/
 
$getOptLongArray = array("stub:");
$getOptParams    = "s:p:v";
$options         = getOpt($getOptParams,$getOptLongArray);
 
if(!isset($options['s'],$options['p']))
{
    echo "You did not specify either a path or a phar file name.\n";
    displayHelp();
    die(1);
}
 
/*
 * Set up our environment
 */
$sourceLocation = $options['s'];
$basePointer    = strpos($options['s'],'Zend');
$pharFile       = $options['p'];
 
/*
 * Make sure things are sane before progressing
 */
if ($basePointer&lt;1) {
    echo "It looks like your path is not a Zend Framework path.\nPlease check and try again.\n";
    displayhelp();
    die(1);
}
 
// At this point, we need to check to see if the file exists. If neither exist, throw exception.
if (isset($options['stub'])) {
    $stubFile = $options['stub'];
} else {
    $stubFile = 'stub.php';
}
 
if(!file_exists($sourceLocation))
{
 echo "ERROR: Source file location does not exist!\nCheck your source and try again.\n";
    displayhelp();
    die(1); 
}
 
/*
* Let the user know what is going on
*/
echo "Creating PHAR\n";
echo "Source      : {$sourceLocation}\n";
echo "Destination : {$pharFile}\n";
echo "Stub File   : {$stubFile}\n\n";
 
/*
* Clean up from previous runs
*/
if (file_exists($pharFile)) {
  Phar::unlinkArchive($pharFile);
}
 
/*
* Setup the phar
*/
$p = new Phar($pharFile, 0, $pharFile);
$p->compressFiles(Phar::GZ);
$p->setSignatureAlgorithm (Phar::SHA1);
 
/*
* Now build the array of files to be in the phar.
* The first file is the stub file. The rest of the files are built from the directory.
*/
$files = array();
$files["stub.php"] = $stubFile;
 
echo "Building the array of files to include.\n";
 
 
$rd = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourceLocation));
foreach($rd as $file) {
 if (strpos($file->getPath(),'.svn')===false &&
      $file->getFilename() != '..' &&
      $file->getFilename() != '.')
      {
         $fileIndex = substr($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename(),$basePointer);
         $fileName = $file->getPath().DIRECTORY_SEPARATOR.$file->getFilename();
         $files[$fileIndex] = $fileName;
         // Coined "phindex" to refer to the included index pointing to the real filename on disk we are creating
         if (isset($options['v'])) {
            echo "   PHIndex[{$fileIndex}] = {$fileName}\n";            
         } // if (isset($options['v']))
      }
} // foreach($rd as $file)
 
echo "Now building the phar.\n";
 
/*
* Now build the archive.
*/
$p->startBuffering();
$p->buildFromIterator(new ArrayIterator($files));
$p->stopBuffering();
 
/*
* finish up.
*/
$p->setStub($p->createDefaultStub("stub.php"));
$p = null;
 
if (isset($options['v'])) {
    echo count($files)." files Added to ".$pharFile."\n";
} // if (isset($options['v']))
 
echo "Done.\n";
exit;
 
function displayHelp()
{
    echo "\n\npachage.php\n";
    echo "  Authors: Cal Evans, John Douglass\n\n";
    echo "  -s The directory where Zend Framework is located. Must end in /Zend. \n";
    echo "  -p The name to give your phar file.\n";
    echo "  --stub The name of your stub file. Will default to stub.php if not passed in.\n";
    echo "  -v verbose mode.\n";
}
</john>

Related posts

  • Lessons in Phar (16)
  • XAMPP, PHP 5.3, PEAR, and PHAR (what a mess) (12)
  • Update from webcast on Zend_Cache_Frontend_Class (2)

Tags: phar, PHP, php 5.3, zend framework

This entry was posted on Sunday, July 26th, 2009 at 4:11 am and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.Both comments and pings are currently closed.

3 Responses to “Packaging Zend Framework As A Phar Revisited”

  1. Cal Evans’ Blog: Packaging Zend Framework As A Phar Revisited | Webs Developer Says:
    July 27th, 2009 at 8:02 am

    [...] at the phar archiving tool that comes standard in PHP releases now (since v5.3), Cal Evans has revisited the topic and updated his code to reflect some recommendations made from a reader (John Douglass). [...]

  2. Cal Evans’ Blog: Packaging Zend Framework As A Phar Revisited | PHP Says:
    July 27th, 2009 at 10:03 am

    [...] at the phar archiving tool that comes standard in PHP releases now (since v5.3), Cal Evans has revisited the topic and updated his code to reflect some recommendations made from a reader (John Douglass). [...]

  3. Things I learned about Zend Tool – techPortal Says:
    August 5th, 2009 at 11:01 am

    [...] Packaging Zend Framework As A Phar Revisited (2) [...]

  • Team Based PHP Training

  • Sponsors and Ads

  • Conferences I’m Attending

  • About Me

    cal_evansThis is my blog. Sometimes it's my deep thoughts, sometimes it's a journal of things I've learned. Every now and then it's my box of shattered dreams. Most of the time though, it's just the place I like to write. Sit with me as I show you some postcards from my life. While you are here, do me a favor and leave a comment.

    If you are looking for my contact information, bio, picture, ASL, check out my EPK.

    My name is Cal Evans and this is my blog.



    Follow me on FriendFeed!

    View Cal Evans's profile on LinkedIn

  • My First Book

  • Support PHPWomen


    US Shop | European Shop

  • What I'm Doing...

    • @farrelley nope, should go live next Tuesday, subscribe in Itunes http://bit.ly/bf9vmN or the feed http://bit.ly/9nFI6A in reply to farrelley 10 hrs ago
    • awesome time w/@Elazar and @brandonsavage recording #oddWeek #5 (at least I hope it's #5 if not I've got some editing to do) 10 hrs ago
    • thanks for all who expressed interest in reviewing my Speaker Tips. I've got a full editorial board now. 14 hrs ago
    • More updates...

  • Tags

    API article Cal Evans codeworks conference contest cw09 developers devzone elizabeth naramore Exim flex fun IBuildings iPod jobs Kathy Evans linkedin Management Marketing microsoft MySQL Nashville phar photography PHP phparchitect php developers podcampnashville podcast podcasting poem Programming Quickies respect Silly-Con Valley sixty second tech software development terry chay twitter upgrade video wordpress zend zend framework

  • RSS PHP Podcasts

    • webcast: Introduction to Doctrine 2
    • 8 Reasons Every PHP Developer Should Love JavaScript
    • oddWeek Episode #4
    • Creating Custom Zend_Form Decorators
    • Habits of Highly Scalable Web Applications
    • PHPSPCast #6 – Ao vivo da Campus Party (Q&A)
    • php|architect Podcast: oddWeek #002
    • php|architect podcast: oddWeek #003
    • Podcast #2010-02: Stalker Edition
    • php|architect Podcast: oddWeek #001

  • XBox Gamer Card

  • Me

    • Best web design company
    • Cal Evans Dot Com
    • Cyrano’s Apprentice
    • Evans Internet Construction Company
    • My Life as a Child
    • PHP Podcasts
    • Sixty Second Tech

  • RSS My Blog at php|arch

    • An error has occurred; the feed is probably down. Try again later.

  • Flickr Recent Photos

    Blue Parabola Southern Office-Rear Annex is closed for snowSnow Heart@dzuelke getting ready to give his talk@fabpot talking about Dependancy Injection@derickr giving the opening keynotePeople meeting other peoplePHP Benelux Goody Bag ContentsCheck InDSCN2280The main room

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me
    • PHP
    • podcasting
    • Programming
    • SQL
    • Technology
    • Web 2.0
    • wordpress
    • WordPress Plugins
    • writing
    • zend framework

  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org


Postcards From My Life is proudly powered by WordPress
Entries (RSS) and Comments (RSS).