Created
July 28, 2015 09:19
-
-
Save vpietri/fb0348b6ed92d51b7b5c to your computer and use it in GitHub Desktop.
Magento dataflow adapter class to get file by regex expression and can backup file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Magento Dataflow Adapter Regex Class | |
* | |
* @author Vincent Pietri <www.vincent-pietri.fr> | |
*/ | |
class ADM_Dataflow_Model_Convert_Adapter_Regex extends Mage_Dataflow_Model_Convert_Adapter_Io | |
{ | |
public function load(){ | |
$type = $this->getVar('type'); | |
$regex = $this->getVar('regex', null); | |
$path = $this->getVar('path', 'var/import'); | |
$archivePath = $this->getVar('archive_path'); | |
$order = $this->getVar('order', false); | |
//$this->setVar('path', $path); | |
if ($type !== 'file') { | |
$message = Mage::helper('dataflow')->__('Regex adapter used only for "file" type'); | |
Mage::throwException($message); | |
return; | |
} | |
if (!$regex) { | |
$message = Mage::helper('dataflow')->__('Regex adapter require "regex" key'); | |
Mage::throwException($message); | |
} else { | |
$io = new Varien_Io_File(); | |
if (strpos($path, '/')!==0) { | |
$absolutePath = BP . DS . $path; | |
} else { | |
$absolutePath = $path; | |
} | |
$io->open(array('path' => $absolutePath)); | |
$list = $io->ls(Varien_Io_File::GREP_FILES); | |
if ($order) { | |
usort($list, array($this,'_sortByDescendingDate')); | |
if ($order == 'date_asc') { | |
rsort($list); | |
} | |
} | |
foreach ($list as $file) { | |
if (preg_match('/'.$regex.'/', $file['text'])) { | |
$this->setVar('filename', $file['text']); | |
parent::load(); | |
if ($archivePath) { | |
if (strpos($archivePath, '/')!==0) { | |
$absoluteArchivePath = BP . DS . $archivePath; | |
} else { | |
$absoluteArchivePath = $archivePath; | |
} | |
$io->setAllowCreateFolders(true); | |
try { | |
if ($io->open(array('path' => $absoluteArchivePath))) { | |
if (!$io->mv($absolutePath . DS .$file['text'], $absoluteArchivePath . DS .$file['text'])) { | |
$message = Mage::helper('dataflow')->__('Cannot move file to "%s".', $absoluteArchivePath); | |
} else { | |
$message = Mage::helper('dataflow')->__('Backup file to "%s".', $absoluteArchivePath); | |
} | |
$this->addException($message); | |
} else { | |
$message = Mage::helper('dataflow')->__('Cannot open dir "%s".', $absoluteArchivePath); | |
$this->addException($message); | |
} | |
} catch (Exception $e) { | |
$this->addException($e->getMessage()); | |
} | |
} | |
return $this; | |
} | |
} | |
$message = Mage::helper('dataflow')->__('No file matching regex %s in "%s"', $regex, $absolutePath); | |
Mage::throwException($message); | |
} | |
} | |
/** | |
* From newer to older | |
* | |
* @param array $a | |
* @param array $b | |
*/ | |
protected function _sortByDescendingDate($a,$b) | |
{ | |
if ($a['mod_date']>$b['mod_date']) { | |
return -1; | |
} elseif ($a['mod_date']<$b['mod_date']) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment