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_Core_Model_Config extends Mage_Core_Model_Config_Base
00036 {
00037 const CACHE_TAG = 'CONFIG';
00038
00039
00040
00041
00042
00043
00044 protected $_useCache = false;
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 protected $_cacheSections = array(
00056 'admin' => 0,
00057 'adminhtml' => 0,
00058 'crontab' => 0,
00059 'install' => 0,
00060 'stores' => 1,
00061 'websites' => 0
00062 );
00063
00064
00065
00066
00067
00068
00069 protected $_cacheLoadedSections = array();
00070
00071
00072
00073
00074
00075
00076 protected $_options;
00077
00078
00079
00080
00081
00082
00083 protected $_classNameCache = array();
00084
00085
00086
00087
00088
00089
00090 protected $_blockClassNameCache = array();
00091
00092
00093
00094
00095
00096
00097 protected $_secureUrlCache = array();
00098
00099
00100
00101
00102
00103
00104 protected $_distroServerVars;
00105
00106
00107
00108
00109
00110
00111 protected $_substServerVars;
00112
00113
00114
00115
00116
00117
00118
00119 protected $_resourceModel;
00120
00121
00122
00123
00124
00125
00126 protected $_eventAreas;
00127
00128
00129
00130
00131
00132
00133 protected $_dirExists = array();
00134
00135
00136
00137
00138
00139
00140 protected $_allowCacheForInit = true;
00141
00142
00143
00144
00145
00146
00147 protected $_cachePartsForSave = array();
00148
00149
00150
00151
00152
00153
00154 protected $_baseDirCache = array();
00155 protected $_customEtcDir = null;
00156
00157
00158
00159
00160
00161
00162 protected $_canUseLocalModules = null;
00163
00164
00165
00166
00167
00168
00169 public function __construct($sourceData=null)
00170 {
00171 $this->setCacheId('config_global');
00172 $this->_options = new Mage_Core_Model_Config_Options();
00173 parent::__construct($sourceData);
00174 }
00175
00176
00177
00178
00179
00180
00181 public function getResourceModel()
00182 {
00183 if (is_null($this->_resourceModel)) {
00184 $this->_resourceModel = Mage::getResourceModel('core/config');
00185 }
00186 return $this->_resourceModel;
00187 }
00188
00189
00190
00191
00192
00193
00194 public function getOptions()
00195 {
00196 return $this->_options;
00197 }
00198
00199
00200
00201
00202
00203
00204 public function init($options=array())
00205 {
00206 $this->setCacheChecksum(null);
00207 $this->_cacheLoadedSections = array();
00208 if (is_array($options)) {
00209 $this->getOptions()->addData($options);
00210 }
00211
00212 $etcDir = $this->getOptions()->getEtcDir();
00213
00214 $localConfigLoaded = $this->loadFile($etcDir.DS.'local.xml');
00215
00216 if (Mage::isInstalled()) {
00217 if ($this->_canUseCacheForInit()) {
00218 Varien_Profiler::start('mage::app::init::config::load_cache');
00219 $loaded = $this->loadCache();
00220 Varien_Profiler::stop('mage::app::init::config::load_cache');
00221 if ($loaded) {
00222 $this->_useCache = true;
00223 return $this;
00224 }
00225 }
00226 }
00227
00228
00229
00230
00231 $configFile = $etcDir.DS.'config.xml';
00232 $this->loadFile($configFile);
00233 $this->_loadDeclaredModules();
00234
00235
00236
00237
00238 Varien_Profiler::start('config/load-modules');
00239 $this->loadModulesConfiguration('config.xml', $this);
00240 Varien_Profiler::stop('config/load-modules');
00241
00242
00243
00244
00245 Varien_Profiler::start('config/load-local');
00246
00247 $mergeConfig = new Mage_Core_Model_Config_Base();
00248 $configFile = $etcDir.DS.'local.xml';
00249 if (is_readable($configFile)) {
00250 $mergeConfig->loadFile($configFile);
00251 $this->extend($mergeConfig);
00252 }
00253
00254 Varien_Profiler::stop('config/load-local');
00255
00256 $this->applyExtends();
00257
00258
00259
00260
00261 if ($localConfigLoaded) {
00262 Varien_Profiler::start('dbUpdates');
00263 Mage_Core_Model_Resource_Setup::applyAllUpdates();
00264 Varien_Profiler::stop('dbUpdates');
00265
00266 Varien_Profiler::start('config/load-db');
00267 $dbConf = $this->getResourceModel();
00268 $dbConf->loadToXml($this);
00269 Varien_Profiler::stop('config/load-db');
00270 }
00271
00272 if (Mage::app()->useCache('config')) {
00273 $this->saveCache(array(self::CACHE_TAG));
00274 }
00275
00276 return $this;
00277 }
00278
00279
00280
00281
00282
00283
00284
00285 public function reinit($options = array())
00286 {
00287 $this->_allowCacheForInit = false;
00288 $this->_useCache = false;
00289 return $this->init($options);
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 protected function _canUseLocalModules()
00301 {
00302 if ($this->_canUseLocalModules !== null) {
00303 return $this->_canUseLocalModules;
00304 }
00305
00306 $disableLocalModules = (string)$this->getNode('global/disable_local_modules');
00307 if (!empty($disableLocalModules)) {
00308 $disableLocalModules = (('true' === $disableLocalModules) || ('1' === $disableLocalModules));
00309 } else {
00310 $disableLocalModules = false;
00311 }
00312
00313 if ($disableLocalModules) {
00314 set_include_path(
00315
00316 BP . DS . 'app' . DS . 'code' . DS . 'community' . PS .
00317 BP . DS . 'app' . DS . 'code' . DS . 'core' . PS .
00318 BP . DS . 'lib' . PS .
00319 Mage::registry('original_include_path')
00320 );
00321 }
00322 $this->_canUseLocalModules = !$disableLocalModules;
00323 return $this->_canUseLocalModules;
00324 }
00325
00326
00327
00328
00329
00330
00331 protected function _canUseCacheForInit()
00332 {
00333 return Mage::app()->useCache('config') && $this->_allowCacheForInit && !$this->_loadCache($this->_getCacheLockId());
00334 }
00335
00336
00337
00338
00339
00340
00341 public function getCache()
00342 {
00343 return Mage::app()->getCache();
00344 }
00345
00346
00347
00348
00349
00350
00351 protected function _getCacheLockId()
00352 {
00353 return $this->getCacheId().'.lock';
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 public function saveCache($tags=array())
00363 {
00364 $cacheLockId = $this->_getCacheLockId();
00365 if ($this->_loadCache($cacheLockId)) {
00366 return $this;
00367 }
00368
00369 if (!empty($this->_cacheSections)) {
00370 $xml = clone $this->_xml;
00371 foreach ($this->_cacheSections as $sectionName => $level) {
00372 $this->_saveSectionCache($this->getCacheId(), $sectionName, $xml, $level, $tags);
00373 unset($xml->$sectionName);
00374 }
00375 $this->_cachePartsForSave[$this->getCacheId()] = $xml->asNiceXml('', false);
00376 } else {
00377 return parent::saveCache($tags);
00378 }
00379
00380 $this->_saveCache(time(), $cacheLockId, array(), 60);
00381 $this->removeCache();
00382 foreach ($this->_cachePartsForSave as $cacheId => $cacheData) {
00383 $this->_saveCache($cacheData, $cacheId, $tags, $this->getCacheLifetime());
00384 }
00385 unset($this->_cachePartsForSave);
00386 $this->_removeCache($cacheLockId);
00387 return $this;
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 protected function _saveSectionCache($idPrefix, $sectionName, $source, $recursionLevel=0, $tags=array())
00400 {
00401 if ($source && $source->$sectionName) {
00402 $cacheId = $idPrefix . '_' . $sectionName;
00403 if ($recursionLevel > 0) {
00404 foreach ($source->$sectionName->children() as $subSectionName => $node) {
00405 $this->_saveSectionCache($cacheId, $subSectionName, $source->$sectionName, $recursionLevel-1, $tags);
00406 }
00407 }
00408 $this->_cachePartsForSave[$cacheId] = $source->$sectionName->asNiceXml('', false);
00409 }
00410 return $this;
00411 }
00412
00413
00414
00415
00416
00417
00418
00419 protected function _loadSectionCache($sectionName)
00420 {
00421 $cacheId = $this->getCacheId() . '_' . $sectionName;
00422 $xmlString = $this->_loadCache($cacheId);
00423
00424
00425
00426
00427 if (!$xmlString) {
00428 $this->_useCache = false;
00429 $this->reinit($this->_options);
00430 return false;
00431 } else {
00432 $xml = simplexml_load_string($xmlString, $this->_elementClass);
00433 return $xml;
00434 }
00435 }
00436
00437
00438
00439
00440
00441
00442
00443 protected function _loadCache($id)
00444 {
00445 return Mage::app()->loadCache($id);
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 protected function _saveCache($data, $id, $tags=array(), $lifetime=false)
00458 {
00459 return Mage::app()->saveCache($data, $id, $tags, $lifetime);
00460 }
00461
00462
00463
00464
00465
00466
00467
00468 protected function _removeCache($id)
00469 {
00470 return Mage::app()->removeCache($id);
00471 }
00472
00473
00474
00475
00476
00477
00478 public function removeCache()
00479 {
00480 Mage::app()->cleanCache(array(self::CACHE_TAG));
00481 return parent::removeCache();
00482 }
00483
00484
00485
00486
00487
00488
00489 public function cleanCache()
00490 {
00491 return $this->reinit();
00492 }
00493
00494
00495
00496
00497
00498
00499
00500 public function getSectionNode($path)
00501 {
00502 $section = $path[0];
00503 $recursion = $this->_cacheSections[$section];
00504 $sectioPath = array_slice($path, 0, $recursion+1);
00505 $path = array_slice($path, $recursion+1);
00506 $sectionKey = implode('_', $sectioPath);
00507
00508 if (!isset($this->_cacheLoadedSections[$sectionKey])) {
00509 Varien_Profiler::start('mage::app::init::config::section::'.$sectionKey);
00510 $this->_cacheLoadedSections[$sectionKey] = $this->_loadSectionCache($sectionKey);
00511 Varien_Profiler::stop('mage::app::init::config::section::'.$sectionKey);
00512 }
00513
00514 if ($this->_cacheLoadedSections[$sectionKey] === false) {
00515 return false;
00516 }
00517 return $this->_cacheLoadedSections[$sectionKey]->descend($path);
00518 }
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 public function getNode($path=null, $scope='', $scopeCode=null)
00529 {
00530 if ($scope !== '') {
00531 if (('store' === $scope) || ('website' === $scope)) {
00532 $scope .= 's';
00533 }
00534 if (('default' !== $scope) && is_int($scopeCode)) {
00535 if ('stores' == $scope) {
00536 $scopeCode = Mage::app()->getStore($scopeCode)->getCode();
00537 } elseif ('websites' == $scope) {
00538 $scopeCode = Mage::app()->getWebsite($scopeCode)->getCode();
00539 } else {
00540 Mage::throwException(Mage::helper('core')->__('Unknown scope "%s"', $scope));
00541 }
00542 }
00543 $path = $scope . ($scopeCode ? '/' . $scopeCode : '' ) . (empty($path) ? '' : '/' . $path);
00544 }
00545
00546
00547
00548
00549 if ($this->_useCache && ($path !== null)) {
00550 $path = explode('/', $path);
00551 $section= $path[0];
00552 if (isset($this->_cacheSections[$section])) {
00553 $res = $this->getSectionNode($path);
00554 if ($res !== false) {
00555 return $res;
00556 }
00557 }
00558 }
00559 return parent::getNode($path);
00560 }
00561
00562
00563
00564
00565
00566
00567 protected function _getDeclaredModuleFiles()
00568 {
00569 $etcDir = $this->getOptions()->getEtcDir();
00570 $moduleFiles = glob($etcDir . DS . 'modules' . DS . '*.xml');
00571
00572 if (!$moduleFiles) {
00573 return false;
00574 }
00575
00576 $collectModuleFiles = array(
00577 'base' => array(),
00578 'mage' => array(),
00579 'custom' => array()
00580 );
00581
00582 foreach ($moduleFiles as $v) {
00583 $name = explode(DIRECTORY_SEPARATOR, $v);
00584 $name = substr($name[count($name) - 1], 0, -4);
00585
00586 if ($name == 'Mage_All') {
00587 $collectModuleFiles['base'][] = $v;
00588 }
00589 elseif (substr($name, 0, 5) == 'Mage_') {
00590 $collectModuleFiles['mage'][] = $v;
00591 }
00592 else {
00593 $collectModuleFiles['custom'][] = $v;
00594 }
00595 }
00596
00597 return array_merge(
00598 $collectModuleFiles['base'],
00599 $collectModuleFiles['mage'],
00600 $collectModuleFiles['custom']
00601 );
00602 }
00603
00604
00605
00606
00607
00608
00609
00610 protected function _loadDeclaredModules($mergeConfig = null)
00611 {
00612 $moduleFiles = $this->_getDeclaredModuleFiles();
00613 if (!$moduleFiles) {
00614 return ;
00615 }
00616
00617 Varien_Profiler::start('config/load-modules-declaration');
00618
00619 $unsortedConfig = new Mage_Core_Model_Config_Base();
00620 $unsortedConfig->loadString('<config/>');
00621 $fileConfig = new Mage_Core_Model_Config_Base();
00622
00623
00624 foreach ($moduleFiles as $file) {
00625 $fileConfig->loadFile($file);
00626 $unsortedConfig->extend($fileConfig);
00627 }
00628
00629 $moduleDepends = array();
00630 foreach ($unsortedConfig->getNode('modules')->children() as $moduleName => $moduleNode) {
00631 $depends = array();
00632 if ($moduleNode->depends) {
00633 foreach ($moduleNode->depends->children() as $depend) {
00634 $depends[$depend->getName()] = true;
00635 }
00636 }
00637 $moduleDepends[$moduleName] = array(
00638 'module' => $moduleName,
00639 'depends' => $depends,
00640 'active' => ('true' === (string)$moduleNode->active ? true : false),
00641 );
00642 }
00643
00644
00645 $moduleDepends = $this->_sortModuleDepends($moduleDepends);
00646
00647
00648 $sortedConfig = new Mage_Core_Model_Config_Base();
00649 $sortedConfig->loadString('<config><modules/></config>');
00650
00651 foreach ($unsortedConfig->getNode()->children() as $nodeName => $node) {
00652 if ($nodeName != 'modules') {
00653 $sortedConfig->getNode()->appendChild($node);
00654 }
00655 }
00656
00657 foreach ($moduleDepends as $moduleProp) {
00658 $node = $unsortedConfig->getNode('modules/'.$moduleProp['module']);
00659 $sortedConfig->getNode('modules')->appendChild($node);
00660 }
00661
00662 $this->extend($sortedConfig);
00663
00664 Varien_Profiler::stop('config/load-modules-declaration');
00665 return $this;
00666 }
00667
00668
00669
00670
00671
00672
00673
00674 protected function _sortModuleDepends($modules)
00675 {
00676 foreach ($modules as $moduleName => $moduleProps) {
00677 $depends = $moduleProps['depends'];
00678 foreach ($moduleProps['depends'] as $depend => $true) {
00679 if ($moduleProps['active'] && ((!isset($modules[$depend])) || empty($modules[$depend]['active']))) {
00680 Mage::throwException(Mage::helper('core')->__('Module "%1$s" requires module "%2$s"', $moduleName, $depend));
00681 }
00682 $depends = array_merge($depends, $modules[$depend]['depends']);
00683 }
00684 $modules[$moduleName]['depends'] = $depends;
00685 }
00686 $modules = array_values($modules);
00687
00688 $size = count($modules) - 1;
00689 for ($i = $size; $i >= 0; $i--) {
00690 for ($j = $size; $i < $j; $j--) {
00691 if (isset($modules[$i]['depends'][$modules[$j]['module']])) {
00692 $value = $modules[$i];
00693 $modules[$i] = $modules[$j];
00694 $modules[$j] = $value;
00695 }
00696 }
00697 }
00698
00699 $definedModules = array();
00700 foreach ($modules as $moduleProp) {
00701 foreach ($moduleProp['depends'] as $dependModule => $true) {
00702 if (!isset($definedModules[$dependModule])) {
00703 Mage::throwException(
00704 Mage::helper('core')->__('Module "%1$s" can not be depended from "%2$s"', $moduleProp['module'], $dependModule)
00705 );
00706 }
00707 }
00708 $definedModules[$moduleProp['module']] = true;
00709 }
00710
00711 return $modules;
00712 }
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722 public function loadModulesConfiguration($fileName, $mergeToObject = null, $mergeModel=null)
00723 {
00724 $disableLocalModules = !$this->_canUseLocalModules();
00725
00726 if ($mergeToObject === null) {
00727 $mergeToObject = new Mage_Core_Model_Config_Base();
00728 $mergeToObject->loadString('<config/>');
00729 }
00730 if ($mergeModel === null) {
00731 $mergeModel = new Mage_Core_Model_Config_Base();
00732 }
00733 $modules = $this->getNode('modules')->children();
00734 foreach ($modules as $modName=>$module) {
00735 if ($module->is('active')) {
00736 if ($disableLocalModules && ('local' === (string)$module->codePool)) {
00737 continue;
00738 }
00739 $configFile = $this->getModuleDir('etc', $modName).DS.$fileName;
00740 if ($mergeModel->loadFile($configFile)) {
00741 $mergeToObject->extend($mergeModel, true);
00742 }
00743 }
00744 }
00745 return $mergeToObject;
00746 }
00747
00748
00749
00750
00751
00752
00753 public function getTempVarDir()
00754 {
00755 return $this->getOptions()->getVarDir();
00756 }
00757
00758
00759
00760
00761
00762
00763 public function getDistroServerVars()
00764 {
00765 if (!$this->_distroServerVars) {
00766
00767 if (isset($_SERVER['SCRIPT_NAME']) && isset($_SERVER['HTTP_HOST'])) {
00768 $secure = (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!='off')) || $_SERVER['SERVER_PORT']=='443';
00769 $scheme = ($secure ? 'https' : 'http') . '://' ;
00770
00771 $hostArr = explode(':', $_SERVER['HTTP_HOST']);
00772 $host = $hostArr[0];
00773 $port = isset($hostArr[1]) && (!$secure && $hostArr[1]!=80 || $secure && $hostArr[1]!=443) ? ':'.$hostArr[1] : '';
00774 $path = Mage::app()->getRequest()->getBasePath();
00775
00776 $baseUrl = $scheme.$host.$port.rtrim($path, '/').'/';
00777 } else {
00778 $baseUrl = 'http://localhost/';
00779 }
00780
00781 $options = $this->getOptions();
00782 $this->_distroServerVars = array(
00783 'root_dir' => $options->getBaseDir(),
00784 'app_dir' => $options->getAppDir(),
00785 'var_dir' => $options->getVarDir(),
00786 'base_url' => $baseUrl,
00787 );
00788
00789 foreach ($this->_distroServerVars as $k=>$v) {
00790 $this->_substServerVars['{{'.$k.'}}'] = $v;
00791 }
00792 }
00793 return $this->_distroServerVars;
00794 }
00795
00796 public function substDistroServerVars($data)
00797 {
00798 $this->getDistroServerVars();
00799 return str_replace(
00800 array_keys($this->_substServerVars),
00801 array_values($this->_substServerVars),
00802 $data
00803 );
00804 }
00805
00806
00807
00808
00809
00810
00811
00812 function getModuleConfig($moduleName='')
00813 {
00814 $modules = $this->getNode('modules');
00815 if (''===$moduleName) {
00816 return $modules;
00817 } else {
00818 return $modules->$moduleName;
00819 }
00820 }
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830 function getModuleSetup($module='')
00831 {
00832 $className = 'Mage_Core_Setup';
00833 if (''!==$module) {
00834 if (is_string($module)) {
00835 $module = $this->getModuleConfig($module);
00836 }
00837 if (isset($module->setup)) {
00838 $moduleClassName = $module->setup->getClassName();
00839 if (!empty($moduleClassName)) {
00840 $className = $moduleClassName;
00841 }
00842 }
00843 }
00844 return new $className($module);
00845 }
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857 public function getBaseDir($type='base')
00858 {
00859 return $this->getOptions()->getDir($type);
00860 }
00861
00862
00863
00864
00865
00866
00867
00868
00869 public function getVarDir($path=null, $type='var')
00870 {
00871 $dir = Mage::getBaseDir($type).($path!==null ? DS.$path : '');
00872 if (!$this->createDirIfNotExists($dir)) {
00873 return false;
00874 }
00875 return $dir;
00876 }
00877
00878 public function createDirIfNotExists($dir)
00879 {
00880 return $this->getOptions()->createDirIfNotExists($dir);
00881 }
00882
00883
00884
00885
00886
00887
00888
00889
00890 public function getModuleDir($type, $moduleName)
00891 {
00892 $codePool = (string)$this->getModuleConfig($moduleName)->codePool;
00893 $dir = $this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);
00894
00895 switch ($type) {
00896 case 'etc':
00897 $dir .= DS.'etc';
00898 break;
00899
00900 case 'controllers':
00901 $dir .= DS.'controllers';
00902 break;
00903
00904 case 'sql':
00905 $dir .= DS.'sql';
00906 break;
00907
00908 case 'locale':
00909 $dir .= DS.'locale';
00910 break;
00911 }
00912
00913 $dir = str_replace('/', DS, $dir);
00914 return $dir;
00915 }
00916
00917
00918
00919
00920
00921
00922
00923 public function loadEventObservers($area)
00924 {
00925 $events = $this->getNode("$area/events");
00926 if ($events) {
00927 $events = $events->children();
00928 }
00929 else {
00930 return false;
00931 }
00932
00933 foreach ($events as $event) {
00934 $eventName = $event->getName();
00935 $observers = $event->observers->children();
00936 foreach ($observers as $observer) {
00937 switch ((string)$observer->type) {
00938 case 'singleton':
00939 $callback = array(
00940 Mage::getSingleton((string)$observer->class),
00941 (string)$observer->method
00942 );
00943 break;
00944 case 'object':
00945 case 'model':
00946 $callback = array(
00947 Mage::getModel((string)$observer->class),
00948 (string)$observer->method
00949 );
00950 break;
00951 default:
00952 $callback = array($observer->getClassName(), (string)$observer->method);
00953 break;
00954 }
00955
00956 $args = (array)$observer->args;
00957 $observerClass = $observer->observer_class ? (string)$observer->observer_class : '';
00958 Mage::addObserver($eventName, $callback, $args, $observer->getName(), $observerClass);
00959 }
00960 }
00961 return true;
00962 }
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972 public function getPathVars($args=null)
00973 {
00974 $path = array();
00975
00976 $path['baseUrl'] = Mage::getBaseUrl();
00977 $path['baseSecureUrl'] = Mage::getBaseUrl('link', true);
00978
00979 return $path;
00980 }
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990 public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
00991 {
00992 if (empty($groupRootNode)) {
00993 $groupRootNode = 'global/'.$groupType.'s';
00994 }
00995
00996 $classArr = explode('/', trim($classId));
00997 $group = $classArr[0];
00998 $class = !empty($classArr[1]) ? $classArr[1] : null;
00999
01000 if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
01001 return $this->_classNameCache[$groupRootNode][$group][$class];
01002 }
01003
01004
01005 $config = $this->_xml->global->{$groupType.'s'}->{$group};
01006
01007 if (isset($config->rewrite->$class)) {
01008 $className = (string)$config->rewrite->$class;
01009 } else {
01010 if (!empty($config)) {
01011 $className = $config->getClassName();
01012 }
01013 if (empty($className)) {
01014 $className = 'mage_'.$group.'_'.$groupType;
01015 }
01016 if (!empty($class)) {
01017 $className .= '_'.$class;
01018 }
01019 $className = uc_words($className);
01020 }
01021
01022 $this->_classNameCache[$groupRootNode][$group][$class] = $className;
01023 return $className;
01024 }
01025
01026
01027
01028
01029
01030
01031
01032 public function getBlockClassName($blockType)
01033 {
01034 if (strpos($blockType, '/')===false) {
01035 return $blockType;
01036 }
01037 return $this->getGroupedClassName('block', $blockType);
01038 }
01039
01040
01041
01042
01043
01044
01045
01046 public function getHelperClassName($helperName)
01047 {
01048 if (strpos($helperName, '/')===false) {
01049 $helperName .= '/data';
01050 }
01051 return $this->getGroupedClassName('helper', $helperName);
01052 }
01053
01054
01055
01056
01057
01058
01059
01060 public function getModelClassName($modelClass)
01061 {
01062 $modelClass = trim($modelClass);
01063 if (strpos($modelClass, '/')===false) {
01064 return $modelClass;
01065 }
01066 return $this->getGroupedClassName('model', $modelClass);
01067 }
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081 public function getModelInstance($modelClass='', $constructArguments=array())
01082 {
01083 $className = $this->getModelClassName($modelClass);
01084 if (class_exists($className)) {
01085 Varien_Profiler::start('CORE::create_object_of::'.$className);
01086 $obj = new $className($constructArguments);
01087 Varien_Profiler::stop('CORE::create_object_of::'.$className);
01088 return $obj;
01089 } else {
01090 #throw Mage::exception('Mage_Core', Mage::helper('core')->__('Model class does not exist: %s', $modelClass));
01091 return false;
01092 }
01093 }
01094
01095 public function getNodeClassInstance($path)
01096 {
01097 $config = Mage::getConfig()->getNode($path);
01098 if (!$config) {
01099 return false;
01100 } else {
01101 $className = $config->getClassName();
01102 return new $className();
01103 }
01104 }
01105
01106
01107
01108
01109
01110
01111
01112
01113 public function getResourceModelInstance($modelClass='', $constructArguments=array())
01114 {
01115 $factoryName = $this->_getResourceModelFactoryClassName($modelClass);
01116 if (!$factoryName) {
01117 return false;
01118 }
01119 return $this->getModelInstance($factoryName, $constructArguments);
01120 }
01121
01122
01123
01124
01125
01126
01127
01128 public function getResourceConfig($name)
01129 {
01130 return $this->_xml->global->resources->{$name};
01131 }
01132
01133
01134
01135
01136
01137
01138
01139 public function getResourceConnectionConfig($name)
01140 {
01141 $config = $this->getResourceConfig($name);
01142 if ($config) {
01143 $conn = $config->connection;
01144 if (!empty($conn->use)) {
01145 return $this->getResourceConnectionConfig((string)$conn->use);
01146 } else {
01147 return $conn;
01148 }
01149 }
01150 return false;
01151 }
01152
01153
01154
01155
01156
01157
01158
01159 public function getResourceTypeConfig($type)
01160 {
01161 return $this->_xml->global->resource->connection->types->{$type};
01162 }
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175 public function getStoresConfigByPath($path, $allowValues = array(), $useAsKey = 'id')
01176 {
01177 $storeValues = array();
01178 $stores = $this->getNode('stores');
01179 foreach ($stores->children() as $code => $store) {
01180 switch ($useAsKey) {
01181 case 'id':
01182 $key = (int) $store->descend('system/store/id');
01183 break;
01184
01185 case 'code':
01186 $key = $code;
01187 break;
01188
01189 case 'name':
01190 $key = (string) $store->descend('system/store/name');
01191 }
01192 if ($key === false) {
01193 continue;
01194 }
01195
01196 $pathValue = (string) $store->descend($path);
01197
01198 if (empty($allowValues)) {
01199 $storeValues[$key] = $pathValue;
01200 }
01201 elseif(in_array($pathValue, $allowValues)) {
01202 $storeValues[$key] = $pathValue;
01203 }
01204 }
01205 return $storeValues;
01206 }
01207
01208
01209
01210
01211
01212
01213
01214 public function shouldUrlBeSecure($url)
01215 {
01216 if (!isset($this->_secureUrlCache[$url])) {
01217 $this->_secureUrlCache[$url] = false;
01218 $secureUrls = $this->getNode('frontend/secure_url');
01219 foreach ($secureUrls->children() as $match) {
01220 if (strpos($url, (string)$match)===0) {
01221 $this->_secureUrlCache[$url] = true;
01222 break;
01223 }
01224 }
01225 }
01226
01227 return $this->_secureUrlCache[$url];
01228 }
01229
01230
01231
01232
01233
01234
01235 public function getTablePrefix()
01236 {
01237 return $this->_xml->global->resources->db->table_prefix;
01238 }
01239
01240
01241
01242
01243
01244
01245
01246
01247 public function getEventConfig($area, $eventName)
01248 {
01249
01250 if (!isset($this->_eventAreas[$area])) {
01251 $this->_eventAreas[$area] = $this->getNode($area)->events;
01252 }
01253 return $this->_eventAreas[$area]->{$eventName};
01254 }
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265 public function saveConfig($path, $value, $scope = 'default', $scopeId = 0)
01266 {
01267 $resource = $this->getResourceModel();
01268 $resource->saveConfig(rtrim($path, '/'), $value, $scope, $scopeId);
01269
01270 return $this;
01271 }
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281 public function deleteConfig($path, $scope = 'default', $scopeId = 0)
01282 {
01283 $resource = $this->getResourceModel();
01284 $resource->deleteConfig(rtrim($path, '/'), $scope, $scopeId);
01285
01286 return $this;
01287 }
01288
01289
01290
01291
01292
01293
01294
01295
01296 public function getFieldset($name, $root = 'global')
01297 {
01298 $rootNode = $this->getNode($root.'/fieldsets');
01299 if (!$rootNode) {
01300 return null;
01301 }
01302 return $rootNode->$name ? $rootNode->$name->children() : null;
01303 }
01304
01305
01306
01307
01308
01309
01310
01311 protected function _getResourceModelFactoryClassName($modelClass)
01312 {
01313 $classArr = explode('/', $modelClass);
01314
01315 $resourceModel = false;
01316
01317 if (!isset($this->_xml->global->models->{$classArr[0]})) {
01318 return false;
01319 }
01320
01321 $module = $this->_xml->global->models->{$classArr[0]};
01322
01323 if ((count($classArr)==2)
01324 && isset($module->{$classArr[1]}->resourceModel)
01325 && $resourceInfo = $module->{$classArr[1]}->resourceModel) {
01326 $resourceModel = (string) $resourceInfo;
01327 }
01328 elseif (isset($module->resourceModel) && $resourceInfo = $module->resourceModel) {
01329 $resourceModel = (string) $resourceInfo;
01330 }
01331
01332 if (!$resourceModel) {
01333 return false;
01334 }
01335 return $resourceModel . '/' . $classArr[1];
01336 }
01337
01338
01339
01340
01341
01342
01343
01344 public function getResourceModelClassName($modelClass)
01345 {
01346 $factoryName = $this->_getResourceModelFactoryClassName($modelClass);
01347 if ($factoryName) {
01348 return $this->getModelClassName($factoryName);
01349 }
01350 return false;
01351 }
01352 }