示例#1
0
 /**
  * Convert PEAR package to Magento package
  * @param string $sourceFile  path to PEAR .tgz
  * @param string|false $destFile    path to newly-created Magento .tgz, false to specify auto
  * @return bool
  */
 public function convertPearToMage($sourceFile, $destFile = false)
 {
     try {
         if (!file_exists($sourceFile)) {
             throw new Exception("File doesn't exist: {$sourceFile}");
         }
         $arc = $this->arc();
         $tempDir = "tmp-" . basename($sourceFile) . uniqid();
         $outDir = "out-" . basename($sourceFile) . uniqid();
         $outDir = rtrim($outDir, "\\/");
         Mage_System_Dirs::mkdirStrict($outDir);
         Mage_System_Dirs::mkdirStrict($tempDir);
         $result = $arc->unpack($sourceFile, $tempDir);
         if (!$result) {
             throw new Exception("'{$sourceFile}' was not unpacked");
         }
         $result = rtrim($result, "\\/");
         $packageXml = $result . DS . "package.xml";
         if (!file_exists($packageXml)) {
             throw new Exception("No package.xml found inside '{$sourceFile}'");
         }
         $reader = $this->oldPackageReader();
         $data = file_get_contents($packageXml);
         $pearObject = $reader->parsePackage($data, $packageXml);
         $mageObject = $this->convertPackageObject($pearObject);
         if (!$mageObject->validate()) {
             throw new Exception("Package validation failed.\n" . implode("\n", $mageObject->getErrors()));
         }
         /**
          * Calculate destination file if false
          */
         if (false === $destFile) {
             $pathinfo = pathinfo($sourceFile);
             $destFile = $pathinfo['dirname'] . DS . $pathinfo['filename'] . '-converted';
             if (isset($pathinfo['extension'])) {
                 $destFile .= "." . $pathinfo['extension'];
             }
         }
         $target = new Mage_Connect_Package_Target("target.xml");
         $targets = $target->getTargets();
         $mageObject->setTarget($target);
         $validRoles = array_keys($targets);
         $data = $pearObject->getFilelist();
         $pathSource = dirname($pearObject->getPackageFile()) . DS . $pearObject->getName() . "-" . $pearObject->getVersion();
         $filesToDo = array();
         foreach ($data as $file => $row) {
             $name = $row['name'];
             $role = $row['role'];
             if (!in_array($role, $validRoles)) {
                 $role = 'mage';
             }
             $baseName = ltrim($targets[$role], "\\/.");
             $baseName = rtrim($baseName, "\\/");
             $sourceFile = $pathSource . DS . $name;
             $targetFile = $outDir . DS . $baseName . DS . $name;
             if (file_exists($sourceFile)) {
                 Mage_System_Dirs::mkdirStrict(dirname($targetFile));
                 $copy = @copy($sourceFile, $targetFile);
                 if (false === $copy) {
                     throw new Exception("Cannot copy '{$sourceFile}' to '{$targetFile}'");
                 }
             }
             $filesToDo[] = array('name' => $name, 'role' => $role);
         }
         $cwd = getcwd();
         @chdir($outDir);
         foreach ($filesToDo as $fileToDo) {
             $mageObject->addContent($fileToDo['name'], $fileToDo['role']);
         }
         $mageObject->save(getcwd());
         @chdir($cwd);
         $filename = $outDir . DS . $mageObject->getReleaseFilename() . ".tgz";
         if (@file_exists($targetArchive)) {
             @unlink($targetArchive);
         }
         Mage_System_Dirs::mkdirStrict(dirname($destFile));
         $copy = @copy($filename, $destFile);
         if (false === $copy) {
             throw new Exception("Cannot copy '{$filename}' to '{$targetArchive}'");
         }
         Mage_System_Dirs::rm($tempDir);
         Mage_System_Dirs::rm($outDir);
     } catch (Exception $e) {
         throw $e;
     }
     return $destFile;
 }
示例#2
0
 /**
  * Retrieve targets
  *
  * @return array
  */
 public function getLabelTargets()
 {
     if (!is_array($this->_targets)) {
         $objectTarget = new Mage_Connect_Package_Target();
         $this->_targets = $objectTarget->getLabelTargets();
     }
     return $this->_targets;
 }