Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • How to Plan a Website
  • 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 <[email protected]>
* @author John Douglass <john .[email protected]>
*/
 
$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>
Buffer

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.

Logging In...

Comments are closed.

  • 3 Replies
  • 0 Comments
  • 0 Tweets
  • 0 Facebook
  • 3 Pingbacks
Last reply was August 5, 2009
  1. Cal Evans’ Blog: Packaging Zend Framework As A Phar Revisited | Webs Developer
    View July 27, 2009

    [...] 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
    View July 27, 2009

    [...] 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
    View August 5, 2009

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

  • Friends of mine

  • My Latest Book


    Avoiding a Goat Rodeo

  • Follow me on twitter!

  • RSS PHP Podcasts

    • Episode 7: Web Sockets Are Fast
    • Better Documentation for PHP internals – Lately in PHP podcast episode 35
    • Episode 31: Feline Tooth Extraction
    • Episode #2 – Adam Culp
    • Episode 6: PSR-X and the Mexican Standoff
    • Episode 109: Typescript and a bit more…
    • A Better PHP Feature Voting Process – Lately in PHP podcast episode 34
    • Episode 30: It’s Episode 30, you guys
    • PHP Innovation Award Winner of 2012 – Lately in PHP podcast episode 33
    • Episode 29: Snappy Answers to Stupid Questions

  • Me, elsewhere on the Web

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

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • Book Review
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me, elsewhere on the Web
    • 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).