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