Skip to content

Commit ae3b128

Browse files
author
Henrik Bjørnskov
committed
[ClassLoader] Support for autoloading include_path incl. tests.
1 parent a746055 commit ae3b128

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static public function enable()
3838
$loader->registerPrefixFallbacks($function[0]->getPrefixFallbacks());
3939
$loader->registerNamespaces($function[0]->getNamespaces());
4040
$loader->registerPrefixes($function[0]->getPrefixes());
41+
$loader->useIncludePath($function[0]->getUseIncludePath());
4142

4243
$function[0] = $loader;
4344
}

src/Symfony/Component/ClassLoader/UniversalClassLoader.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
* 'Swift_' => __DIR__.'/Swift',
4242
* ));
4343
*
44+
*
45+
* // to enable searching the include path (eg. for PEAR packages)
46+
* $loader->useIncludePath(true);
47+
*
4448
* // activate the autoloader
4549
* $loader->register();
4650
*
@@ -60,6 +64,29 @@ class UniversalClassLoader
6064
private $prefixes = array();
6165
private $namespaceFallbacks = array();
6266
private $prefixFallbacks = array();
67+
private $useIncludePath = false;
68+
69+
/**
70+
* Turns on searching the include for class files. Allows easy loading
71+
* of installed PEAR packages
72+
*
73+
* @param Boolean $useIncludePath
74+
*/
75+
public function useIncludePath($useIncludePath)
76+
{
77+
$this->useIncludePath = $useIncludePath;
78+
}
79+
80+
/**
81+
* Can be used to check if the autoloader uses the include path to check
82+
* for classes.
83+
*
84+
* @return Boolean
85+
*/
86+
public function getUseIncludePath()
87+
{
88+
return $this->useIncludePath;
89+
}
6390

6491
/**
6592
* Gets the configured namespaces.
@@ -219,47 +246,54 @@ public function findFile($class)
219246
if (false !== $pos = strrpos($class, '\\')) {
220247
// namespaced class name
221248
$namespace = substr($class, 0, $pos);
249+
$className = substr($class, $pos + 1);
250+
$normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
222251
foreach ($this->namespaces as $ns => $dirs) {
223252
if (0 !== strpos($namespace, $ns)) {
224253
continue;
225254
}
226255

227256
foreach ($dirs as $dir) {
228-
$className = substr($class, $pos + 1);
229-
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
257+
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
230258
if (file_exists($file)) {
231259
return $file;
232260
}
233261
}
234262
}
235263

236264
foreach ($this->namespaceFallbacks as $dir) {
237-
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
265+
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
238266
if (file_exists($file)) {
239267
return $file;
240268
}
241269
}
270+
242271
} else {
243272
// PEAR-like class name
273+
$normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
244274
foreach ($this->prefixes as $prefix => $dirs) {
245275
if (0 !== strpos($class, $prefix)) {
246276
continue;
247277
}
248278

249279
foreach ($dirs as $dir) {
250-
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
280+
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
251281
if (file_exists($file)) {
252282
return $file;
253283
}
254284
}
255285
}
256286

257287
foreach ($this->prefixFallbacks as $dir) {
258-
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
288+
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
259289
if (file_exists($file)) {
260290
return $file;
261291
}
262292
}
263293
}
294+
295+
if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
296+
return $file;
297+
}
264298
}
265299
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
}

tests/Symfony/Tests/Component/ClassLoader/UniversalClassLoaderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ public function getLoadClassTests()
3737
);
3838
}
3939

40+
public function testUseIncludePath()
41+
{
42+
$loader = new UniversalClassLoader();
43+
$this->assertFalse($loader->getUseIncludePath());
44+
45+
$this->assertEquals(null, $loader->findFile('Foo'));
46+
47+
$includePath = get_include_path();
48+
49+
$loader->useIncludePath(true);
50+
$this->assertTrue($loader->getUseIncludePath());
51+
52+
set_include_path(__DIR__.'/Fixtures/includepath' . PATH_SEPARATOR . $includePath);
53+
54+
$this->assertEquals(__DIR__.'/Fixtures/includepath/Foo.php', $loader->findFile('Foo'));
55+
56+
set_include_path($includePath);
57+
}
58+
4059
/**
4160
* @dataProvider getLoadClassFromFallbackTests
4261
*/

0 commit comments

Comments
 (0)