00001 <?php
00002
00003 require_once("Varien/Object.php");
00004 require_once('Varien/Directory/IFactory.php');
00005
00006 class Varien_File_Object extends SplFileObject implements IFactory {
00007 protected $_filename;
00008 protected $_path;
00009 protected $_filter;
00010 protected $_isCorrect=true; # - pass or not filter checking
00011 protected $filtered;
00012
00013
00014
00015
00016
00017
00018
00019 public function __construct($path)
00020 {
00021 parent::__construct($path);
00022 $this->_path=$path;
00023 $this->_filename=basename($path);
00024 }
00025
00026
00027
00028
00029
00030
00031 public function getFilesName(&$files)
00032 {
00033 $this->getFileName(&$files);
00034 }
00035
00036
00037
00038
00039
00040
00041 public function getFileName(&$files=null)
00042 {
00043 if($this->_isCorrect){
00044 if($files===null)
00045 return $this->_filename;
00046 $files[] = $this->_filename;
00047 }
00048 }
00049
00050
00051
00052
00053
00054
00055 public function getFilesPaths(&$paths)
00056 {
00057 if($this->_isCorrect){
00058 $paths[] = (string)$this->_path;
00059 }
00060 }
00061
00062
00063
00064
00065
00066
00067 public function getFilePath(&$path=null)
00068 {
00069 if($this->_isCorrect){
00070 if($path===null)
00071 return $this->_path;
00072 $paths[] = $this->_path;
00073 }
00074 }
00075
00076
00077
00078
00079
00080
00081 public function useFilter($useFilter)
00082 {
00083 if($useFilter){
00084 $this->renderFilter();
00085 } else {
00086 $this->_isCorrect = true;
00087 $this->filtered = false;
00088 }
00089
00090 }
00091
00092
00093
00094
00095
00096
00097 public function getFilesObj(&$objs)
00098 {
00099 if($this->_isCorrect){
00100 $objs[] = $this;
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109 public function getDirsName(&$dirs)
00110 {
00111 return Varien_Directory_Collection::getLastDir($this->_path);
00112 }
00113
00114
00115
00116
00117
00118
00119 public function getDirName()
00120 {
00121 return Varien_Directory_Collection::lastDir($this->_path);
00122 }
00123
00124
00125
00126
00127
00128
00129 public function setFilesFilter($filter)
00130 {
00131 $this->addFilter($filter);
00132 }
00133
00134
00135
00136
00137
00138
00139 public function addFilter($filter)
00140 {
00141 $this->_filter = $filter;
00142 }
00143
00144
00145
00146
00147
00148 public function getExtension()
00149 {
00150 return self::getExt($this->_filename);
00151 }
00152
00153
00154
00155
00156
00157
00158 static public function getExt($fileName)
00159 {
00160 if($fileName === ''){
00161 $path_parts = pathinfo($this->_filename);
00162 } else {
00163 $path_parts = pathinfo($fileName);
00164 }
00165 if(isset($path_parts["extension"]))
00166 return $path_parts["extension"];
00167 else
00168 return '';
00169 }
00170
00171
00172
00173
00174
00175 public function getName()
00176 {
00177 return basename($this->_filename,'.'.$this->getExtension());
00178 }
00179
00180
00181
00182
00183
00184 public function renderFilter()
00185 {
00186 #print_r($this->_filter);
00187 if(isset($this->_filter) && count($this->_filter)>0 && $this->filtered==false){
00188 $this->filtered = true;
00189 if(isset($this->_filter['extension'])){
00190 $filter = $this->_filter['extension'];
00191 if($filter!=null){
00192 if(is_array($filter)){
00193 if(!in_array($this->getExtension(),$filter)){
00194 $this->_isCorrect = false;
00195 }
00196 } else {
00197 if($this->getExtension()!=$filter){
00198 $this->_isCorrect = false;
00199 }
00200 }
00201 }
00202 }
00203 if(isset($this->_filter['name'])){
00204 $filter = $this->_filter['name'];
00205 if($filter!=null){
00206 if(is_array($filter)){
00207 if(!in_array($this->getName(),$filter)){
00208 $this->_isCorrect = false;
00209 }
00210 } else {
00211 if($this->getName()!=$filter){
00212 $this->_isCorrect = false;
00213 }
00214 }
00215 }
00216 }
00217
00218 if(isset($this->_filter['regName'])){
00219 $filter = $this->_filter['regName'];
00220
00221 if($filter!=null){
00222 foreach ($filter as $value) {
00223 if(!preg_match($value,$this->getName())){
00224 $this->_isCorrect = false;
00225 }
00226 }
00227
00228 }
00229 }
00230 }
00231 }
00232
00233
00234
00235
00236
00237
00238 public function toArray(&$arr)
00239 {
00240 if($this->_isCorrect){
00241 $arr['files_in_dirs'][] = $this->_filename;
00242 }
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
00254 {
00255 if($this->_isCorrect){
00256 $xml .=str_repeat("\t",$recursionLevel+2).'<fileName>'.$this->_filename.'</fileName>'."\n";
00257 }
00258 }
00259
00260 }
00261
00262 ?>