Loader.php
00001 <?php
00009 class Dsao_Xml_Loader
00010 {
00014 protected $_xmlObjects = array();
00015
00019 protected $_fileNames = array();
00020
00024 protected $_path = null;
00025
00033 public function __construct($path = null, array $fileNames = array())
00034 {
00035 $this->_path = $path;
00036 $this->_fileNames = $fileNames;
00037 }
00038
00044 protected function _load()
00045 {
00046
00047 $this->_xmlObjects = array();
00048
00049
00050 if (!is_dir($this->_path) || !is_readable($this->_path))
00051 {
00052 throw new Dsao_Exception(array(
00053 'message' => 'dsao_xml_loader_path',
00054 'variables' => array('path' => $this->_path)));
00055 }
00056
00057 $fileNames = $this->_fileNames;
00058
00059
00060 if (!$fileNames)
00061 {
00062 $directory = new DirectoryIterator($this->_path);
00063
00064
00065 while ($directory->valid())
00066 {
00067
00068 if ($directory->isFile())
00069 {
00070 $fileNameParts = explode('.', $directory->getFileName(), 2);
00071
00072
00073 if (isset($fileNameParts[1]) && $fileNameParts[1] == 'xml')
00074 {
00075 $fileNames[] = $fileNameParts[0];
00076 }
00077 }
00078
00079 $directory->next();
00080 }
00081 }
00082
00083
00084 foreach ($fileNames as $fileName)
00085 {
00086 $fileNameAbsolute = $this->_path.$fileName.'.xml';
00087
00088
00089 if (file_exists($fileNameAbsolute))
00090 {
00091 $this->_xmlObjects[$fileName] = simplexml_load_file($fileNameAbsolute);
00092 }
00093 }
00094 }
00095
00102 public function addFileName($fileName)
00103 {
00104 $this->_fieNames[] = $fileName;
00105
00106 return $this;
00107 }
00108
00114 public function getFileNames()
00115 {
00116 return $this->_fileNames;
00117 }
00118
00124 public function getPath()
00125 {
00126 return $this->_path;
00127 }
00128
00135 public function getXmlObjects($forceReload = false)
00136 {
00137
00138 if (!$this->_xmlObjects || $forceReload)
00139 {
00140 $this->_load();
00141 }
00142
00143 return $this->_xmlObjects;
00144 }
00145
00152 public function setFileNames(array $fileNames)
00153 {
00154 $this->_fileNames = $fileNames;
00155
00156 $this->_xmlObjects = array();
00157
00158 return $this;
00159 }
00160
00167 public function setPath($path)
00168 {
00169 $this->_path = (string) $path;
00170
00171
00172 $this->_xmlObjects = array();
00173
00174 return $this;
00175 }
00176 }