Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 042be41

Browse files
committed
merged branch fabpot/debug-classloader (PR #8870)
This PR was merged into the master branch. Discussion ---------- duplicated the DebugClassLoader in the Debug component | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This is a follow-up for #8553. Commits ------- d146461 duplicated the DebugClassLoader in the Debug component
2 parents fc15c70 + d146461 commit 042be41

File tree

7 files changed

+163
-11
lines changed

7 files changed

+163
-11
lines changed

src/Symfony/Component/ClassLoader/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.4.0
5+
-----
6+
7+
* deprecated the DebugClassLoader as it has been moved to the Debug component instead
8+
49
2.3.0
510
-----
611

src/Symfony/Component/ClassLoader/DebugClassLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* @author Christophe Coevoet <[email protected]>
2323
*
2424
* @api
25+
*
26+
* @deprecated Deprecated since version 2.4, to be removed in 3.0. Use the DebugClassLoader provided by the Debug component instead.
2527
*/
2628
class DebugClassLoader
2729
{

src/Symfony/Component/Debug/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.4.0
55
-----
66

7+
* added a DebugClassLoader able to wrap any autoloader providing a findFile method
78
* improved error messages for not found classes and functions
89

910
2.3.0

src/Symfony/Component/Debug/Debug.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Debug;
1313

14-
use Symfony\Component\ClassLoader\DebugClassLoader;
14+
use Symfony\Component\Debug\DebugClassLoader;
1515

1616
/**
1717
* Registers all the debug tools.
@@ -51,8 +51,6 @@ public static function enable($errorReportingLevel = null, $displayErrors = true
5151
ini_set('display_errors', 1);
5252
}
5353

54-
if (class_exists('Symfony\Component\ClassLoader\DebugClassLoader')) {
55-
DebugClassLoader::enable();
56-
}
54+
DebugClassLoader::enable();
5755
}
5856
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Debug;
13+
14+
/**
15+
* Autoloader checking if the class is really defined in the file found.
16+
*
17+
* The ClassLoader will wrap all registered autoloaders providing a
18+
* findFile method and will throw an exception if a file is found but does
19+
* not declare the class.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
* @author Christophe Coevoet <[email protected]>
23+
*
24+
* @api
25+
*/
26+
class DebugClassLoader
27+
{
28+
private $classFinder;
29+
30+
/**
31+
* Constructor.
32+
*
33+
* @param object $classFinder
34+
*
35+
* @api
36+
*/
37+
public function __construct($classFinder)
38+
{
39+
$this->classFinder = $classFinder;
40+
}
41+
42+
/**
43+
* Gets the wrapped class loader.
44+
*
45+
* @return object a class loader instance
46+
*/
47+
public function getClassLoader()
48+
{
49+
return $this->classFinder;
50+
}
51+
52+
/**
53+
* Replaces all autoloaders implementing a findFile method by a DebugClassLoader wrapper.
54+
*/
55+
public static function enable()
56+
{
57+
if (!is_array($functions = spl_autoload_functions())) {
58+
return;
59+
}
60+
61+
foreach ($functions as $function) {
62+
spl_autoload_unregister($function);
63+
}
64+
65+
foreach ($functions as $function) {
66+
if (is_array($function) && !$function[0] instanceof self && method_exists($function[0], 'findFile')) {
67+
$function = array(new static($function[0]), 'loadClass');
68+
}
69+
70+
spl_autoload_register($function);
71+
}
72+
}
73+
74+
/**
75+
* Disables the wrapping.
76+
*/
77+
public static function disable()
78+
{
79+
if (!is_array($functions = spl_autoload_functions())) {
80+
return;
81+
}
82+
83+
foreach ($functions as $function) {
84+
spl_autoload_unregister($function);
85+
}
86+
87+
foreach ($functions as $function) {
88+
if (is_array($function) && $function[0] instanceof self) {
89+
$function[0] = $function[0]->getClassLoader();
90+
}
91+
92+
spl_autoload_register($function);
93+
}
94+
}
95+
96+
/**
97+
* Finds a file by class name
98+
*
99+
* @param string $class A class name to resolve to file
100+
*
101+
* @return string|null
102+
*/
103+
public function findFile($class)
104+
{
105+
return $this->classFinder->findFile($class);
106+
}
107+
108+
/**
109+
* Loads the given class or interface.
110+
*
111+
* @param string $class The name of the class
112+
*
113+
* @return Boolean|null True, if loaded
114+
*
115+
* @throws \RuntimeException
116+
*/
117+
public function loadClass($class)
118+
{
119+
if ($file = $this->classFinder->findFile($class)) {
120+
require $file;
121+
122+
if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
123+
if (false !== strpos($class, '/')) {
124+
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));
125+
}
126+
127+
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));
128+
}
129+
130+
return true;
131+
}
132+
}
133+
}

src/Symfony/Component/ClassLoader/Tests/DebugClassLoaderTest.php renamed to src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\ClassLoader\Tests;
12+
namespace Symfony\Component\Debug\Tests;
1313

14-
use Symfony\Component\ClassLoader\ClassLoader;
15-
use Symfony\Component\ClassLoader\DebugClassLoader;
14+
use Symfony\Component\Debug\DebugClassLoader;
1615

1716
class DebugClassLoaderTest extends \PHPUnit_Framework_TestCase
1817
{
@@ -41,12 +40,27 @@ public function testIdempotence()
4140
$reflProp = $reflClass->getProperty('classFinder');
4241
$reflProp->setAccessible(true);
4342

44-
$this->assertNotInstanceOf('Symfony\Component\ClassLoader\DebugClassLoader', $reflProp->getValue($function[0]));
43+
$this->assertNotInstanceOf('Symfony\Component\Debug\DebugClassLoader', $reflProp->getValue($function[0]));
44+
45+
DebugClassLoader::disable();
4546

4647
return;
4748
}
4849
}
4950

50-
throw new \Exception('DebugClassLoader did not register');
51+
DebugClassLoader::disable();
52+
53+
$this->fail('DebugClassLoader did not register');
54+
}
55+
}
56+
57+
class ClassLoader
58+
{
59+
public function loadClass($class)
60+
{
61+
}
62+
63+
public function findFile($class)
64+
{
5165
}
5266
}

src/Symfony/Component/Debug/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
},
2525
"suggest": {
2626
"symfony/http-foundation": "",
27-
"symfony/http-kernel": "",
28-
"symfony/class-loader": ""
27+
"symfony/http-kernel": ""
2928
},
3029
"autoload": {
3130
"psr-0": { "Symfony\\Component\\Debug\\": "" }

0 commit comments

Comments
 (0)