createPhar.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. {
  15. $filename = $options['name'];
  16. // NOTE: do not consider "append_version" with Phar compression do to a bug in
  17. // Phar::compress() when renaming phar archives containing dots in their name.
  18. if ($options['append_version'] && $options['compression'] === Phar::NONE) {
  19. $versionFile = @fopen(__DIR__ . '/../VERSION', 'r');
  20. if ($versionFile === false) {
  21. throw new Exception("Could not locate the VERSION file.");
  22. }
  23. $version = trim(fgets($versionFile));
  24. fclose($versionFile);
  25. $filename .= "_$version";
  26. }
  27. return "$filename.phar";
  28. }
  29. function getPharStub($options)
  30. {
  31. return <<<EOSTUB
  32. <?php
  33. Phar::mapPhar('predis.phar');
  34. spl_autoload_register(function(\$class) {
  35. if (strpos(\$class, 'Predis\\\\') === 0) {
  36. \$file = 'phar://predis.phar/'.strtr(\$class, '\\\', '/').'.php';
  37. if (file_exists(\$file)) {
  38. require \$file;
  39. return true;
  40. }
  41. }
  42. });
  43. __HALT_COMPILER();
  44. EOSTUB;
  45. }
  46. // -------------------------------------------------------------------------- //
  47. $phar = new Phar(getPharFilename($options));
  48. $phar->compress($options['compression']);
  49. $phar->setStub(getPharStub($options));
  50. $phar->buildFromDirectory($options['project_path']);