Skip to content

Instantly share code, notes, and snippets.

@aurimasniekis
Last active December 3, 2015 03:51
Show Gist options
  • Save aurimasniekis/ccd75a5b168a14bcf0be to your computer and use it in GitHub Desktop.
Save aurimasniekis/ccd75a5b168a14bcf0be to your computer and use it in GitHub Desktop.
Funct multi file function merger to single file
<?php
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
require_once __DIR__ . '/vendor/autoload.php';
$groups = ['Collection', 'String', 'Objects'];
foreach ($groups as $groupName) {
$lowGroupName = strtolower($groupName);
$root = realpath(__DIR__ . '/../funct/src');
$finder = new Finder();
$finder->in($root . '/CodeBlocks/' . $groupName)->name('*.php')->files();
$files = [];
/** @var SplFileInfo $splFile */
foreach ($finder as $splFile) {
$files[] = [
'real_path' => $splFile->getRealPath(),
'filename' => $splFile->getFilename(),
'content' => $splFile->getContents()
];
}
$functions = [];
foreach ($files as $file) {
$tokens = [];
$startCapturingTokens = false;
$functionNameTokens = 2;
$functionName = null;
foreach (token_get_all($file['content']) as $token) {
if (is_array($token)) {
if ($token[0] === T_DOC_COMMENT) {
$startCapturingTokens = true;
}
if ($functionNameTokens === 0) {
$token[1] = str_replace($lowGroupName . '_', '', $token[1]);
$functionName = $token[1];
$functionNameTokens = false;
}
if ($token[0] === T_FUNCTION) {
$functionNameTokens--;
}
if ($functionNameTokens === 1 && $token[0] === T_WHITESPACE) {
$functionNameTokens--;
}
if ($token[0] === T_STRING) {
$token[1] = str_replace($lowGroupName . '_', '', $token[1]);
}
}
if ($startCapturingTokens) {
$tokens[] = $token;
}
}
$code = '';
foreach ($tokens as $token) {
if (is_array($token)) {
$code .= $token[1];
} else {
$code .= $token;
}
}
$functions[$functionName] = $code;
}
ksort($functions);
$template = [
'<?php',
'',
'namespace Funct\\' . $groupName . ';',
'',
implode(PHP_EOL . PHP_EOL, $functions)
];
$file = $root . '/' . $groupName . '.php';
file_put_contents($file, implode(PHP_EOL, $template));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment