Skip to content
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

Dependency Injection: fix custom loader on Windows #16364

Merged
merged 3 commits into from
Nov 19, 2020
Merged
Changes from 2 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
27 changes: 21 additions & 6 deletions config/dependency-injection/custom-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ private function getClassFromClassMap( $path ) {

if ( ! $class_map ) {
$class_map = require __DIR__ . '/../../vendor/composer/autoload_classmap.php';
$class_map = \array_map( [ $this, 'normalize_slashes' ], $class_map );
$class_map = \array_flip( $class_map );
}

foreach ( $class_map as $class => $class_path ) {
if ( $path === $class_path ) {
return $class;
}
if ( isset( $class_map[ $path ] ) ) {
return $class_map[ $path ];
}

return false;
Expand Down Expand Up @@ -124,7 +124,8 @@ private function findClasses( $namespace, $pattern, $exclude ) {
}

// Normalize Windows slashes.
$exclude_paths[ \str_replace( '\\', '/', $path ) ] = true;
$path = $this->normalize_slashes( $path );
$exclude_paths[ $path ] = true;
}
}

Expand All @@ -141,7 +142,10 @@ private function findClasses( $namespace, $pattern, $exclude ) {
}
}

if ( isset( $exclude_paths[ \str_replace( '\\', '/', $path ) ] ) ) {
// Normalize Windows slashes.
$path = $this->normalize_slashes( $path );

if ( isset( $exclude_paths[ $path ] ) ) {
continue;
}

Expand Down Expand Up @@ -187,4 +191,15 @@ private function findClasses( $namespace, $pattern, $exclude ) {

return $classes;
}

/**
* Normalize all slashes in a file path to forward slashes.
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $path File path.
*
* @return string
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
*/
private function normalize_slashes( $path ) {
return \str_replace( '\\', '/', $path );
}
}