Skip to content
This repository
Browse code

[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())
  • Loading branch information...
commit 86bb1be61f03897c0a3a39533bed7e71be1e7271 1 parent ab48114
Victor Berchet vicb authored
16 src/Composer/Autoload/ClassMapGenerator.php
@@ -107,21 +107,27 @@ private static function findClasses($path)
107 107 throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
108 108 }
109 109
110   - if (!preg_match('{\b(?:class|interface'.$traits.')\b}i', $contents)) {
111   - return array();
112   - }
113   -
114 110 // strip heredocs/nowdocs
115 111 $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
116 112 // strip strings
117 113 $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}', 'null', $contents);
  114 + // keep only php code
  115 + $phpContents = preg_match_all('{<\?(?:php)?(.*)\?>}s', $contents, $m) ? join($m[1], ' ') : '';
  116 + $contents = preg_replace('{<\?(php)?.*\?>}s', '', $contents);
  117 + if (preg_match('{<\?(?:php)?(.*)}s', $contents, $m)) {
  118 + $phpContents .= ' ' . $m[1];
  119 + }
  120 +
  121 + if (!preg_match('{\b(?:class|interface'.$traits.')\b}i', $phpContents)) {
  122 + return array();
  123 + }
118 124
119 125 preg_match_all('{
120 126 (?:
121 127 \b(?<![\$:>])(?<type>class|interface'.$traits.') \s+ (?<name>\S+)
122 128 | \b(?<![\$:>])(?<ns>namespace) (?<nsname>\s+[^\s;{}\\\\]+(?:\s*\\\\\s*[^\s;{}\\\\]+)*)? \s*[\{;]
123 129 )
124   - }ix', $contents, $matches);
  130 + }ix', $phpContents, $matches);
125 131 $classes = array();
126 132
127 133 $namespace = '';
1  tests/Composer/Test/Autoload/ClassMapGeneratorTest.php
@@ -53,6 +53,7 @@ public function getTestCreateMapTests()
53 53 'ClassMap\\SomeParent' => realpath(__DIR__).'/Fixtures/classmap/SomeParent.php',
54 54 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
55 55 )),
  56 + array(__DIR__.'/Fixtures/template', array()),
56 57 );
57 58
58 59 if (version_compare(PHP_VERSION, '5.4', '>=')) {
6 tests/Composer/Test/Autoload/Fixtures/template/template_1.php
... ... @@ -0,0 +1,6 @@
  1 +/*
  2 + * class templateClass_1
  3 + * interface templateInterface_1
  4 + * trait temlpateTrait_1
  5 + */
  6 +<?php echo $code
8 tests/Composer/Test/Autoload/Fixtures/template/template_2.php
... ... @@ -0,0 +1,8 @@
  1 +/*
  2 + * class templateClass_2
  3 + * interface templateInterface_2
  4 + * trait temlpateTrait_2
  5 + */
  6 +<?php
  7 + echo $code
  8 +?>

0 comments on commit 86bb1be

Please sign in to comment.
Something went wrong with that request. Please try again.