Last active
August 29, 2015 14:20
-
-
Save aitboudad/6eed3ff1187bd8930520 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <fabien@symfony.com> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Symfony\Component\Translation\Loader; | |
/** | |
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | |
*/ | |
class ContainerAwareDelegationLoader extends DelegatingLoader | |
{ | |
/** | |
* The container from where services are loaded. | |
* | |
* @var ContainerInterface | |
*/ | |
private $container; | |
/** | |
* @var array | |
*/ | |
private $loaderIds; | |
/** | |
* @param ContainerInterface $container A ContainerInterface instance | |
* @param array $loaderIds | |
*/ | |
public function __construct(ContainerInterface $container, $loaderIds) | |
{ | |
$this->container = $container; | |
$this->loaderIds = $loaderIds; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getLoaders() | |
{ | |
foreach ($this->loaderIds as $id => $aliases) { | |
foreach ($aliases as $alias) { | |
$this->addLoader($alias, $this->container->get($id)); | |
} | |
} | |
return parent::getLoaders(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getResources($locale) | |
{ | |
if ($this->container->hasParameter('translator_resources_'.$locale)) { | |
return $this->container->getParameter('translator_resources_'.$locale); | |
} | |
return array(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <fabien@symfony.com> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Symfony\Component\Translation; | |
use Symfony\Component\Translation\Loader\LoaderInterface; | |
/** | |
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | |
*/ | |
class DelegatingLoader implements TranslatorBagInterface | |
{ | |
/** | |
* @var array | |
*/ | |
private $resources = array(); | |
/** | |
* @var LoaderInterface[] An array of LoaderInterface objects | |
*/ | |
private $loaders = array(); | |
/** | |
* @param LoaderInterface[] $loaders An array of loaders | |
* @param array $resources An array of resources | |
*/ | |
public function __construct(array $loaders = array(), $resources = array()) | |
{ | |
foreach ($loaders as $format => $loader) { | |
$this->addLoader($format, $loader); | |
} | |
foreach ($resources as $resource) { | |
$this->addResource($resource['format'], $resource['resource'], $resource['locale'], $resource['domain']); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCatalogue($locale = null) | |
{ | |
$catalogue = new MessageCatalogue($locale); | |
$loaders = $this->getLoaders(); | |
foreach ($this->getResources($locale) as $resource) { | |
if (!isset($loaders[$resource[0]])) { | |
throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0])); | |
} | |
$catalogue->addCatalogue($loaders[$resource[0]]->load($resource[1], $locale, $resource[2])); | |
} | |
return $catalogue; | |
} | |
/** | |
* Adds a Resource. | |
* | |
* @param string $format The name of the loader (@see addLoader()) | |
* @param mixed $resource The resource name | |
* @param string $locale The locale | |
* @param string $domain The domain | |
*/ | |
public function addResource($format, $resource, $locale, $domain = 'messages') | |
{ | |
$this->resources[$locale][] = array($format, $resource, $domain); | |
} | |
/** | |
* Adds a Loader. | |
* | |
* @param string $format The name of the loader (@see addResource()) | |
* @param LoaderInterface $loader A LoaderInterface instance | |
*/ | |
public function addLoader($format, LoaderInterface $loader) | |
{ | |
$this->loaders[$format] = $loader; | |
} | |
/** | |
* Returns the registered loaders. | |
* | |
* @return LoaderInterface[] An array of LoaderInterface instances | |
*/ | |
public function getLoaders() | |
{ | |
return $this->loaders; | |
} | |
/** | |
* @return array | |
*/ | |
public function getResources($locale) | |
{ | |
return isset($this->resources[$locale]) ? $this->resources[$locale] : array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment