From 86bb1be61f03897c0a3a39533bed7e71be1e7271 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 12 Nov 2012 09:15:35 +0100 Subject: [PATCH] [ClassMapGeneratot] Filter out non php code Otherwise files like https://github.com/propelorm/Propel/blob/master/generator/lib/behavior/i18n/templates/queryUseI18nQuery.php would fail ("class" keyword would not be filtered out by php_strip_whitespace()) --- src/Composer/Autoload/ClassMapGenerator.php | 16 +++++++++++----- .../Test/Autoload/ClassMapGeneratorTest.php | 1 + .../Autoload/Fixtures/template/template_1.php | 6 ++++++ .../Autoload/Fixtures/template/template_2.php | 8 ++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/template/template_1.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/template/template_2.php diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index e2518a721..0fe9b5cf7 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -107,21 +107,27 @@ class ClassMapGenerator throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e); } - if (!preg_match('{\b(?:class|interface'.$traits.')\b}i', $contents)) { - return array(); - } - // strip heredocs/nowdocs $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents); // strip strings $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}', 'null', $contents); + // keep only php code + $phpContents = preg_match_all('{<\?(?:php)?(.*)\?>}s', $contents, $m) ? join($m[1], ' ') : ''; + $contents = preg_replace('{<\?(php)?.*\?>}s', '', $contents); + if (preg_match('{<\?(?:php)?(.*)}s', $contents, $m)) { + $phpContents .= ' ' . $m[1]; + } + + if (!preg_match('{\b(?:class|interface'.$traits.')\b}i', $phpContents)) { + return array(); + } preg_match_all('{ (?: \b(?])(?class|interface'.$traits.') \s+ (?\S+) | \b(?])(?namespace) (?\s+[^\s;{}\\\\]+(?:\s*\\\\\s*[^\s;{}\\\\]+)*)? \s*[\{;] ) - }ix', $contents, $matches); + }ix', $phpContents, $matches); $classes = array(); $namespace = ''; diff --git a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php index 751aa6a38..779104567 100644 --- a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php +++ b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php @@ -53,6 +53,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase 'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php', 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php', )), + array(__DIR__.'/Fixtures/template', array()), ); if (version_compare(PHP_VERSION, '5.4', '>=')) { diff --git a/tests/Composer/Test/Autoload/Fixtures/template/template_1.php b/tests/Composer/Test/Autoload/Fixtures/template/template_1.php new file mode 100644 index 000000000..4a706f317 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/template/template_1.php @@ -0,0 +1,6 @@ +/* + * class templateClass_1 + * interface templateInterface_1 + * trait temlpateTrait_1 + */ +