Permalink
Browse files

Merge branch '2.0' into 2.1

* 2.0:
  [ClassLoader] fixed unbracketed namespaces (closes #5747)

Conflicts:
	src/Symfony/Component/ClassLoader/ClassCollectionLoader.php
	tests/Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php
  • Loading branch information...
2 parents 1de4a18 + c393e86 commit d095c397b03c5714c7acfcec63ee6428f180a408 @fabpot fabpot committed Oct 27, 2012
Showing with 39 additions and 45 deletions.
  1. +14 −1 ClassCollectionLoader.php
  2. +25 −44 Tests/ClassCollectionLoaderTest.php
View
15 ClassCollectionLoader.php
@@ -20,6 +20,7 @@ class ClassCollectionLoader
{
private static $loaded;
private static $seen;
+ private static $useTokenizer = true;
/**
* Loads a list of classes and caches them in one big file.
@@ -135,7 +136,11 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
*/
public static function fixNamespaceDeclarations($source)
{
- if (!function_exists('token_get_all')) {
+ if (!function_exists('token_get_all') || !self::$useTokenizer) {
+ if (preg_match('/namespace(.*?)\s*;/', $source)) {
+ $source = preg_replace('/namespace(.*?)\s*;/', "namespace$1\n{", $source)."}\n";
+ }
+
return $source;
}
@@ -317,4 +322,12 @@ private static function getTraits(\ReflectionClass $class)
return $classes;
}
+
+ /**
+ * This method is only useful for testing.
+ */
+ public static function enableTokenizer($bool)
+ {
+ self::$useTokenizer = (Boolean) $bool;
+ }
}
View
69 Tests/ClassCollectionLoaderTest.php
@@ -117,53 +117,34 @@ public function getDifferentOrdersForTraits()
);
}
- public function testFixNamespaceDeclarations()
+ /**
+ * @dataProvider getFixNamespaceDeclarationsData
+ */
+ public function testFixNamespaceDeclarations($source, $expected)
{
- $source = <<<EOF
-<?php
-
-namespace Foo;
-class Foo {}
-namespace Bar ;
-class Foo {}
-namespace Foo\Bar;
-class Foo {}
-namespace Foo\Bar\Bar
-{
- class Foo {}
-}
-namespace
-{
- class Foo {}
-}
-EOF;
-
- $expected = <<<EOF
-<?php
+ $this->assertEquals('<?php '.$expected, ClassCollectionLoader::fixNamespaceDeclarations('<?php '.$source));
+ }
-namespace Foo
-{
-class Foo {}
-}
-namespace Bar
-{
-class Foo {}
-}
-namespace Foo\Bar
-{
-class Foo {}
-}
-namespace Foo\Bar\Bar
-{
- class Foo {}
-}
-namespace
-{
- class Foo {}
-}
-EOF;
+ /**
+ * @dataProvider getFixNamespaceDeclarationsData
+ */
+ public function testFixNamespaceDeclarationsWithoutTokenizer($source, $expected)
+ {
+ ClassCollectionLoader::enableTokenizer(false);
+ $this->assertEquals('<?php '.$expected, ClassCollectionLoader::fixNamespaceDeclarations('<?php '.$source));
+ ClassCollectionLoader::enableTokenizer(true);
+ }
- $this->assertEquals($expected, ClassCollectionLoader::fixNamespaceDeclarations($source));
+ public function getFixNamespaceDeclarationsData()
+ {
+ return array(
+ array("namespace;\nclass Foo {}\n", "namespace\n{\nclass Foo {}\n}\n"),
+ array("namespace Foo;\nclass Foo {}\n", "namespace Foo\n{\nclass Foo {}\n}\n"),
+ array("namespace Bar ;\nclass Foo {}\n", "namespace Bar\n{\nclass Foo {}\n}\n"),
+ array("namespace Foo\Bar;\nclass Foo {}\n", "namespace Foo\Bar\n{\nclass Foo {}\n}\n"),
+ array("namespace Foo\Bar\Bar\n{\nclass Foo {}\n}\n", "namespace Foo\Bar\Bar\n{\nclass Foo {}\n}\n"),
+ array("namespace\n{\nclass Foo {}\n}\n", "namespace\n{\nclass Foo {}\n}\n"),
+ );
}
/**

0 comments on commit d095c39

Please sign in to comment.