Files
llmdumper/llmdumper.php
2025-11-12 22:33:12 +02:00

142 lines
4.8 KiB
PHP

<?php
/**
* LLM Dumper for PrestaShop Modules
*
* This script recursively scans a PrestaShop module directory, concatenates all relevant source
* files into a single text file, and prepends each file's content with its relative path.
*
* PURPOSE: To gather all meaningful source code and template files from a module
* into a single file for easy pasting into a Large Language Model (LLM) context window.
*
* EXCLUSIONS:
* - The 'vendor' directory (Composer dependencies).
* - Image files (jpg, png, gif, svg, webp).
* - The script itself ('llmdumper.php').
* - The output file ('.llmdump').
*
* HOW TO USE:
* 1. Place this script in the root directory of the PrestaShop module you want to dump.
* (e.g., /modules/yourmodule/llmdumper.php)
* 2. Run it from the command line for best results:
* cd /path/to/prestashop/modules/yourmodule/
* php llmdumper.php
* 3. A hidden file named '.llmdump' will be created in the module's root directory.
* 4. Open '.llmdump', copy its contents, and paste it into your LLM prompt.
* 5. **IMPORTANT**: Delete this script and the '.llmdump' file from your server when you are done.
*/
// --- Configuration ---
// The name of the output file. Using a dotfile to keep it hidden.
define('OUTPUT_FILE', '.llmdump');
// The directory to exclude. Typically for Composer packages.
define('VENDOR_DIR', 'vendor');
define('GIT_DIR', '.git');
// A list of image file extensions to exclude. Case-insensitive.
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico', 'bmp'];
// --- Script Logic ---
// Set a long execution time for large modules. 0 means no time limit.
set_time_limit(0);
// The directory where this script is running (the module's root).
$moduleRootPath = __DIR__;
// The name of the module's directory.
$moduleDirName = basename($moduleRootPath);
// The full path for the output file.
$outputFilePath = $moduleRootPath . DIRECTORY_SEPARATOR . OUTPUT_FILE;
// Delete the old dump file if it exists to start fresh.
if (file_exists($outputFilePath)) {
unlink($outputFilePath);
}
echo "Starting LLM Dumper for module: '{$moduleDirName}'\n";
echo "---------------------------------------------------\n";
// Use RecursiveDirectoryIterator to scan all files and directories.
$directoryIterator = new RecursiveDirectoryIterator($moduleRootPath, RecursiveDirectoryIterator::SKIP_DOTS);
$fileIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);
$totalBytes = 0;
$fileCount = 0;
foreach ($fileIterator as $file) {
// --- Filtering Logic ---
// Get the full real path of the file.
$filePath = $file->getRealPath();
// 1. Exclude the script itself and the output file.
if ($file->getBasename() === 'llmdumper.php' || $file->getBasename() === OUTPUT_FILE) {
continue;
}
// 2. Exclude the entire 'vendor' directory.
// We check if the path contains '/vendor/'.
if (strpos($filePath, DIRECTORY_SEPARATOR . VENDOR_DIR . DIRECTORY_SEPARATOR) !== false) {
continue;
}
if (strpos($filePath, DIRECTORY_SEPARATOR . GIT_DIR . DIRECTORY_SEPARATOR) !== false) {
continue;
}
// We only care about files, not directories.
if ($file->isDir()) {
continue;
}
// 3. Exclude image files based on their extension.
$extension = strtolower($file->getExtension());
if (in_array($extension, IMAGE_EXTENSIONS)) {
continue;
}
// --- File Processing ---
// Get the file path relative to the module's root directory.
$relativePath = str_replace($moduleRootPath . DIRECTORY_SEPARATOR, '', $filePath);
// Prepend the module directory name to the relative path as requested.
$finalPath = $moduleDirName . DIRECTORY_SEPARATOR . $relativePath;
// Read the file content.
$content = file_get_contents($filePath);
if ($content === false) {
echo "WARNING: Could not read file: {$finalPath}\n";
continue;
}
// Create a header for this file's content to identify it in the dump.
$fileHeader = "
//- - - - - - - - - - START: {$finalPath} - - - - - - - - - -//
";
$fileFooter = "
//- - - - - - - - - - END: {$finalPath} - - - - - - - - - -//
\n";
// Assemble the full string to append to the output file.
$dumpContent = $fileHeader . $content . $fileFooter;
// Append the content to the output file.
if (file_put_contents($outputFilePath, $dumpContent, FILE_APPEND)) {
echo "Appended: {$finalPath}\n";
$totalBytes += strlen($dumpContent);
$fileCount++;
} else {
echo "ERROR: Could not write to output file!\n";
exit(1); // Exit with an error code.
}
}
echo "---------------------------------------------------\n";
echo "Dump complete!\n";
echo "- Files processed: {$fileCount}\n";
echo "- Total size: " . round($totalBytes / 1024, 2) . " KB\n";
echo "- Output file: {$outputFilePath}\n";