Skip to content

Commit

Permalink
made the charset overridable (closes #2072)
Browse files Browse the repository at this point in the history
The charset was configurable in a configuration file but it never worked:

    framework:
        charset: ISO-8859-1

Now, like for the cache and log dirs, you can configure the charset by
overriding the getCharset() method in the app kernel:

    public function getCharset()
    {
        return 'ISO-8859-1';
    }
  • Loading branch information
fabpot committed Jul 3, 2012
1 parent 81fe2ff commit d9439ab
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 7 deletions.
Expand Up @@ -46,7 +46,21 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('charset')->info('general configuration')->end()
->scalarNode('charset')
->defaultNull()
->beforeNormalization()
->ifTrue(function($v) { return null !== $v; })
->then(function($v) {
$message = 'The charset setting is deprecated. Just remove it from your configuration file.';

if ('UTF-8' !== $v) {
$messages .= sprintf(' You need to define a getCharset() method in your Application Kernel class that returns "%s".', $v);
}

throw new \RuntimeException($message);
})
->end()
->end()
->scalarNode('trust_proxy_headers')->defaultFalse()->end()
->scalarNode('secret')->isRequired()->end()
->scalarNode('ide')->defaultNull()->end()
Expand Down
Expand Up @@ -60,9 +60,6 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

if (isset($config['charset'])) {
$container->setParameter('kernel.charset', $config['charset']);
}
$container->setParameter('kernel.secret', $config['secret']);

$container->setParameter('kernel.trust_proxy_headers', $config['trust_proxy_headers']);
Expand Down
Expand Up @@ -21,6 +21,7 @@
<xsd:element name="annotations" type="annotations" minOccurs="0" maxOccurs="1" />
</xsd:all>

<!-- charset is deprecated and will be removed in 2.2 -->
<xsd:attribute name="charset" type="xsd:string" />
<xsd:attribute name="trust-proxy-headers" type="xsd:string" />
<xsd:attribute name="ide" type="xsd:string" />
Expand Down
@@ -1,5 +1,4 @@
framework:
charset: UTF-8
secret: test
csrf_protection:
enabled: true
Expand Down
@@ -1,5 +1,4 @@
framework:
charset: UTF-8
secret: test
csrf_protection:
enabled: true
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
2.1.0
-----

* [BC BREAK] the charset is now configured via the Kernel::getCharset() method
* [BC BREAK] the current locale for the user is not stored anymore in the session
* added the HTTP method to the profiler storage
* updated all listeners to implement EventSubscriberInterface
Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -467,6 +467,18 @@ public function getLogDir()
return $this->rootDir.'/logs';
}

/**
* Gets the charset of the application.
*
* @return string The charset
*
* @api
*/
public function getCharset()
{
return 'UTF-8';
}

/**
* Initializes the data structures related to the bundle management.
*
Expand Down Expand Up @@ -601,7 +613,7 @@ protected function getKernelParameters()
'kernel.cache_dir' => $this->getCacheDir(),
'kernel.logs_dir' => $this->getLogDir(),
'kernel.bundles' => $bundles,
'kernel.charset' => 'UTF-8',
'kernel.charset' => $this->getCharset(),
'kernel.container_class' => $this->getContainerClass(),
),
$this->getEnvParameters()
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpKernel/KernelInterface.php
Expand Up @@ -196,4 +196,13 @@ function getCacheDir();
* @api
*/
function getLogDir();

/**
* Gets the charset of the application.
*
* @return string The charset
*
* @api
*/
function getCharset();
}

0 comments on commit d9439ab

Please sign in to comment.