Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • Flex
  • Zend Framework
« 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)
  • Zend’s new namespace converter (7)
  • XAMPP, PHP 5.3, PEAR, and PHAR (what a mess) (12)

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) [...]

  • Event Registration Online for Day Camp 4 Developers : Soft Skills

  • Team Based PHP Training

  • Tags

    adobe API article Cal Evans codeworks community conference cw09 Derick Rethans developers devzone elizabeth naramore Exim flex fun IBuildings Kathy Evans linkedin Management Marketing microsoft MySQL Nashville open source phar PHP phparchitect php developers podcampnashville podcast Programming Quickies respect Sebastian Bergmann Silly-Con Valley sixty second tech software development terry chay twitter upgrade video windows wordpress zend zend framework

  • RSS PHP Podcasts

    • JSClasses, JSMag, PHP Alpha 1, PHP strict typing, IndieConf – Lately in PHP podcast episode 4
    • SitePoint Podcast #76: Wicked WordPress Themes with Allan Cole and Jeffrey Way
    • DPCRadio: Designing for Reusability
    • SitePoint Podcast #75: Awesome Overkill
    • DPCRadio: Technical Debt
    • SitePoint Podcast #74: WordPress Themes with Nathan Rice and Cory Miller
    • SitePoint Podcast #73: Cease and Desoup
    • DPCRadio: APC & Memcache the High Performance Duo
    • APC & Memcache the High Performance Duo
    • SitePoint Podcast #72: Web Video and Social Media with Gregory Ng and Wayne Sutton

  • Me

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

  • 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


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