PageRenderTime 941ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/build_phar.php

https://github.com/sean-gnu/pheanstalk
PHP | 72 lines | 56 code | 12 blank | 4 comment | 3 complexity | 3ce8d05a2ebb81daaf3be9032dfaf19a MD5 | raw file
Possible License(s): MIT
  1. #!/usr/bin/env php
  2. <?php
  3. define('BASE_DIR', realpath(__DIR__ . '/..'));
  4. define('PHAR_PATH', BASE_DIR . '/pheanstalk.phar');
  5. define('PHAR_FILENAME', basename(PHAR_PATH));
  6. // ----------------------------------------
  7. reexecute_if_phar_readonly($argv);
  8. delete_existing_pheanstalk_phar();
  9. build_pheanstalk_phar();
  10. verify_pheanstalk_phar();
  11. exit(0);
  12. // ----------------------------------------
  13. // See: http://www.php.net/manual/en/phar.configuration.php#ini.phar.readonly
  14. function reexecute_if_phar_readonly($argv)
  15. {
  16. if (ini_get('phar.readonly') && !in_array('--ignore-readonly', $argv)) {
  17. $command = sprintf(
  18. 'php -d phar.readonly=0 %s --ignore-readonly',
  19. implode($argv, ' ')
  20. );
  21. echo "Phar configured readonly in php.ini; attempting to re-execute:\n";
  22. echo "$command\n";
  23. passthru($command, $exitStatus);
  24. exit($exitStatus);
  25. }
  26. }
  27. function delete_existing_pheanstalk_phar()
  28. {
  29. if (file_exists(PHAR_PATH)) {
  30. printf("- Deleting existing %s\n", PHAR_FILENAME);
  31. unlink(PHAR_PATH);
  32. }
  33. }
  34. function build_pheanstalk_phar()
  35. {
  36. $classDir = BASE_DIR . '/classes';
  37. printf("- Building %s from %s\n", PHAR_FILENAME, $classDir);
  38. $phar = new Phar(PHAR_PATH);
  39. $phar->buildFromDirectory($classDir);
  40. $phar->setStub(pheanstalk_phar_stub());
  41. }
  42. function pheanstalk_phar_stub()
  43. {
  44. $pheanstalkInit = BASE_DIR . '/pheanstalk_init.php';
  45. printf("- Generating Phar stub based on %s\n", basename($pheanstalkInit));
  46. $stub = file_get_contents($pheanstalkInit);
  47. $stub = str_replace('<?php', '', $stub);
  48. $stub = str_replace("dirname(__FILE__) . '/classes';", "'phar://' . __FILE__;", $stub);
  49. return implode(array(
  50. '<?php',
  51. 'Phar::mapPhar();',
  52. $stub,
  53. '__HALT_COMPILER();'
  54. ), PHP_EOL);
  55. }
  56. function verify_pheanstalk_phar()
  57. {
  58. $phar = new Phar(PHAR_PATH);
  59. printf("- %s built with %d files.\n", PHAR_FILENAME, $phar->count());
  60. }