1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Openl10n\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class PullCommand extends AbstractCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$this |
18
|
|
|
->setName('pull') |
19
|
|
|
->setDescription('Pull the translations from the server to the local files') |
20
|
|
|
->addOption( |
21
|
|
|
'locale', |
22
|
|
|
null, |
23
|
|
|
InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, |
24
|
|
|
'The locale id, "default" for the source, "all" for every locales found', |
25
|
|
|
['default'] |
26
|
|
|
) |
27
|
|
|
->addArgument( |
28
|
|
|
'files', |
29
|
|
|
InputArgument::IS_ARRAY, |
30
|
|
|
'File list you want to pull from the server' |
31
|
|
|
) |
32
|
|
|
; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
39
|
|
|
{ |
40
|
|
|
$api = $this->get('api'); |
41
|
|
|
$projectApi = $api->getEntryPoint('project'); |
42
|
|
|
$resourceApi = $api->getEntryPoint('resource'); |
43
|
|
|
|
44
|
|
|
// |
45
|
|
|
// Get project |
46
|
|
|
// |
47
|
|
|
$projectSlug = $this->get('project_handler')->getProjectSlug(); |
48
|
|
|
$project = $projectApi->get($projectSlug); |
49
|
|
|
|
50
|
|
|
// |
51
|
|
|
// Get project locales |
52
|
|
|
// |
53
|
|
|
$languages = $projectApi->getLanguages($project->getSlug()); |
54
|
|
|
$projectLocales = array_map(function ($language) { |
55
|
|
|
return $language->getLocale(); |
56
|
|
|
}, $languages); |
57
|
|
|
|
58
|
|
|
$defaultLocale = $project->getDefaultLocale(); |
59
|
|
|
|
60
|
|
|
// Retrieve locales option |
61
|
|
|
$localesToPull = $input->getOption('locale'); |
62
|
|
|
$localesToPull = array_unique($localesToPull); |
63
|
|
|
|
64
|
|
|
// Process locales special cases |
65
|
|
View Code Duplication |
if (in_array('all', $localesToPull)) { |
|
|
|
|
66
|
|
|
$localesToPull = $projectLocales; |
67
|
|
|
} elseif (false !== $key = array_search('default', $localesToPull)) { |
68
|
|
|
unset($localesToPull[$key]); |
69
|
|
|
$localesToPull[] = $defaultLocale; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Deduplicate values |
73
|
|
|
$localesToPull = array_unique($localesToPull); |
74
|
|
|
|
75
|
|
|
// |
76
|
|
|
// Retrieve existing project's resources |
77
|
|
|
// |
78
|
|
|
$resources = $resourceApi->findByProject($project); |
79
|
|
|
// Set resources' pathname as array key |
80
|
|
|
$resources = array_combine(array_map(function ($resource) { |
81
|
|
|
return $resource->getPathname(); |
82
|
|
|
}, $resources), $resources); |
83
|
|
|
|
84
|
|
|
// |
85
|
|
|
// Iterate over resources |
86
|
|
|
// |
87
|
|
|
$fileSets = $this->get('file_handler')->getFileSets(); |
88
|
|
|
$fileFilter = $input->getArgument('files'); |
89
|
|
|
|
90
|
|
|
foreach ($fileSets as $fileSet) { |
91
|
|
|
$files = $fileSet->getFiles(); |
92
|
|
|
$options = $fileSet->getOptions('pull'); |
93
|
|
|
|
94
|
|
|
$resourcesToPull = []; |
|
|
|
|
95
|
|
|
|
96
|
|
|
foreach ($files as $file) { |
97
|
|
|
$resourceIdentifier = $file->getPathname(['locale' => $defaultLocale]); |
98
|
|
|
$locale = $file->getAttribute('locale'); |
99
|
|
|
|
100
|
|
|
// Skip unwanted files |
101
|
|
|
if (!empty($fileFilter) && !in_array($file->getRelativePathname(), $fileFilter)) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!isset($resources[$resourceIdentifier])) { |
106
|
|
|
$output->writeln(sprintf('Skipping file %s', $file->getRelativePathname())); |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$resource = $resources[$resourceIdentifier]; |
111
|
|
|
|
112
|
|
|
// Ignore non specified locales |
113
|
|
|
if (!in_array($locale, $localesToPull)) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$output->writeln(sprintf('<info>Downloading</info> file <comment>%s</comment>', $file->getRelativePathname())); |
118
|
|
|
$content = $resourceApi->export($resource, $locale, $options); |
119
|
|
|
|
120
|
|
|
file_put_contents($file->getAbsolutePathname(), $content); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.