Browse Source

Add a script to generate a Phar archive of Predis from the repository.

Daniele Alessandri 14 years ago
parent
commit
658782f531
2 changed files with 60 additions and 3 deletions
  1. 7 3
      README.markdown
  2. 53 0
      bin/createPhar.php

+ 7 - 3
README.markdown

@@ -40,9 +40,13 @@ When used in simple projects or scripts you might need to define an autoloader f
         }
     });
 
-Optionally, you can generate a single PHP file that holds every class (just like older versions of Predis) 
-by launching the _createSingleFile.php_ script from the _bin_ directory of the repository. In this way 
-you can load Predis in your scripts simply by using functions such as _require_ and _include_.
+You can also create a single Phar archive from the repository just by launching the _createPhar.php_ 
+script located in the _bin_ directory. The generated Phar ships with a stub that defines an autoloader 
+function for Predis, so you just need to require the Phar archive in order to be able to use the library.
+
+Alternatively you can generate a single PHP file that holds every class, just like older versions of Predis, 
+using the _createSingleFile.php_ script located in the _bin_ directory. In this way you can load Predis in 
+your scripts simply by using functions such as _require_ and _include_, but this practice is not encouraged.
 
 
 ### Connecting to a local instance of Redis ###

+ 53 - 0
bin/createPhar.php

@@ -0,0 +1,53 @@
+#!/usr/bin/php
+<?php
+// In order to be able to execute this script to create a Phar archive of Predis,
+// the Phar module must be loaded and the "phar.readonly" directive php.ini must
+// be set to "off". You can change the values in the $options array to customize
+// the creation of the Phar archive to better suit your needs.
+
+$options = array(
+    'name'           => 'predis',
+    'project_path'   => __DIR__ . '/../lib/',
+    'compression'    => Phar::NONE,
+    'append_version' => true,
+);
+
+function getPharFilename($options) {
+    $filename = $options['name'];
+    // NOTE: do not consider "append_version" with Phar compression do to a bug in
+    // Phar::compress() when renaming phar archives containing dots in their name.
+    if ($options['append_version'] && $options['compression'] === Phar::NONE) {
+        $versionFile = @fopen(__DIR__ . '/../VERSION', 'r');
+        if ($versionFile === false) {
+            throw new Exception("Could not locate the VERSION file.");
+        }
+        $version = trim(fgets($versionFile));
+        fclose($versionFile);
+        $filename .= "_$version";
+    }
+    return "$filename.phar";
+}
+
+function getPharStub($options) {
+    return <<<EOSTUB
+<?php
+Phar::mapPhar('predis.phar');
+spl_autoload_register(function(\$class) {
+    if (strpos(\$class, 'Predis\\\\') === 0) {
+        \$file = 'phar://predis.phar/'.strtr(\$class, '\\\', '/').'.php';
+        if (file_exists(\$file)) {
+            require \$file;
+            return true;
+        }
+    }
+});
+__HALT_COMPILER();
+EOSTUB;
+}
+
+// -------------------------------------------------------------------------- //
+
+$phar = new Phar(getPharFilename($options));
+$phar->compress($options['compression']);
+$phar->setStub(getPharStub($options));
+$phar->buildFromDirectory($options['project_path']);