From ea620634899cb824003ecb04f2ce501fcc8443b8 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Fri, 11 Sep 2015 18:12:21 -0700 Subject: [PATCH] Ensure that a classes base class comes first in the merged source. This fixes https://github.com/mongofill/mongofill-hhvm/issues/46 --- tools/merge.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tools/merge.php b/tools/merge.php index 3f1d02b..a8cd567 100644 --- a/tools/merge.php +++ b/tools/merge.php @@ -21,8 +21,28 @@ private function mergePHPFiles(array $files) { $code = ''; + $needed_first = array(); + $file_names = array(); foreach($files as $file) { - $code .= PHP_EOL . $this->readCodeFromFile($file); + $file_names[basename($file)] = $file; + $code = $this->readCodeFromFile($file); + if (preg_match('/extends\s+(\w+)\s+.*/i', $code, $match)) { + $needed_first[$match[1] . '.php'] = True; + } + } + + print var_dump(array_keys($file_names)); + foreach(array_keys($needed_first) as $basename) { + if (array_key_exists($basename, $file_names)) { + $code .= PHP_EOL . $this->readCodeFromFile( + $file_names[$basename] + ); + } + } + foreach($files as $file) { + if (!array_key_exists(basename($file), $needed_first)) { + $code .= PHP_EOL . $this->readCodeFromFile($file); + } } return $code; @@ -80,7 +100,7 @@ private function prepareNamespaces($code) $ns, substr($ns, 0, strlen($ns)-1) . ' {', $code - ) . PHP_EOL . '}'; + ) . PHP_EOL . '}'; } private function findPHPFiles(array $paths) @@ -113,7 +133,7 @@ private function saveCodeToFile($filename, $code) } (new Merger( - ['src', 'vendor/mongofill/mongofill/src'], + ['src', 'vendor/mongofill/mongofill/src'], ['vendor/mongofill/mongofill/src/functions.php'], 'src/ext_mongo.php' ));