[ClassLoader] fixed unbracketed namespaces (closes #5747)
This commit is contained in:
parent
887207402b
commit
6f15c4780f
@ -19,6 +19,7 @@ namespace Symfony\Component\ClassLoader;
|
||||
class ClassCollectionLoader
|
||||
{
|
||||
private static $loaded;
|
||||
private static $useTokenizer = true;
|
||||
|
||||
/**
|
||||
* Loads a list of classes and caches them in one big file.
|
||||
@ -125,7 +126,11 @@ class ClassCollectionLoader
|
||||
*/
|
||||
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$2\n{", $source)."}\n";
|
||||
}
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
@ -219,4 +224,12 @@ class ClassCollectionLoader
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is only useful for testing.
|
||||
*/
|
||||
public static function enableTokenizer($bool)
|
||||
{
|
||||
self::$useTokenizer = (Boolean) $bool;
|
||||
}
|
||||
}
|
||||
|
@ -15,53 +15,34 @@ use Symfony\Component\ClassLoader\ClassCollectionLoader;
|
||||
|
||||
class ClassCollectionLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testFixNamespaceDeclarations()
|
||||
/**
|
||||
* @dataProvider getFixNamespaceDeclarationsData
|
||||
*/
|
||||
public function testFixNamespaceDeclarations($source, $expected)
|
||||
{
|
||||
$source = <<<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);
|
||||
}
|
||||
|
||||
$expected = <<<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;
|
||||
|
||||
$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"),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user