00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Dataflow_Model_Batch_Io
00036 {
00037 const TMP_DIR = '/var/tmp/';
00038 const TMP_NAME = 'batch_%d.tmp';
00039
00040
00041
00042
00043
00044
00045 protected $_batchModel;
00046
00047
00048
00049
00050
00051
00052 protected $_path;
00053
00054
00055
00056
00057
00058
00059 protected $_filename;
00060
00061
00062
00063
00064
00065
00066 protected $_ioFile;
00067
00068
00069
00070
00071
00072
00073 protected $_fileSize = 0;
00074
00075
00076
00077
00078
00079
00080
00081 public function init(Mage_Dataflow_Model_Batch $object)
00082 {
00083 $this->_batchModel = $object;
00084 return $this;
00085 }
00086
00087
00088
00089
00090
00091
00092 public function getPath()
00093 {
00094 if (is_null($this->_path)) {
00095 $this->_path = $this->getIoAdapter()->getCleanPath(Mage::getBaseDir('tmp'));
00096 $this->getIoAdapter()->checkAndCreateFolder($this->_path);
00097 }
00098 return $this->_path;
00099 }
00100
00101
00102
00103
00104
00105
00106 public function getFile($withPath = false)
00107 {
00108 if (is_null($this->_filename)) {
00109 $this->_filename = sprintf(self::TMP_NAME, $this->_batchModel->getId());
00110 }
00111 if ($withPath) {
00112 return $this->getPath() . $this->_filename;
00113 }
00114 return $this->_filename;
00115 }
00116
00117
00118
00119
00120
00121
00122 public function getIoAdapter()
00123 {
00124 if (is_null($this->_ioFile)) {
00125 $this->_ioFile = new Varien_Io_File();
00126 }
00127 return $this->_ioFile;
00128 }
00129
00130
00131
00132
00133
00134
00135 public function open($write = true)
00136 {
00137 $mode = $write ? 'w+' : 'r+';
00138 $ioConfig = array(
00139 'path' => $this->getPath()
00140 );
00141 $this->getIoAdapter()->setAllowCreateFolders(true);
00142 $this->getIoAdapter()->open($ioConfig);
00143 $this->getIoAdapter()->streamOpen($this->getFile(), $mode);
00144
00145 $this->_fileSize = 0;
00146
00147 return $this;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 public function write($string)
00157 {
00158 $this->_fileSize += strlen($string);
00159 return $this->getIoAdapter()->streamWrite($string);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 public function read($csv = false, $delimiter = ',', $enclosure = '"')
00171 {
00172 if ($csv) {
00173 $content = $this->getIoAdapter()->streamReadCsv($delimiter, $enclosure);
00174 }
00175 else {
00176 $content = $this->getIoAdapter()->streamRead(1024);
00177 $this->_fileSize += strlen($content);
00178 }
00179 return $content;
00180 }
00181
00182
00183
00184
00185
00186
00187 public function close()
00188 {
00189 return $this->getIoAdapter()->streamClose();
00190 }
00191
00192 public function clear()
00193 {
00194 return $this->getIoAdapter()->rm($this->getFile(true));
00195 }
00196
00197
00198
00199
00200
00201
00202 public function getFileSize()
00203 {
00204 return $this->_fileSize;
00205 }
00206 }