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 Varien_Simplexml_Config
00036 {
00037
00038
00039
00040
00041
00042
00043 protected $_xml = null;
00044
00045
00046
00047
00048
00049
00050 protected $_cacheId = null;
00051
00052
00053
00054
00055
00056
00057 protected $_cacheTags = array();
00058
00059
00060
00061
00062
00063
00064 protected $_cacheLifetime = null;
00065
00066
00067
00068
00069
00070
00071 protected $_cacheChecksum = false;
00072
00073
00074
00075
00076
00077
00078 protected $_cacheSaved = false;
00079
00080
00081
00082
00083
00084
00085 protected $_cache = null;
00086
00087
00088
00089
00090
00091
00092 protected $_elementClass = 'Varien_Simplexml_Element';
00093
00094
00095
00096
00097
00098
00099 protected $_xpathExtends = "//*[@extends]";
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 public function __construct($sourceData=null) {
00111 if (is_null($sourceData)) {
00112 return;
00113 }
00114 if ($sourceData instanceof Varien_Simplexml_Element) {
00115 $this->setXml($sourceData);
00116 } elseif (is_string($sourceData) && !empty($sourceData)) {
00117 if (strlen($sourceData)<1000 && is_readable($sourceData)) {
00118 $this->loadFile($sourceData);
00119 } else {
00120 $this->loadString($sourceData);
00121 }
00122 }
00123 #$this->setCache(new Varien_Simplexml_Config_Cache_File());
00124 #$this->getCache()->setConfig($this);
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 public function setXml(Varien_Simplexml_Element $node)
00134 {
00135 $this->_xml = $node;
00136 return $this;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 public function getNode($path=null)
00147 {
00148 if (!$this->_xml instanceof Varien_Simplexml_Element) {
00149 return false;
00150 } elseif ($path === null) {
00151 return $this->_xml;
00152 } else {
00153 return $this->_xml->descend($path);
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 public function getXpath($xpath)
00164 {
00165 if (empty($this->_xml)) {
00166 return false;
00167 }
00168
00169 if (!$result = @$this->_xml->xpath($xpath)) {
00170 return false;
00171 }
00172
00173 return $result;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 public function setCache($cache)
00183 {
00184 $this->_cache = $cache;
00185 return $this;
00186 }
00187
00188
00189
00190
00191
00192
00193 public function getCache()
00194 {
00195 return $this->_cache;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 public function setCacheSaved($flag)
00205 {
00206 $this->_cacheSaved = $flag;
00207 return $this;
00208 }
00209
00210
00211
00212
00213
00214
00215 public function getCacheSaved()
00216 {
00217 return $this->_cacheSaved;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 public function setCacheId($id)
00227 {
00228 $this->_cacheId = $id;
00229 return $this;
00230 }
00231
00232
00233
00234
00235
00236
00237 public function getCacheId()
00238 {
00239 return $this->_cacheId;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 public function setCacheTags($tags)
00249 {
00250 $this->_cacheTags = $tags;
00251 return $this;
00252 }
00253
00254
00255
00256
00257
00258
00259 public function getCacheTags()
00260 {
00261 return $this->_cacheTags;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270 public function setCacheLifetime($lifetime)
00271 {
00272 $this->_cacheLifetime = $lifetime;
00273 return $this;
00274 }
00275
00276
00277
00278
00279
00280
00281 public function getCacheLifetime()
00282 {
00283 return $this->_cacheLifetime;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 public function setCacheChecksum($data)
00293 {
00294 if (is_null($data)) {
00295 $this->_cacheChecksum = null;
00296 } elseif (false===$data || 0===$data) {
00297 $this->_cacheChecksum = false;
00298 } else {
00299 $this->_cacheChecksum = md5($data);
00300 }
00301 return $this;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 public function updateCacheChecksum($data)
00311 {
00312 if (false===$this->getCacheChecksum()) {
00313 return $this;
00314 }
00315 if (false===$data || 0===$data) {
00316 $this->_cacheChecksum = false;
00317 } else {
00318 $this->setCacheChecksum($this->getCacheChecksum().':'.$data);
00319 }
00320 return $this;
00321 }
00322
00323
00324
00325
00326
00327
00328 public function getCacheChecksum()
00329 {
00330 return $this->_cacheChecksum;
00331 }
00332
00333
00334
00335
00336
00337
00338 public function getCacheChecksumId()
00339 {
00340 return $this->getCacheId().'__CHECKSUM';
00341 }
00342
00343
00344
00345
00346
00347
00348 public function fetchCacheChecksum()
00349 {
00350 return false;
00351 }
00352
00353
00354
00355
00356
00357
00358 public function validateCacheChecksum()
00359 {
00360 $newChecksum = $this->getCacheChecksum();
00361 if (false===$newChecksum) {
00362 return false;
00363 }
00364 if (is_null($newChecksum)) {
00365 return true;
00366 }
00367 $cachedChecksum = $this->getCache()->load($this->getCacheChecksumId());
00368 return $newChecksum===false && $cachedChecksum===false || $newChecksum===$cachedChecksum;
00369 }
00370
00371
00372
00373
00374
00375
00376 public function loadCache()
00377 {
00378 if (!$this->validateCacheChecksum()) {
00379 return false;
00380 }
00381
00382 $xmlString = $this->_loadCache($this->getCacheId());
00383 $xml = simplexml_load_string($xmlString, $this->_elementClass);
00384 if ($xml) {
00385 $this->_xml = $xml;
00386 $this->setCacheSaved(true);
00387 return true;
00388 }
00389
00390 return false;
00391 }
00392
00393
00394
00395
00396
00397
00398
00399 public function saveCache($tags=null)
00400 {
00401 if ($this->getCacheSaved()) {
00402 return $this;
00403 }
00404 if (false===$this->getCacheChecksum()) {
00405 return $this;
00406 }
00407
00408 if (is_null($tags)) {
00409 $tags = $this->_cacheTags;
00410 }
00411
00412 if (!is_null($this->getCacheChecksum())) {
00413 $this->_saveCache($this->getCacheChecksum(), $this->getCacheChecksumId(), $tags, $this->getCacheLifetime());
00414 }
00415
00416 $xmlString = $this->getXmlString();
00417 $this->_saveCache($xmlString, $this->getCacheId(), $tags, $this->getCacheLifetime());
00418
00419 $this->setCacheSaved(true);
00420
00421 return $this;
00422 }
00423
00424
00425
00426
00427
00428
00429 public function getXmlString()
00430 {
00431 return $this->getNode()->asNiceXml('', false);
00432 }
00433
00434
00435
00436
00437
00438
00439 public function removeCache()
00440 {
00441 $this->_removeCache($this->getCacheId());
00442 $this->_removeCache($this->getCacheChecksumId());
00443 return $this;
00444 }
00445
00446
00447
00448
00449
00450
00451
00452 protected function _loadCache($id)
00453 {
00454 return $this->getCache()->load($id);
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 protected function _saveCache($data, $id, $tags=array(), $lifetime=false)
00467 {
00468 return $this->getCache()->save($data, $id, $tags, $lifetime);
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478 protected function _removeCache($id)
00479 {
00480 return $this->getCache()->remove($id);
00481 }
00482
00483
00484
00485
00486
00487
00488
00489 public function loadFile($filePath)
00490 {
00491 if (!is_readable($filePath)) {
00492
00493 return false;
00494 }
00495
00496 $fileData = file_get_contents($filePath);
00497 $fileData = $this->processFileData($fileData);
00498 return $this->loadString($fileData, $this->_elementClass);
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 public function loadString($string)
00508 {
00509 if (!empty($string)) {
00510 $xml = simplexml_load_string($string, $this->_elementClass);
00511 }
00512 else {
00513 throw new Exception('"$string" parameter for simplexml_load_string is empty');
00514 }
00515
00516 if ($xml instanceof Varien_Simplexml_Element) {
00517 $this->_xml = $xml;
00518 return true;
00519 }
00520
00521 return false;
00522 }
00523
00524
00525
00526
00527
00528
00529
00530 public function loadDom($dom)
00531 {
00532 $xml = simplexml_import_dom($dom, $this->_elementClass);
00533
00534 if ($xml) {
00535 $this->_xml = $xml;
00536 return true;
00537 }
00538
00539 return false;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 public function setNode($path, $value, $overwrite=true)
00551 {
00552 $xml = $this->_xml->setNode($path, $value, $overwrite);
00553 return $this;
00554 }
00555
00556
00557
00558
00559
00560
00561 public function applyExtends()
00562 {
00563 $targets = $this->getXpath($this->_xpathExtends);
00564 if (!$targets) {
00565 return $this;
00566 }
00567
00568 foreach ($targets as $target) {
00569 $sources = $this->getXpath((string)$target['extends']);
00570 if ($sources) {
00571 foreach ($sources as $source) {
00572 $target->extend($source);
00573 }
00574 } else {
00575 #echo "Not found extend: ".(string)$target['extends'];
00576 }
00577 #unset($target['extends']);
00578 }
00579 return $this;
00580 }
00581
00582
00583
00584
00585
00586
00587
00588 public function processFileData($text)
00589 {
00590 return $text;
00591 }
00592
00593
00594
00595
00596
00597
00598
00599
00600 public function extend(Varien_Simplexml_Config $config, $overwrite=true)
00601 {
00602 $this->getNode()->extend($config->getNode(), $overwrite);
00603 return $this;
00604 }
00605
00606 }