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

Skip to content

Commit 6d552c9

Browse files
committed
merged branch fabpot/debug-component (PR #7441)
This PR was merged into the master branch. Discussion ---------- [Debug] added the component (closes #6828, closes #6834, closes #7330) | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #6828, #6834, #7330 | License | MIT | Doc PR | symfony/symfony-docs#2479 You can use the individual tools, or register them all: ```php use Symfony\Component\Debug\Debug; Debug::enable(); ``` Changes in Symfony SE: symfony/symfony-standard#523 Commits ------- f693128 fixed typos 1ab1146 [Debug] fixed minor bugs daa3a3c [Debug] changed composer to accept more versions e455269 [Debug] ensured that the Debug tools can only be registered once 946bfb2 [Debug] made the exception handler independant of HttpFoundation 2b305c2 added a main Debug class to ease integration 2ff0927 [Debug] added the component (closes #6828, closes #6834, closes #7330)
2 parents 22adfdf + f693128 commit 6d552c9

21 files changed

+978
-649
lines changed

UPGRADE-3.0.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ UPGRADE FROM 2.x to 3.0
33

44
### ClassLoader
55

6-
* The `UniversalClassLoader` class has been removed in favor of `ClassLoader`. The only difference is that some method
7-
names are different:
6+
* The `UniversalClassLoader` class has been removed in favor of
7+
`ClassLoader`. The only difference is that some method names are different:
88

99
* `registerNamespaces()` -> `addPrefixes()`
1010
* `registerPrefixes()` -> `addPrefixes()`
@@ -34,6 +34,16 @@ UPGRADE FROM 2.x to 3.0
3434
* `Symfony\Bridge\Monolog\Logger`
3535
* `Symfony\Component\HttpKernel\Log\NullLogger`
3636

37+
* The `Symfony\Component\HttpKernel\Kernel::init()` method has been removed.
38+
39+
* The following classes have been renamed as they have been moved to the
40+
Debug component:
41+
42+
* `Symfony\Component\HttpKernel\Debug\ErrorHandler` -> `Symfony\Component\Debug\ErrorHandler`
43+
* `Symfony\Component\HttpKernel\Debug\ExceptionHandler` -> `Symfony\Component\Debug\ExceptionHandler`
44+
* `Symfony\Component\HttpKernel\Exception\FatalErrorException` -> `Symfony\Component\Debug\Exception\FatalErrorException`
45+
* `Symfony\Component\HttpKernel\Exception\FlattenException` -> `Symfony\Component\Debug\Exception\FlattenException`
46+
3747
### Routing
3848

3949
* Some route settings have been renamed:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
2.3.0
5+
-----
6+
7+
* added the component

src/Symfony/Component/Debug/Debug.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
use Symfony\Component\ClassLoader\DebugClassLoader;
15+
16+
/**
17+
* Registers all the debug tools.
18+
*
19+
* @author Fabien Potencier <[email protected]>
20+
*/
21+
class Debug
22+
{
23+
private static $enabled = false;
24+
25+
/**
26+
* Enables the debug tools.
27+
*
28+
* This method registers an error handler and an exception handler.
29+
*
30+
* If the Symfony ClassLoader component is available, a special
31+
* class loader is also registered.
32+
*
33+
* @param integer $errorReportingLevel The level of error reporting you want
34+
*/
35+
public static function enable($errorReportingLevel = null)
36+
{
37+
if (static::$enabled) {
38+
return;
39+
}
40+
41+
static::$enabled = true;
42+
43+
error_reporting(-1);
44+
45+
ErrorHandler::register($errorReportingLevel);
46+
if ('cli' !== php_sapi_name()) {
47+
ExceptionHandler::register();
48+
} elseif (!ini_get('log_errors') || ini_get('error_log')) {
49+
ini_set('display_errors', 1);
50+
}
51+
52+
if (class_exists('Symfony\Component\ClassLoader\DebugClassLoader')) {
53+
DebugClassLoader::enable();
54+
}
55+
}
56+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
use Symfony\Component\Debug\Exception\FatalErrorException;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* ErrorHandler.
19+
*
20+
* @author Fabien Potencier <[email protected]>
21+
*/
22+
class ErrorHandler
23+
{
24+
const TYPE_DEPRECATION = -100;
25+
26+
private $levels = array(
27+
E_WARNING => 'Warning',
28+
E_NOTICE => 'Notice',
29+
E_USER_ERROR => 'User Error',
30+
E_USER_WARNING => 'User Warning',
31+
E_USER_NOTICE => 'User Notice',
32+
E_STRICT => 'Runtime Notice',
33+
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
34+
E_DEPRECATED => 'Deprecated',
35+
E_USER_DEPRECATED => 'User Deprecated',
36+
E_ERROR => 'Error',
37+
E_CORE_ERROR => 'Core Error',
38+
E_COMPILE_ERROR => 'Compile Error',
39+
E_PARSE => 'Parse',
40+
);
41+
42+
private $level;
43+
44+
private $reservedMemory;
45+
46+
/** @var LoggerInterface */
47+
private static $logger;
48+
49+
/**
50+
* Registers the error handler.
51+
*
52+
* @param integer $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
53+
*
54+
* @return The registered error handler
55+
*/
56+
public static function register($level = null)
57+
{
58+
$handler = new static();
59+
$handler->setLevel($level);
60+
61+
ini_set('display_errors', 0);
62+
set_error_handler(array($handler, 'handle'));
63+
register_shutdown_function(array($handler, 'handleFatal'));
64+
$handler->reservedMemory = str_repeat('x', 10240);
65+
66+
return $handler;
67+
}
68+
69+
public function setLevel($level)
70+
{
71+
$this->level = null === $level ? error_reporting() : $level;
72+
}
73+
74+
public static function setLogger(LoggerInterface $logger)
75+
{
76+
self::$logger = $logger;
77+
}
78+
79+
/**
80+
* @throws \ErrorException When error_reporting returns error
81+
*/
82+
public function handle($level, $message, $file, $line, $context)
83+
{
84+
if (0 === $this->level) {
85+
return false;
86+
}
87+
88+
if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) {
89+
if (null !== self::$logger) {
90+
$stack = version_compare(PHP_VERSION, '5.4', '<') ? array_slice(debug_backtrace(false), 0, 10) : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
91+
92+
self::$logger->warning($message, array('type' => self::TYPE_DEPRECATION, 'stack' => $stack));
93+
}
94+
95+
return true;
96+
}
97+
98+
if (error_reporting() & $level && $this->level & $level) {
99+
throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line), 0, $level, $file, $line);
100+
}
101+
102+
return false;
103+
}
104+
105+
public function handleFatal()
106+
{
107+
if (null === $error = error_get_last()) {
108+
return;
109+
}
110+
111+
unset($this->reservedMemory);
112+
$type = $error['type'];
113+
if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
114+
return;
115+
}
116+
117+
// get current exception handler
118+
$exceptionHandler = set_exception_handler(function() {});
119+
restore_exception_handler();
120+
121+
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
122+
$level = isset($this->levels[$type]) ? $this->levels[$type] : $type;
123+
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);
124+
$exception = new FatalErrorException($message, 0, $type, $error['file'], $error['line']);
125+
$exceptionHandler[0]->handle($exception);
126+
}
127+
}
128+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
13+
14+
/**
15+
* Fatal Error Exception.
16+
*
17+
* @author Konstanton Myakshin <[email protected]>
18+
*/
19+
class FatalErrorException extends \ErrorException
20+
{
21+
}

0 commit comments

Comments
 (0)