1
0
Fork 0

[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())
pull/1315/head
Victor Berchet 2012-11-12 09:15:35 +01:00
parent ab48114531
commit 86bb1be61f
4 changed files with 26 additions and 5 deletions

View File

@ -107,21 +107,27 @@ class ClassMapGenerator
throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e); 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 // strip heredocs/nowdocs
$contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents); $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
// strip strings // strip strings
$contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}', 'null', $contents); $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('{ preg_match_all('{
(?: (?:
\b(?<![\$:>])(?<type>class|interface'.$traits.') \s+ (?<name>\S+) \b(?<![\$:>])(?<type>class|interface'.$traits.') \s+ (?<name>\S+)
| \b(?<![\$:>])(?<ns>namespace) (?<nsname>\s+[^\s;{}\\\\]+(?:\s*\\\\\s*[^\s;{}\\\\]+)*)? \s*[\{;] | \b(?<![\$:>])(?<ns>namespace) (?<nsname>\s+[^\s;{}\\\\]+(?:\s*\\\\\s*[^\s;{}\\\\]+)*)? \s*[\{;]
) )
}ix', $contents, $matches); }ix', $phpContents, $matches);
$classes = array(); $classes = array();
$namespace = ''; $namespace = '';

View File

@ -53,6 +53,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php', 'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php',
'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php', 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
)), )),
array(__DIR__.'/Fixtures/template', array()),
); );
if (version_compare(PHP_VERSION, '5.4', '>=')) { if (version_compare(PHP_VERSION, '5.4', '>=')) {

View File

@ -0,0 +1,6 @@
/*
* class templateClass_1
* interface templateInterface_1
* trait temlpateTrait_1
*/
<?php echo $code

View File

@ -0,0 +1,8 @@
/*
* class templateClass_2
* interface templateInterface_2
* trait temlpateTrait_2
*/
<?php
echo $code
?>