2010-09-27 08:45:29 +01:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2010-09-27 08:45:29 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-09-27 08:45:29 +01:00
*
2011-01-15 13:29:43 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
2010-09-27 08:45:29 +01:00
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\Translation ;
2011-02-10 15:15:51 +00:00
use Symfony\Component\Config\Resource\ResourceInterface ;
2016-09-21 18:16:29 +01:00
use Symfony\Component\Translation\Exception\LogicException ;
2011-01-15 13:29:43 +00:00
2010-09-27 08:45:29 +01:00
/**
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2010-09-27 08:45:29 +01:00
*/
2012-12-14 12:37:57 +00:00
class MessageCatalogue implements MessageCatalogueInterface , MetadataAwareInterface
2010-09-27 08:45:29 +01:00
{
2011-03-08 19:47:32 +00:00
private $messages = array ();
2012-12-14 12:35:59 +00:00
private $metadata = array ();
2013-02-13 00:53:15 +00:00
private $resources = array ();
2011-03-08 19:47:32 +00:00
private $locale ;
2011-08-22 15:07:39 +01:00
private $fallbackCatalogue ;
2011-11-11 07:29:16 +00:00
private $parent ;
2010-09-27 08:45:29 +01:00
/**
* @ param string $locale The locale
* @ param array $messages An array of messages classified by domain
*/
2017-10-28 19:15:32 +01:00
public function __construct ( ? string $locale , array $messages = array ())
2010-09-27 08:45:29 +01:00
{
$this -> locale = $locale ;
$this -> messages = $messages ;
}
/**
* { @ inheritdoc }
*/
public function getLocale ()
{
return $this -> locale ;
}
/**
* { @ inheritdoc }
*/
public function getDomains ()
{
2018-11-14 13:56:01 +00:00
$domains = array ();
$suffixLength = \strlen ( self :: INTL_DOMAIN_SUFFIX );
foreach ( $this -> messages as $domain => $messages ) {
if ( \strlen ( $domain ) > $suffixLength && false !== $i = strpos ( $domain , self :: INTL_DOMAIN_SUFFIX , - $suffixLength )) {
$domain = substr ( $domain , 0 , $i );
}
$domains [ $domain ] = $domain ;
}
return array_values ( $domains );
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
2010-11-23 08:42:19 +00:00
public function all ( $domain = null )
2010-09-27 08:45:29 +01:00
{
2018-11-14 13:56:01 +00:00
if ( null !== $domain ) {
return ( $this -> messages [ $domain . self :: INTL_DOMAIN_SUFFIX ] ? ? array ()) + ( $this -> messages [ $domain ] ? ? array ());
}
$allMessages = array ();
$suffixLength = \strlen ( self :: INTL_DOMAIN_SUFFIX );
foreach ( $this -> messages as $domain => $messages ) {
if ( \strlen ( $domain ) > $suffixLength && false !== $i = strpos ( $domain , self :: INTL_DOMAIN_SUFFIX , - $suffixLength )) {
$domain = substr ( $domain , 0 , $i );
$allMessages [ $domain ] = $messages + ( $allMessages [ $domain ] ? ? array ());
} else {
$allMessages [ $domain ] = ( $allMessages [ $domain ] ? ? array ()) + $messages ;
}
2010-09-27 08:45:29 +01:00
}
2018-11-14 13:56:01 +00:00
return $allMessages ;
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
2010-11-23 08:42:19 +00:00
public function set ( $id , $translation , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2010-11-23 08:42:19 +00:00
$this -> add ( array ( $id => $translation ), $domain );
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
2010-11-23 08:42:19 +00:00
public function has ( $id , $domain = 'messages' )
2011-09-14 21:29:44 +01:00
{
2018-11-14 13:56:01 +00:00
if ( isset ( $this -> messages [ $domain ][ $id ]) || isset ( $this -> messages [ $domain . self :: INTL_DOMAIN_SUFFIX ][ $id ])) {
2011-09-14 21:29:44 +01:00
return true ;
}
if ( null !== $this -> fallbackCatalogue ) {
return $this -> fallbackCatalogue -> has ( $id , $domain );
}
return false ;
}
/**
* { @ inheritdoc }
*/
2011-09-14 21:49:04 +01:00
public function defines ( $id , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2018-11-14 13:56:01 +00:00
return isset ( $this -> messages [ $domain ][ $id ]) || isset ( $this -> messages [ $domain . self :: INTL_DOMAIN_SUFFIX ][ $id ]);
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
2010-11-23 08:42:19 +00:00
public function get ( $id , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2018-11-14 13:56:01 +00:00
if ( isset ( $this -> messages [ $domain . self :: INTL_DOMAIN_SUFFIX ][ $id ])) {
return $this -> messages [ $domain . self :: INTL_DOMAIN_SUFFIX ][ $id ];
}
2011-08-22 15:07:39 +01:00
if ( isset ( $this -> messages [ $domain ][ $id ])) {
return $this -> messages [ $domain ][ $id ];
}
if ( null !== $this -> fallbackCatalogue ) {
return $this -> fallbackCatalogue -> get ( $id , $domain );
}
return $id ;
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
2010-11-23 08:42:19 +00:00
public function replace ( $messages , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2018-11-14 13:56:01 +00:00
unset ( $this -> messages [ $domain ], $this -> messages [ $domain . self :: INTL_DOMAIN_SUFFIX ]);
2010-09-27 08:45:29 +01:00
2010-11-23 08:42:19 +00:00
$this -> add ( $messages , $domain );
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
2010-11-23 08:42:19 +00:00
public function add ( $messages , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
if ( ! isset ( $this -> messages [ $domain ])) {
$this -> messages [ $domain ] = $messages ;
} else {
$this -> messages [ $domain ] = array_replace ( $this -> messages [ $domain ], $messages );
}
}
/**
* { @ inheritdoc }
*/
public function addCatalogue ( MessageCatalogueInterface $catalogue )
{
2010-10-31 21:33:08 +00:00
if ( $catalogue -> getLocale () !== $this -> locale ) {
2016-09-21 18:16:29 +01:00
throw new LogicException ( sprintf ( 'Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"' , $catalogue -> getLocale (), $this -> locale ));
2010-10-31 21:33:08 +00:00
}
2010-11-23 08:42:19 +00:00
foreach ( $catalogue -> all () as $domain => $messages ) {
2018-11-14 13:56:01 +00:00
if ( $intlMessages = $catalogue -> all ( $domain . self :: INTL_DOMAIN_SUFFIX )) {
$this -> add ( $intlMessages , $domain . self :: INTL_DOMAIN_SUFFIX );
$messages = array_diff_key ( $messages , $intlMessages );
}
2010-11-23 08:42:19 +00:00
$this -> add ( $messages , $domain );
2010-09-27 08:45:29 +01:00
}
foreach ( $catalogue -> getResources () as $resource ) {
$this -> addResource ( $resource );
}
2012-05-24 15:20:13 +01:00
2013-02-14 09:33:24 +00:00
if ( $catalogue instanceof MetadataAwareInterface ) {
$metadata = $catalogue -> getMetadata ( '' , '' );
$this -> addMetadata ( $metadata );
}
2010-09-27 08:45:29 +01:00
}
2010-10-31 21:33:08 +00:00
/**
* { @ inheritdoc }
*/
public function addFallbackCatalogue ( MessageCatalogueInterface $catalogue )
{
2011-11-11 07:29:16 +00:00
// detect circular references
2015-09-02 10:56:25 +01:00
$c = $catalogue ;
while ( $c = $c -> getFallbackCatalogue ()) {
if ( $c -> getLocale () === $this -> getLocale ()) {
2016-09-21 18:16:29 +01:00
throw new LogicException ( sprintf ( 'Circular reference detected when adding a fallback catalogue for locale "%s".' , $catalogue -> getLocale ()));
2015-09-02 10:56:25 +01:00
}
}
2011-11-11 07:29:16 +00:00
$c = $this ;
do {
if ( $c -> getLocale () === $catalogue -> getLocale ()) {
2016-09-21 18:16:29 +01:00
throw new LogicException ( sprintf ( 'Circular reference detected when adding a fallback catalogue for locale "%s".' , $catalogue -> getLocale ()));
2011-11-11 07:29:16 +00:00
}
2016-11-06 08:05:17 +00:00
foreach ( $catalogue -> getResources () as $resource ) {
$c -> addResource ( $resource );
}
2011-11-11 08:02:31 +00:00
} while ( $c = $c -> parent );
2011-11-11 07:29:16 +00:00
2011-11-11 08:02:31 +00:00
$catalogue -> parent = $this ;
2011-08-22 15:07:39 +01:00
$this -> fallbackCatalogue = $catalogue ;
2010-10-31 21:33:08 +00:00
foreach ( $catalogue -> getResources () as $resource ) {
$this -> addResource ( $resource );
}
}
2011-09-28 15:46:33 +01:00
/**
2013-02-13 01:06:47 +00:00
* { @ inheritdoc }
2011-09-28 15:46:33 +01:00
*/
public function getFallbackCatalogue ()
{
return $this -> fallbackCatalogue ;
}
2010-09-27 08:45:29 +01:00
/**
* { @ inheritdoc }
*/
public function getResources ()
{
2013-02-13 02:04:10 +00:00
return array_values ( $this -> resources );
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
*/
public function addResource ( ResourceInterface $resource )
{
2013-02-13 02:04:10 +00:00
$this -> resources [ $resource -> __toString ()] = $resource ;
2010-09-27 08:45:29 +01:00
}
2012-05-24 15:20:13 +01:00
/**
* { @ inheritdoc }
*/
2012-12-14 13:10:50 +00:00
public function getMetadata ( $key = '' , $domain = 'messages' )
2012-05-24 15:20:13 +01:00
{
2013-02-14 09:33:24 +00:00
if ( '' == $domain ) {
2012-12-14 12:35:59 +00:00
return $this -> metadata ;
2012-05-24 15:20:13 +01:00
}
2012-12-14 12:35:59 +00:00
if ( isset ( $this -> metadata [ $domain ])) {
2013-02-14 09:33:24 +00:00
if ( '' == $key ) {
2012-12-14 12:35:59 +00:00
return $this -> metadata [ $domain ];
2012-05-24 15:20:13 +01:00
}
2013-02-14 09:33:24 +00:00
if ( isset ( $this -> metadata [ $domain ][ $key ])) {
return $this -> metadata [ $domain ][ $key ];
}
2012-05-24 15:20:13 +01:00
}
}
/**
* { @ inheritdoc }
*/
2012-12-14 12:35:59 +00:00
public function setMetadata ( $key , $value , $domain = 'messages' )
2012-05-24 15:20:13 +01:00
{
2012-12-14 12:35:59 +00:00
$this -> metadata [ $domain ][ $key ] = $value ;
2012-05-24 15:20:13 +01:00
}
/**
* { @ inheritdoc }
*/
2012-12-14 13:10:50 +00:00
public function deleteMetadata ( $key = '' , $domain = 'messages' )
2012-05-24 15:20:13 +01:00
{
2013-02-14 09:33:24 +00:00
if ( '' == $domain ) {
2012-12-14 12:35:59 +00:00
$this -> metadata = array ();
2013-02-14 09:33:24 +00:00
} elseif ( '' == $key ) {
2012-12-14 12:35:59 +00:00
unset ( $this -> metadata [ $domain ]);
2013-02-14 09:33:24 +00:00
} else {
unset ( $this -> metadata [ $domain ][ $key ]);
2012-05-24 15:20:13 +01:00
}
}
/**
2012-12-14 12:37:57 +00:00
* Adds current values with the new values .
2012-05-24 15:20:13 +01:00
*
* @ param array $values Values to add
*/
2012-12-14 12:35:59 +00:00
private function addMetadata ( array $values )
2012-05-24 15:20:13 +01:00
{
foreach ( $values as $domain => $keys ) {
foreach ( $keys as $key => $value ) {
2012-12-14 12:35:59 +00:00
$this -> setMetadata ( $key , $value , $domain );
2012-05-24 15:20:13 +01:00
}
}
}
2010-09-27 08:45:29 +01:00
}