createPhar.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env php
  2. <?php
  3. // In order to be able to execute this script to create a Phar archive of Predis,
  4. // the Phar module must be loaded and the "phar.readonly" directive php.ini must
  5. // be set to "off". You can change the values in the $options array to customize
  6. // the creation of the Phar archive to better suit your needs.
  7. $options = array(
  8. 'name' => 'predis',
  9. 'project_path' => __DIR__ . '/../lib/',
  10. 'compression' => Phar::NONE,
  11. 'append_version' => true,
  12. );
  13. function getPharFilename($options) {
  14. $filename = $options['name'];
  15. // NOTE: do not consider "append_version" with Phar compression do to a bug in
  16. // Phar::compress() when renaming phar archives containing dots in their name.
  17. if ($options['append_version'] && $options['compression'] === Phar::NONE) {
  18. $versionFile = @fopen(__DIR__ . '/../VERSION', 'r');
  19. if ($versionFile === false) {
  20. throw new Exception("Could not locate the VERSION file.");
  21. }
  22. $version = trim(fgets($versionFile));
  23. fclose($versionFile);
  24. $filename .= "_$version";
  25. }
  26. return "$filename.phar";
  27. }
  28. function getPharStub($options) {
  29. return <<<EOSTUB
  30. <?php
  31. Phar::mapPhar('predis.phar');
  32. spl_autoload_register(function(\$class) {
  33. if (strpos(\$class, 'Predis\\\\') === 0) {
  34. \$file = 'phar://predis.phar/'.strtr(\$class, '\\\', '/').'.php';
  35. if (file_exists(\$file)) {
  36. require \$file;
  37. return true;
  38. }
  39. }
  40. });
  41. __HALT_COMPILER();
  42. EOSTUB;
  43. }
  44. // -------------------------------------------------------------------------- //
  45. $phar = new Phar(getPharFilename($options));
  46. $phar->compress($options['compression']);
  47. $phar->setStub(getPharStub($options));
  48. $phar->buildFromDirectory($options['project_path']);