Skip to content

Fix UniversalClassLoader matching collisions. #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/Symfony/Component/HttpFoundation/UniversalClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,12 @@ public function loadClass($class)
$namespace = substr($class, 0, $pos);
foreach ($this->namespaces as $ns => $dir) {
if (0 === strpos($namespace, $ns)) {
$class = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
require $file;
return;
}

return;
}
}
} else {
Expand All @@ -148,9 +147,8 @@ public function loadClass($class)
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}

return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace NamespaceCollision\A;

class Bar
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace NamespaceCollision\A;

class Foo
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class PrefixCollision_A_Bar
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class PrefixCollision_A_Foo
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace NamespaceCollision\A\B;

class Bar
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace NamespaceCollision\A\B;

class Foo
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class PrefixCollision_A_B_Bar
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class PrefixCollision_A_B_Foo
{
public static $loaded = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,104 @@ public static function testClassProvider()
array('\\Pearlike_Bar', '\\Pearlike_Bar', '->loadClass() loads Pearlike_Bar class with a leading slash'),
);
}
}

/**
* @dataProvider namespaceCollisionClassProvider
*/
public function testLoadClassNamespaceCollision($namespaces, $className, $message)
{
$loader = new UniversalClassLoader();
$loader->registerNamespaces($namespaces);

$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}

public static function namespaceCollisionClassProvider()
{
return array(
array(
array(
'NamespaceCollision\\A' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
'NamespaceCollision\\A\\B' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
),
'NamespaceCollision\A\Foo',
'->loadClass() loads NamespaceCollision\A\Foo from alpha.',
),
array(
array(
'NamespaceCollision\\A\\B' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
'NamespaceCollision\\A' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
),
'NamespaceCollision\A\Bar',
'->loadClass() loads NamespaceCollision\A\Bar from alpha.',
),
array(
array(
'NamespaceCollision\\A' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
'NamespaceCollision\\A\\B' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
),
'NamespaceCollision\A\B\Foo',
'->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
),
array(
array(
'NamespaceCollision\\A\\B' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
'NamespaceCollision\\A' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
),
'NamespaceCollision\A\B\Bar',
'->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
),
);
}

/**
* @dataProvider prefixCollisionClassProvider
*/
public function testLoadClassPrefixCollision($prefixes, $className, $message)
{
$loader = new UniversalClassLoader();
$loader->registerPrefixes($prefixes);

$loader->loadClass($className);
$this->assertTrue(class_exists($className), $message);
}

public static function prefixCollisionClassProvider()
{
return array(
array(
array(
'PrefixCollision_A_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
'PrefixCollision_A_B_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
),
'PrefixCollision_A_Foo',
'->loadClass() loads PrefixCollision_A_Foo from alpha.',
),
array(
array(
'PrefixCollision_A_B_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
'PrefixCollision_A_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
),
'PrefixCollision_A_Bar',
'->loadClass() loads PrefixCollision_A_Bar from alpha.',
),
array(
array(
'PrefixCollision_A_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
'PrefixCollision_A_B_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
),
'PrefixCollision_A_B_Foo',
'->loadClass() loads PrefixCollision_A_B_Foo from beta.',
),
array(
array(
'PrefixCollision_A_B_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/beta',
'PrefixCollision_A_' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures/alpha',
),
'PrefixCollision_A_B_Bar',
'->loadClass() loads PrefixCollision_A_B_Bar from beta.',
),
);
}
}