create-phar 2.2 KB

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