Skip to content

Instantly share code, notes, and snippets.

@caendesilva
Created July 13, 2024 20:09
Show Gist options
  • Save caendesilva/b99d30f3d1a610d665a7408236273014 to your computer and use it in GitHub Desktop.
Save caendesilva/b99d30f3d1a610d665a7408236273014 to your computer and use it in GitHub Desktop.
Package all PHP source code files into a single file to use for AI contexting
<?php
function createCodeModel($directory, $outputFile): void
{
// Open the output file in append mode
$output = fopen($outputFile, 'a');
// Get all files in the current directory
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
fwrite($output, "--- Below is all code in the $directory directory ---\n");
foreach ($files as $file) {
if ($file->isFile() && $file->getExtension() == 'php') {
$relativePath = substr($file->getPathname(), strlen($directory) + 1);
$content = file_get_contents($file->getPathname());
// Unset the opening PHP tag
$content = preg_replace('/<\?php/', '', $content, 1);
$content = trim($content);
// Write the file path comment and content to the output file
fwrite($output, "\n<?php // file: $relativePath\n\n");
fwrite($output, $content . "\n?>\n");
}
}
fclose($output);
}
// Usage
$srcDirectory = './src';
$outputFile = './classes.php';
// Clear the output file if it already exists
file_put_contents($outputFile, '');
// Run the aggregation
createCodeModel($srcDirectory, $outputFile);
echo "PHP files have been aggregated into $outputFile";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment