protected function execute(InputInterface $input, OutputInterface $output)
 {
     $download = new DownloadTranslations(new ConsoleLogger($output), new Config());
     $download->setForceDownloadTrlFiles(true);
     try {
         $download->downloadTrlFiles();
     } catch (LingohubException $e) {
         echo "LingohubException: " . $e->getMessage() . "\n";
         return 1;
     }
 }
 private function _downloadTranslations(Event $event)
 {
     if (!class_exists(__NAMESPACE__ . '\\DownloadTranslations')) {
         // uninstalling this package
         return;
     }
     $download = new DownloadTranslations(new ComposerOutput($this->_io), $this->_config);
     try {
         $download->downloadTrlFiles();
     } catch (LingohubException $e) {
         echo "LingohubException: " . $e->getMessage() . "\n";
     }
 }
 public function downloadTrlFiles()
 {
     $this->_logger->info('Iterating over packages and downloading trl-resources');
     $composerJsonFilePaths = DownloadTranslations::getComposerJsonFiles();
     foreach ($composerJsonFilePaths as $composerJsonFilePath) {
         $composerJsonFile = file_get_contents($composerJsonFilePath);
         $composerConfig = json_decode($composerJsonFile);
         if (!isset($composerConfig->extra->{'kwf-lingohub'})) {
             continue;
         }
         $kwfLingohub = $composerConfig->extra->{'kwf-lingohub'};
         $accountName = strtolower($kwfLingohub->account);
         $projectName = strtolower($kwfLingohub->project);
         $trlTempDir = $this->_getTempFolder($accountName, $projectName);
         if ($this->_checkDownloadTrlFiles($accountName, $projectName)) {
             if (!file_exists($trlTempDir)) {
                 mkdir($trlTempDir, 0777, true);
                 //write and read for everyone
             }
             $this->_logger->info("Checking for resources of {$kwfLingohub->account}/{$kwfLingohub->project}");
             $params = array('auth_token' => $this->_config->getApiToken());
             $resourcesUrl = "https://api.lingohub.com/v1/{$accountName}" . "/projects/{$projectName}/resources.json" . "?" . http_build_query($params);
             $content = file_get_contents($resourcesUrl);
             if ($content === false) {
                 throw new LingohubException('Service unavailable');
             }
             $resources = json_decode($content);
             if ($resources == null) {
                 throw new LingohubException('No json returned');
             }
             foreach ($resources->members as $resource) {
                 $poFilePath = $trlTempDir . '/' . $resource->project_locale . '.po';
                 $this->_logger->info("Downloading {$resource->name}");
                 $urlParts = parse_url($resource->links[0]->href);
                 $separator = isset($urlParts['query']) ? '&' : '?';
                 $this->_logger->info('Calling Url: ' . $resource->links[0]->href . $separator . http_build_query($params));
                 $ch = curl_init();
                 curl_setopt($ch, CURLOPT_URL, $resource->links[0]->href . $separator . http_build_query($params));
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 $file = curl_exec($ch);
                 if ($file === false) {
                     throw new LingohubException('Url provided from Lingohub not working');
                 }
                 if (strpos($file, '"Content-Type: text/plain; charset=UTF-8"') === false) {
                     $poHeader = "msgid \"\"\n" . "msgstr \"\"\n" . "\"Content-Type: text/plain; charset=UTF-8\"\n\n";
                     $file = $poHeader . $file;
                 }
                 file_put_contents($poFilePath, $file);
             }
             file_put_contents($this->_getLastUpdateFile($accountName, $projectName), date('Y-m-d H:i:s'));
         }
         if (!file_exists(dirname($composerJsonFilePath) . '/trl/')) {
             mkdir(dirname($composerJsonFilePath) . '/trl/', 0777, true);
             //write and read for everyone
         }
         foreach (scandir($trlTempDir) as $file) {
             if (substr($file, 0, 1) === '.') {
                 continue;
             }
             copy($trlTempDir . '/' . $file, dirname($composerJsonFilePath) . '/trl/' . basename($file));
         }
     }
 }