/** * 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; }
/** * Package installation to FS * @param Mage_Connect_Package $package * @param string $file * @return void * @throws Exception */ public function processInstallPackage($package, $file, $configObj) { $contents = $package->getContents(); $arc = $this->getArchiver(); $target = dirname($file) . DS . $package->getReleaseFilename(); @mkdir($target, 0777, true); $mode = $configObj->global_dir_mode; $tar = $arc->unpack($file, $target); $modeFile = $configObj->global_file_mode; $modeDir = $configObj->global_dir_mode; foreach ($contents as $file) { $fileName = basename($file); $filePath = dirname($file); $source = $tar . DS . $file; $targetPath = rtrim($configObj->magento_root, "\\/"); @mkdir($targetPath . DS . $filePath, $modeDir, true); $dest = $targetPath . DS . $filePath . DS . $fileName; if (is_file($source)) { @copy($source, $dest); if ($modeFile) { @chmod($dest, $modeFile); } } else { @mkdir($dest, $modeDir); } } Mage_System_Dirs::rm(array("-r", $target)); }
/** * Package installation to FS * @param Mage_Connect_Package $package * @param string $file * @return void * @throws Exception */ public function processInstallPackage($package, $file, $configObj) { $contents = $package->getContents(); $arc = $this->getArchiver(); $target = dirname($file) . DS . $package->getReleaseFilename(); @mkdir($target, 0777, true); $tar = $arc->unpack($file, $target); $modeFile = $this->_getFileMode($configObj); $modeDir = $this->_getDirMode($configObj); foreach ($contents as $file) { $fileName = basename($file); $filePath = dirname($file); $source = $tar . DS . $file; $targetPath = rtrim($configObj->magento_root, "\\/"); @mkdir($targetPath . DS . $filePath, $modeDir, true); $dest = $targetPath . DS . $filePath . DS . $fileName; if (is_file($source)) { @copy($source, $dest); if ($modeFile) { @chmod($dest, $modeFile); } } else { @mkdir($dest, $modeDir); } } $packageXml = $tar . Mage_Connect_Package_Reader::DEFAULT_NAME_PACKAGE; if (is_file($packageXml)) { $destDir = $targetPath . DS . Mage_Connect_Package::PACKAGE_XML_DIR; $destFile = $package->getReleaseFilename() . '.xml'; $dest = $destDir . DS . $destFile; @copy($packageXml, $dest); @chmod($dest, $modeFile); } Mage_System_Dirs::rm(array("-r", $target)); }
/** * Archives package. * * @return Mage_Connect_Package_Writer */ public function archivePackage() { $this->_pathToArchive = $this->_getArchivator()->pack($this->_temporaryPackageDir, $this->_namePackage . '.tgz', true); //delete temporary dir Mage_System_Dirs::rm(array("-r", $this->_temporaryPackageDir)); return $this; }