/**
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function chooseInstallationFolder(InputInterface $input, OutputInterface $output)
 {
     $validateInstallationFolder = function ($folderName) use($input) {
         $folderName = rtrim(trim($folderName, ' '), '/');
         if (substr($folderName, 0, 1) == '.') {
             $cwd = \getcwd();
             if (empty($cwd) && isset($_SERVER['PWD'])) {
                 $cwd = $_SERVER['PWD'];
             }
             $folderName = $cwd . substr($folderName, 1);
         }
         if (empty($folderName)) {
             throw new \InvalidArgumentException('Installation folder cannot be empty');
         }
         if (!is_dir($folderName)) {
             if (!@mkdir($folderName, 0777, true)) {
                 throw new \InvalidArgumentException('Cannot create folder.');
             }
             return $folderName;
         }
         if ($input->hasOption('noDownload') && $input->getOption('noDownload')) {
             /** @var ContaoHelper $contaoHelper */
             $contaoHelper = new ContaoHelper();
             $contaoHelper->detect($folderName);
             if ($contaoHelper->getRootFolder() !== $folderName) {
                 throw new \InvalidArgumentException(sprintf('Folder %s is not a Contao working copy.', $folderName));
             }
             $localXml = $folderName . '/app/etc/local.xml';
             if (file_exists($localXml)) {
                 throw new \InvalidArgumentException(sprintf('Contao working copy in %s seems already installed. Please remove %s and retry.', $folderName, $localXml));
             }
         }
         return $folderName;
     };
     if (($installationFolder = $input->getOption('installationFolder')) == null) {
         $defaultFolder = './contao';
         $question[] = "<question>Enter installation folder:</question> [<comment>" . $defaultFolder . "</comment>]";
         $installationFolder = $this->getHelper('dialog')->askAndValidate($output, $question, $validateInstallationFolder, false, $defaultFolder);
     } else {
         // @Todo improve validation and bring it to 1 single function
         $installationFolder = $validateInstallationFolder($installationFolder);
     }
     $this->config['installationFolder'] = realpath($installationFolder);
     \chdir($this->config['installationFolder']);
 }
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @param array|PackageInterface $config
  * @param string $targetFolder
  * @param bool $preferSource
  * @return \Composer\Package\CompletePackage
  */
 protected function downloadByComposerConfig(InputInterface $input, OutputInterface $output, $config, $targetFolder, $preferSource = true)
 {
     $dm = $this->getComposerDownloadManager($input, $output);
     if (!$config instanceof PackageInterface) {
         $package = $this->createComposerPackageByConfig($config);
     } else {
         $package = $config;
     }
     $helper = new \IMI\Util\Console\Helper\ContaoHelper();
     $helper->detect($targetFolder);
     if ($this->isSourceTypeRepository($package->getSourceType()) && $helper->getRootFolder() == $targetFolder) {
         $package->setInstallationSource('source');
         $this->checkRepository($package, $targetFolder);
         $dm->update($package, $package, $targetFolder);
     } else {
         $dm->download($package, $targetFolder, $preferSource);
     }
     return $package;
 }