build_phar.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. $dist = dirname(__DIR__).'/dist';
  3. if (!is_dir($dist)) {
  4. mkdir($dist, 0755);
  5. }
  6. if (file_exists($dist.'/random_compat.phar')) {
  7. unlink($dist.'/random_compat.phar');
  8. }
  9. $phar = new Phar(
  10. $dist.'/random_compat.phar',
  11. FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME,
  12. 'random_compat.phar'
  13. );
  14. rename(
  15. dirname(__DIR__).'/lib/random.php',
  16. dirname(__DIR__).'/lib/index.php'
  17. );
  18. $phar->buildFromDirectory(dirname(__DIR__).'/lib');
  19. rename(
  20. dirname(__DIR__).'/lib/index.php',
  21. dirname(__DIR__).'/lib/random.php'
  22. );
  23. /**
  24. * If we pass an (optional) path to a private key as a second argument, we will
  25. * sign the Phar with OpenSSL.
  26. *
  27. * If you leave this out, it will produce an unsigned .phar!
  28. */
  29. if ($argc > 1) {
  30. if (!@is_readable($argv[1])) {
  31. echo 'Could not read the private key file:', $argv[1], "\n";
  32. exit(255);
  33. }
  34. $pkeyFile = file_get_contents($argv[1]);
  35. $private = openssl_get_privatekey($pkeyFile);
  36. if ($private !== false) {
  37. $pkey = '';
  38. openssl_pkey_export($private, $pkey);
  39. $phar->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
  40. /**
  41. * Save the corresponding public key to the file
  42. */
  43. if (!@is_readable($dist.'/random_compat.phar.pubkey')) {
  44. $details = openssl_pkey_get_details($private);
  45. file_put_contents(
  46. $dist.'/random_compat.phar.pubkey',
  47. $details['key']
  48. );
  49. }
  50. } else {
  51. echo 'An error occurred reading the private key from OpenSSL.', "\n";
  52. exit(255);
  53. }
  54. }