2010-11-14 19:27:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-11-14 19:27:35 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-11-14 19:27:35 +00: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-11-14 19:27:35 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Translation\Loader;
|
|
|
|
|
2012-12-12 19:43:13 +00:00
|
|
|
use Symfony\Component\Translation\Exception\InvalidResourceException;
|
|
|
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2013-05-05 10:27:54 +01:00
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
2012-12-12 19:43:13 +00:00
|
|
|
use Symfony\Component\Yaml\Exception\ParseException;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-11-14 19:27:35 +00:00
|
|
|
/**
|
|
|
|
* YamlFileLoader loads translations from Yaml files.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-11-14 19:27:35 +00:00
|
|
|
*/
|
2015-03-20 16:17:32 +00:00
|
|
|
class YamlFileLoader extends ArrayLoader
|
2010-11-14 19:27:35 +00:00
|
|
|
{
|
2013-05-05 10:27:54 +01:00
|
|
|
private $yamlParser;
|
|
|
|
|
2010-11-14 19:27:35 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-11-14 19:27:35 +00:00
|
|
|
*/
|
|
|
|
public function load($resource, $locale, $domain = 'messages')
|
|
|
|
{
|
2012-12-12 19:43:13 +00:00
|
|
|
if (!stream_is_local($resource)) {
|
|
|
|
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($resource)) {
|
|
|
|
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:27:54 +01:00
|
|
|
if (null === $this->yamlParser) {
|
|
|
|
$this->yamlParser = new YamlParser();
|
|
|
|
}
|
|
|
|
|
2012-12-12 19:43:13 +00:00
|
|
|
try {
|
2013-05-05 10:27:54 +01:00
|
|
|
$messages = $this->yamlParser->parse(file_get_contents($resource));
|
2012-12-12 19:43:13 +00:00
|
|
|
} catch (ParseException $e) {
|
2014-05-20 09:28:23 +01:00
|
|
|
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
|
2012-12-12 19:43:13 +00:00
|
|
|
}
|
2010-11-14 19:27:35 +00:00
|
|
|
|
2010-12-12 07:39:37 +00:00
|
|
|
// empty file
|
|
|
|
if (null === $messages) {
|
|
|
|
$messages = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// not an array
|
|
|
|
if (!is_array($messages)) {
|
2012-12-12 19:43:13 +00:00
|
|
|
throw new InvalidResourceException(sprintf('The file "%s" must contain a YAML array.', $resource));
|
2010-12-12 07:39:37 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 19:31:11 +00:00
|
|
|
$catalogue = parent::load($messages, $locale, $domain);
|
2010-11-14 19:27:35 +00:00
|
|
|
$catalogue->addResource(new FileResource($resource));
|
|
|
|
|
|
|
|
return $catalogue;
|
|
|
|
}
|
|
|
|
}
|