fetch-manual 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of Psy Shell.
  5. *
  6. * (c) 2012-2025 Justin Hileman
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. // Fetch the latest manual from GitHub releases for bundling in PHAR builds
  12. const RELEASES_URL = 'https://api.github.com/repos/bobthecow/psysh-manual/releases';
  13. function fetchLatestManual(): bool {
  14. echo "Fetching latest manual release info...\n";
  15. $context = stream_context_create([
  16. 'http' => [
  17. 'user_agent' => 'PsySH Manual Fetcher',
  18. 'timeout' => 10.0,
  19. ],
  20. ]);
  21. $result = @file_get_contents(RELEASES_URL, false, $context);
  22. if (!$result) {
  23. echo "Failed to fetch releases from GitHub\n";
  24. return false;
  25. }
  26. $releases = json_decode($result, true);
  27. if (!$releases || !is_array($releases)) {
  28. echo "Invalid response from GitHub releases API\n";
  29. return false;
  30. }
  31. // Find the first release with a manifest
  32. foreach ($releases as $release) {
  33. $manifest = fetchManifest($release, $context);
  34. if ($manifest === null) {
  35. continue;
  36. }
  37. // Find English PHP format manual in the manifest
  38. foreach ($manifest['manuals'] as $manual) {
  39. if ($manual['lang'] === 'en' && $manual['format'] === 'php') {
  40. echo "Found manual v{$manual['version']} (en, php format)\n";
  41. // Find the download URL for the manual
  42. $filename = sprintf('psysh-manual-v%s-en.tar.gz', $manual['version']);
  43. $downloadUrl = null;
  44. foreach ($release['assets'] as $asset) {
  45. if ($asset['name'] === $filename) {
  46. $downloadUrl = $asset['browser_download_url'];
  47. break;
  48. }
  49. }
  50. if ($downloadUrl === null) {
  51. echo "Could not find download URL for $filename\n";
  52. return false;
  53. }
  54. // Download and extract the manual
  55. return downloadAndExtractManual($downloadUrl, $filename, $context);
  56. }
  57. }
  58. }
  59. echo "No English PHP manual found in releases\n";
  60. return false;
  61. }
  62. function fetchManifest(array $release, $context): ?array {
  63. foreach ($release['assets'] as $asset) {
  64. if ($asset['name'] === 'manifest.json') {
  65. $manifestContent = @file_get_contents($asset['browser_download_url'], false, $context);
  66. if ($manifestContent) {
  67. return json_decode($manifestContent, true);
  68. }
  69. }
  70. }
  71. return null;
  72. }
  73. function downloadAndExtractManual(string $downloadUrl, string $filename, $context): bool {
  74. echo "Downloading $filename...\n";
  75. $tempFile = sys_get_temp_dir() . '/' . $filename;
  76. $content = @file_get_contents($downloadUrl, false, $context);
  77. if (!$content) {
  78. echo "Failed to download manual\n";
  79. return false;
  80. }
  81. if (!file_put_contents($tempFile, $content)) {
  82. echo "Failed to save manual to $tempFile\n";
  83. return false;
  84. }
  85. echo "Extracting manual...\n";
  86. // Create temp directory for extraction
  87. $tempDir = sys_get_temp_dir() . '/psysh-manual-' . uniqid();
  88. if (!mkdir($tempDir)) {
  89. echo "Failed to create temp directory\n";
  90. unlink($tempFile);
  91. return false;
  92. }
  93. try {
  94. // Extract using PharData
  95. $phar = new PharData($tempFile);
  96. $phar->extractTo($tempDir);
  97. // Find the php_manual.php file
  98. $extractedFile = $tempDir . '/php_manual.php';
  99. if (!file_exists($extractedFile)) {
  100. echo "php_manual.php not found in extracted archive\n";
  101. return false;
  102. }
  103. // Copy to current directory
  104. if (!copy($extractedFile, 'php_manual.php')) {
  105. echo "Failed to copy manual to current directory\n";
  106. return false;
  107. }
  108. echo "Successfully fetched manual to php_manual.php\n";
  109. return true;
  110. } catch (Exception $e) {
  111. echo "Failed to extract manual: " . $e->getMessage() . "\n";
  112. return false;
  113. } finally {
  114. // Clean up
  115. @unlink($tempFile);
  116. removeDirectory($tempDir);
  117. }
  118. }
  119. function removeDirectory(string $dir) {
  120. if (!is_dir($dir)) {
  121. return;
  122. }
  123. $files = array_diff(scandir($dir), ['.', '..']);
  124. foreach ($files as $file) {
  125. $path = $dir . '/' . $file;
  126. is_dir($path) ? removeDirectory($path) : unlink($path);
  127. }
  128. rmdir($dir);
  129. }
  130. // Main execution
  131. exit(fetchLatestManual() ? 0 : 1);