bug #39018 [FrameworkBundle] Fixed validation of php_errors.log (lyrixx)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[FrameworkBundle] Fixed validation of php_errors.log

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

It's actually possible to pass a map or PHP errors => Log level like
following

```yaml
    php_errors:
        log:
            !php/const \E_DEPRECATED: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_DEPRECATED: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_NOTICE: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_NOTICE: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_STRICT: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_COMPILE_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_CORE_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_RECOVERABLE_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_COMPILE_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_PARSE: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_CORE_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
```

Commits
-------

45519f2e9e [FrameworkBundle] Added support for configuring PHP error level to log level
This commit is contained in:
Fabien Potencier 2020-12-05 17:36:24 +01:00
commit e6eef9b5b7
7 changed files with 75 additions and 4 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
5.3.0
-----
* Added support for configuring PHP error level to log levels
5.2.0
-----

View File

@ -1070,14 +1070,31 @@ class Configuration implements ConfigurationInterface
->info('PHP errors handling configuration')
->addDefaultsIfNotSet()
->children()
->scalarNode('log')
->variableNode('log')
->info('Use the application logger instead of the PHP logger for logging PHP errors.')
->example('"true" to use the default configuration: log all errors. "false" to disable. An integer bit field of E_* constants.')
->example('"true" to use the default configuration: log all errors. "false" to disable. An integer bit field of E_* constants, or an array mapping E_* constants to log levels.')
->defaultValue($this->debug)
->treatNullLike($this->debug)
->beforeNormalization()
->ifArray()
->then(function (array $v): array {
if (!($v[0]['type'] ?? false)) {
return $v;
}
// Fix XML normalization
$ret = [];
foreach ($v as ['type' => $type, 'logLevel' => $logLevel]) {
$ret[$type] = $logLevel;
}
return $ret;
})
->end()
->validate()
->ifTrue(function ($v) { return !(\is_int($v) || \is_bool($v)); })
->thenInvalid('The "php_errors.log" parameter should be either an integer or a boolean.')
->ifTrue(function ($v) { return !(\is_int($v) || \is_bool($v) || \is_array($v)); })
->thenInvalid('The "php_errors.log" parameter should be either an integer, a boolean, or an array')
->end()
->end()
->booleanNode('throw')

View File

@ -313,10 +313,18 @@
</xsd:complexType>
<xsd:complexType name="php-errors">
<xsd:sequence>
<xsd:element name="log" type="logLevel" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="log" type="xsd:string" />
<xsd:attribute name="throw" type="xsd:boolean" />
</xsd:complexType>
<xsd:complexType name="logLevel" mixed="true">
<xsd:attribute name="type" type="xsd:string" use="required" />
<xsd:attribute name="logLevel" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="marking_store">
<xsd:sequence>
<xsd:element name="argument" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />

View File

@ -0,0 +1,10 @@
<?php
$container->loadFromExtension('framework', [
'php_errors' => [
'log' => [
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
\E_WARNING => \Psr\Log\LogLevel::ERROR,
]
],
]);

View File

@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:php-errors>
<framework:log type="8" logLevel="error" />
<framework:log type="2" logLevel="error" />
</framework:php-errors>
</framework:config>
</container>

View File

@ -0,0 +1,5 @@
framework:
php_errors:
log:
!php/const \E_NOTICE: !php/const Psr\Log\LogLevel::ERROR
!php/const \E_WARNING: !php/const Psr\Log\LogLevel::ERROR

View File

@ -498,6 +498,18 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertSame(8, $definition->getArgument(2));
}
public function testPhpErrorsWithLogLevels()
{
$container = $this->createContainerFromFile('php_errors_log_levels');
$definition = $container->getDefinition('debug.debug_handlers_listener');
$this->assertEquals(new Reference('monolog.logger.php', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertSame([
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
\E_WARNING => \Psr\Log\LogLevel::ERROR,
], $definition->getArgument(2));
}
public function testRouter()
{
$container = $this->createContainerFromFile('full');