1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EllipseSynergie\LocaleToYaml\Commands; |
4
|
|
|
|
5
|
|
|
use EllipseSynergie\LocaleToYaml\Exceptions\YamlException; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
use EllipseSynergie\LocaleToYaml\Exceptions\FileNotFoundException; |
12
|
|
|
use Symfony\Component\Yaml\Exception\DumpException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ExportCommand |
16
|
|
|
* @package EllipseSynergie\LocaleToYaml\Commands |
17
|
|
|
*/ |
18
|
|
|
class ExportCommand extends Command |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
*/ |
22
|
3 |
|
protected function configure() |
23
|
|
|
{ |
24
|
3 |
|
$this->setName('lang:export-to-yaml') |
25
|
3 |
|
->setDescription('Export locale file to Yaml.') |
26
|
3 |
|
->setHelp("This command allows you to export a locale file from PHP to Yaml...") |
27
|
3 |
|
->addArgument('in', InputArgument::REQUIRED, 'The locale input filename.'); |
28
|
3 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param InputInterface $input |
32
|
|
|
* @param OutputInterface $output |
33
|
|
|
*/ |
34
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
// Which file need to be converted? |
37
|
2 |
|
$in = $input->getArgument('in'); |
38
|
|
|
// Define output file from input one... |
39
|
2 |
|
$out = str_replace('.php', '.yml', $in); |
40
|
|
|
|
41
|
2 |
|
$output->writeln('Exporting ' . $in . ' to ' . $out); |
42
|
|
|
|
43
|
2 |
|
if (!file_exists($in)) { |
44
|
1 |
|
throw new FileNotFoundException("Cannot find file " . $in); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
|
49
|
|
|
// Get content from input file |
50
|
1 |
|
$strings = include($in); |
51
|
|
|
// Output it to Yaml file... |
52
|
1 |
|
file_put_contents($out, Yaml::dump($strings, 100, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); |
53
|
|
|
|
54
|
1 |
|
} catch (DumpException $e) { |
55
|
|
|
|
56
|
|
|
throw new YamlException("Not able to convert input file to Yaml."); |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
|
|
} |
60
|
|
|
|