forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYamlFileDumper.php
More file actions
84 lines (74 loc) · 2.3 KB
/
Copy pathYamlFileDumper.php
File metadata and controls
84 lines (74 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?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 Drupal\AppConsole;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Translation\Dumper\FileDumper;
/**
* YamlFileDumper generates yaml files from a message catalogue.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class YamlFileDumper extends FileDumper
{
/**
* @var integer Nesting depth. 0 means one line by message, 1 will
* indent at most one time, and so on.
*/
public $nestLevel = 0;
/**
* {@inheritDoc}
*/
public function dump(MessageCatalogue $messages, $options = array())
{
$this->nestLevel = array_key_exists('nest-level', $options) ? $options['nest-level'] : 0;
parent::dump($messages, $options);
}
/**
* {@inheritDoc}
*/
protected function format(MessageCatalogue $messages, $domain)
{
$m = $messages->all($domain);
if ($this->nestLevel > 0) {
// build a message tree from the message list, with a max depth
// of $this->nestLevel
$tree = array();
foreach ($m as $key => $message) {
// dots are ignored at the beginning and at the end of a key
$key = trim($key, "\t .");
if (strlen($key) > 0) {
$codes = explode('.', $key, $this->nestLevel + 1);
$node = &$tree;
foreach ($codes as $code) {
if (strlen($code) > 0) {
if (!isset($node)) {
$node = array();
}
$node = &$node[$code];
}
}
$node = $message;
}
}
return Yaml::dump($tree,
$this->nestLevel + 1); // second parameter at 1 outputs normal line-by-line catalogue
} else {
return Yaml::dump($m, 1);
}
}
/**
* {@inheritDoc}
*/
protected function getExtension()
{
return 'yml';
}
}