Skip to content

[Debug] Replaced logic for detecting filesystem case sensitivity #18130

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 4 commits into from
Closed
Changes from 1 commit
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
Next Next commit
[Debug] Replaced logic for detecting filesystem case sensitivity
When I cloned the master branch onto a Virtualbox Vagrant OSX El Capitan host, Ubuntu Wily guest, the `Symfony\Component\Debug\Tests\DebugClassLoaderTest::testFileCaseMismatch` failed because 'Failed asserting that exception of type "\RuntimeException" is thrown'.

@wouterj confirmed he got the same problem, and it's because Virtualbox shared folders aren't case sensitive, even when the guest is using a case sensitive filesystem. So I've replaced the logic that looked at the name of the operating system.

I ran the tests in the following environments:
* Virtualbox/Vagrant - OSX Host, Ubuntu guest
* Virtualbox/Vagrant - OSX Host, Windows guest
* OSX native
* Ubuntu native

NB - I _didn't_ run it on native Windows (because I don't have easy access to one).
  • Loading branch information
Dan Blows committed Mar 12, 2016
commit eb70c9e28fa7fc57776b9ab261e30f2243659de1
66 changes: 44 additions & 22 deletions src/Symfony/Component/Debug/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class DebugClassLoader
private $isFinder;
private $wasFinder;
private static $caseCheck;
private static $deprecated = array();
private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
private static $darwinCache = array('/' => array('/', array()));
private static $deprecated = [];
private static $php7Reserved = ['int', 'float', 'bool', 'string', 'true', 'false', 'null'];
private static $darwinCache = ['/' => ['/', []]];

/**
* Constructor.
Expand All @@ -42,16 +42,26 @@ public function __construct($classLoader)
$this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile');

if ($this->wasFinder) {
@trigger_error('The '.__METHOD__.' method will no longer support receiving an object into its $classLoader argument in 3.0.', E_USER_DEPRECATED);
$this->classLoader = array($classLoader, 'loadClass');
@trigger_error('The ' . __METHOD__ . ' method will no longer support receiving an object into its $classLoader argument in 3.0.',
E_USER_DEPRECATED);
$this->classLoader = [$classLoader, 'loadClass'];
$this->isFinder = true;
} else {
$this->classLoader = $classLoader;
$this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
}

if (!isset(self::$caseCheck)) {
self::$caseCheck = false !== stripos(PHP_OS, 'win') ? (false !== stripos(PHP_OS, 'darwin') ? 2 : 1) : 0;
if (!file_exists(strtolower(__FILE__))) {
// filesystem is case sensitive
self::$caseCheck = 0;
} elseif (realpath(strtolower(__FILE__)) === realpath(__FILE__)) {
// filesystem is not case sensitive
self::$caseCheck = 1;
} else {
// filesystem is not case sensitive AND realpath() fails to normalize case
self::$caseCheck = 2;
}
}
}

Expand Down Expand Up @@ -84,7 +94,7 @@ class_exists('Psr\Log\LogLevel');

foreach ($functions as $function) {
if (!is_array($function) || !$function[0] instanceof self) {
$function = array(new static($function), 'loadClass');
$function = [new static($function), 'loadClass'];
}

spl_autoload_register($function);
Expand Down Expand Up @@ -124,7 +134,8 @@ public static function disable()
*/
public function findFile($class)
{
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.5 and will be removed in 3.0.',
E_USER_DEPRECATED);

if ($this->wasFinder) {
return $this->classLoader[0]->findFile($class);
Expand Down Expand Up @@ -161,7 +172,8 @@ public function loadClass($class)

ErrorHandler::unstackErrors();

$exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
$exists = class_exists($class, false) || interface_exists($class,
false) || (function_exists('trait_exists') && trait_exists($class, false));

if ('\\' === $class[0]) {
$class = substr($class, 1);
Expand All @@ -172,11 +184,13 @@ public function loadClass($class)
$name = $refl->getName();

if ($name !== $class && 0 === strcasecmp($name, $class)) {
throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s',
$class, $name));
}

if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
@trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
@trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name,
$refl->getShortName()), E_USER_DEPRECATED);
} elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
} else {
Expand All @@ -197,12 +211,17 @@ public function loadClass($class)

if (!$parent || strncmp($ns, $parent, $len)) {
if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
@trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
@trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent,
self::$deprecated[$parent]), E_USER_DEPRECATED);
}

foreach (class_implements($class) as $interface) {
if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !is_subclass_of($parent, $interface)) {
@trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, interface_exists($class) ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface,
$len) && !is_subclass_of($parent, $interface)
) {
@trigger_error(sprintf('The %s %s %s that is deprecated %s', $name,
interface_exists($class) ? 'interface extends' : 'class implements', $interface,
self::$deprecated[$interface]), E_USER_DEPRECATED);
}
}
}
Expand All @@ -212,13 +231,15 @@ public function loadClass($class)
if ($file) {
if (!$exists) {
if (false !== strpos($class, '/')) {
throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".',
$class));
}

throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.',
$class, $file));
}
if (self::$caseCheck) {
$real = explode('\\', $class.strrchr($file, '.'));
$real = explode('\\', $class . strrchr($file, '.'));
$tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));

$i = count($tail) - 1;
Expand All @@ -232,7 +253,7 @@ public function loadClass($class)
array_splice($tail, 0, $i + 1);
}
if (self::$caseCheck && $tail) {
$tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail);
$tail = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $tail);
$tailLen = strlen($tail);
$real = $refl->getFileName();

Expand All @@ -253,14 +274,14 @@ public function loadClass($class)
} else {
$dir = getcwd();
chdir($real);
$real = getcwd().'/';
$real = getcwd() . '/';
chdir($dir);

$dir = $real;
$k = $kDir;
$i = strlen($dir) - 1;
while (!isset(self::$darwinCache[$k])) {
self::$darwinCache[$k] = array($dir, array());
self::$darwinCache[$k] = [$dir, []];
self::$darwinCache[$dir] = &self::$darwinCache[$k];

while ('/' !== $dir[--$i]) {
Expand Down Expand Up @@ -297,9 +318,10 @@ public function loadClass($class)
}

if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
&& 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
&& 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
) {
throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s',
substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
}
}

Expand Down