From e74223470a95f370f99f8b5455941c30264f59b4 Mon Sep 17 00:00:00 2001 From: Joe Holdcroft Date: Wed, 14 Nov 2012 10:34:19 +0000 Subject: [PATCH 1/4] - Adding prefix option to ClassLoader::add() - Adding set method to ClassLoader for overwriting --- src/Composer/Autoload/ClassLoader.php | 41 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index a4a0dc5a6..af2bdf933 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -75,12 +75,13 @@ class ClassLoader } /** - * Registers a set of classes + * Registers a set of classes, merging with any others previously set. * - * @param string $prefix The classes prefix - * @param array|string $paths The location(s) of the classes + * @param string $prefix The classes prefix + * @param array|string $paths The location(s) of the classes + * @param bool $prepend Prepend the location(s) */ - public function add($prefix, $paths) + public function add($prefix, $paths, $prepend = false) { if (!$prefix) { foreach ((array) $paths as $path) { @@ -90,15 +91,39 @@ class ClassLoader return; } if (isset($this->prefixes[$prefix])) { - $this->prefixes[$prefix] = array_merge( - $this->prefixes[$prefix], - (array) $paths - ); + if ($prepend) { + $this->prefixes[$prefix] = array_merge( + (array) $paths, + $this->prefixes[$prefix] + ); + } + else { + $this->prefixes[$prefix] = array_merge( + $this->prefixes[$prefix], + (array) $paths + ); + } } else { $this->prefixes[$prefix] = (array) $paths; } } + /** + * Registers a set of classes, replacing any others previously set. + * + * @param string $prefix The classes prefix + * @param array|string $paths The location(s) of the classes + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirs = (array) $path; + + return; + } + $this->prefixes[$prefix] = (array) $paths; + } + /** * Turns on searching the include path for class files. * From 6510ee5c4c55b33e943393d7727eae37c457e010 Mon Sep 17 00:00:00 2001 From: Joe Holdcroft Date: Wed, 14 Nov 2012 11:41:08 +0000 Subject: [PATCH 2/4] Refactoring ClassLoader::add() to return early --- src/Composer/Autoload/ClassLoader.php | 29 ++++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index af2bdf933..1b4823742 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -90,21 +90,22 @@ class ClassLoader return; } - if (isset($this->prefixes[$prefix])) { - if ($prepend) { - $this->prefixes[$prefix] = array_merge( - (array) $paths, - $this->prefixes[$prefix] - ); - } - else { - $this->prefixes[$prefix] = array_merge( - $this->prefixes[$prefix], - (array) $paths - ); - } - } else { + if (!isset($this->prefixes[$prefix])) { $this->prefixes[$prefix] = (array) $paths; + + return; + } + if ($prepend) { + $this->prefixes[$prefix] = array_merge( + (array) $paths, + $this->prefixes[$prefix] + ); + } + else { + $this->prefixes[$prefix] = array_merge( + $this->prefixes[$prefix], + (array) $paths + ); } } From 538cdc914bcea471c96bd438b2beaa3b0a311506 Mon Sep 17 00:00:00 2001 From: Joe Holdcroft Date: Wed, 14 Nov 2012 11:46:49 +0000 Subject: [PATCH 3/4] Making prepend work with fallbacks & coding standards --- src/Composer/Autoload/ClassLoader.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index 1b4823742..9c039d4ea 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -85,7 +85,11 @@ class ClassLoader { if (!$prefix) { foreach ((array) $paths as $path) { - $this->fallbackDirs[] = $path; + if ($prepend) { + array_unshift($this->fallbackDirs, $path); + } else { + $this->fallbackDirs[] = $path; + } } return; @@ -100,8 +104,7 @@ class ClassLoader (array) $paths, $this->prefixes[$prefix] ); - } - else { + } else { $this->prefixes[$prefix] = array_merge( $this->prefixes[$prefix], (array) $paths From 7d5e4f76fbc072283a0c8e80cffb77a31149a811 Mon Sep 17 00:00:00 2001 From: Joe Holdcroft Date: Thu, 17 Jan 2013 14:12:03 +0000 Subject: [PATCH 4/4] Bug fix & changing loop + array_unshift to array_merge --- src/Composer/Autoload/ClassLoader.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index 9c039d4ea..db39073eb 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -84,12 +84,17 @@ class ClassLoader public function add($prefix, $paths, $prepend = false) { if (!$prefix) { - foreach ((array) $paths as $path) { - if ($prepend) { - array_unshift($this->fallbackDirs, $path); - } else { - $this->fallbackDirs[] = $path; - } + if ($prepend) { + $this->fallbackDirs = array_merge( + (array) $paths, + $this->fallbackDirs + ); + } + else { + $this->fallbackDirs = array_merge( + $this->fallbackDirs, + (array) $paths + ); } return; @@ -121,7 +126,7 @@ class ClassLoader public function set($prefix, $paths) { if (!$prefix) { - $this->fallbackDirs = (array) $path; + $this->fallbackDirs = (array) $paths; return; }