Skip to content

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>

3 thoughts on “Packaging Zend Framework As A Phar Revisited

  1. Pingback: Things I learned about Zend Tool – techPortal

Comments are closed.