1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Model_Config extends Mage_Core_Model_Config_Base
36: {
37: const CACHE_TAG = 'CONFIG';
38:
39: 40: 41: 42: 43:
44: protected $_useCache = false;
45:
46: 47: 48: 49: 50: 51: 52: 53: 54:
55: protected $_cacheSections = array(
56: 'admin' => 0,
57: 'adminhtml' => 0,
58: 'crontab' => 0,
59: 'install' => 0,
60: 'stores' => 1,
61: 'websites' => 0
62: );
63:
64: 65: 66: 67: 68:
69: protected $_cacheLoadedSections = array();
70:
71: 72: 73: 74: 75:
76: protected $_options;
77:
78: 79: 80: 81: 82:
83: protected $_classNameCache = array();
84:
85: 86: 87: 88: 89:
90: protected $_blockClassNameCache = array();
91:
92: 93: 94: 95: 96:
97: protected $_secureUrlCache = array();
98:
99: 100: 101: 102: 103:
104: protected $_distroServerVars;
105:
106: 107: 108: 109: 110:
111: protected $_substServerVars;
112:
113: 114: 115: 116: 117: 118:
119: protected $_resourceModel;
120:
121: 122: 123: 124: 125:
126: protected $_eventAreas;
127:
128: 129: 130: 131: 132:
133: protected $_dirExists = array();
134:
135: 136: 137: 138: 139:
140: protected $_allowCacheForInit = true;
141:
142: 143: 144: 145: 146:
147: protected $_cachePartsForSave = array();
148:
149: 150: 151: 152: 153:
154: protected $_prototype;
155:
156: 157: 158: 159: 160:
161: protected $_isLocalConfigLoaded = false;
162:
163: 164: 165: 166: 167:
168: protected $_baseDirCache = array();
169: protected $_customEtcDir = null;
170:
171: 172: 173: 174: 175:
176: protected $_canUseLocalModules = null;
177:
178: 179: 180: 181:
182: private $_moduleNamespaces = null;
183:
184: 185: 186: 187: 188: 189:
190: protected $_allowedModules = array();
191:
192: 193: 194: 195: 196:
197: public function __construct($sourceData=null)
198: {
199: $this->setCacheId('config_global');
200: $this->_options = new Mage_Core_Model_Config_Options($sourceData);
201: $this->_prototype = new Mage_Core_Model_Config_Base();
202: $this->_cacheChecksum = null;
203: parent::__construct($sourceData);
204: }
205:
206: 207: 208: 209: 210:
211: public function getResourceModel()
212: {
213: if (is_null($this->_resourceModel)) {
214: $this->_resourceModel = Mage::getResourceModel('core/config');
215: }
216: return $this->_resourceModel;
217: }
218:
219: 220: 221: 222: 223:
224: public function getOptions()
225: {
226: return $this->_options;
227: }
228:
229: 230: 231: 232: 233: 234:
235: public function setOptions($options)
236: {
237: if (is_array($options)) {
238: $this->getOptions()->addData($options);
239: }
240: return $this;
241: }
242:
243: 244: 245: 246: 247:
248: public function init($options=array())
249: {
250: $this->setCacheChecksum(null);
251: $this->_cacheLoadedSections = array();
252: $this->setOptions($options);
253: $this->loadBase();
254:
255: $cacheLoad = $this->loadModulesCache();
256: if ($cacheLoad) {
257: return $this;
258: }
259: $this->loadModules();
260: $this->loadDb();
261: $this->saveCache();
262: return $this;
263: }
264:
265: 266: 267: 268: 269:
270: public function loadBase()
271: {
272: $etcDir = $this->getOptions()->getEtcDir();
273: $files = glob($etcDir.DS.'*.xml');
274: $this->loadFile(current($files));
275: while ($file = next($files)) {
276: $merge = clone $this->_prototype;
277: $merge->loadFile($file);
278: $this->extend($merge);
279: }
280: if (in_array($etcDir.DS.'local.xml', $files)) {
281: $this->_isLocalConfigLoaded = true;
282: }
283: return $this;
284: }
285:
286: 287: 288: 289: 290:
291: public function loadModulesCache()
292: {
293: if (Mage::isInstalled(array('etc_dir' => $this->getOptions()->getEtcDir()))) {
294: if ($this->_canUseCacheForInit()) {
295: Varien_Profiler::start('mage::app::init::config::load_cache');
296: $loaded = $this->loadCache();
297: Varien_Profiler::stop('mage::app::init::config::load_cache');
298: if ($loaded) {
299: $this->_useCache = true;
300: return true;
301: }
302: }
303: }
304: return false;
305: }
306:
307: 308: 309: 310: 311:
312: public function loadModules()
313: {
314: Varien_Profiler::start('config/load-modules');
315: $this->_loadDeclaredModules();
316:
317: $resourceConfig = sprintf('config.%s.xml', $this->_getResourceConnectionModel('core'));
318: $this->loadModulesConfiguration(array('config.xml',$resourceConfig), $this);
319:
320: 321: 322:
323: $mergeConfig = clone $this->_prototype;
324: $this->_isLocalConfigLoaded = $mergeConfig->loadFile($this->getOptions()->getEtcDir().DS.'local.xml');
325: if ($this->_isLocalConfigLoaded) {
326: $this->extend($mergeConfig);
327: }
328:
329: $this->applyExtends();
330: Varien_Profiler::stop('config/load-modules');
331: return $this;
332: }
333:
334: 335: 336: 337: 338:
339: public function isLocalConfigLoaded()
340: {
341: return $this->_isLocalConfigLoaded;
342: }
343:
344: 345: 346: 347: 348:
349: public function loadDb()
350: {
351: if ($this->_isLocalConfigLoaded && Mage::isInstalled()) {
352: Varien_Profiler::start('config/load-db');
353: $dbConf = $this->getResourceModel();
354: $dbConf->loadToXml($this);
355: Varien_Profiler::stop('config/load-db');
356: }
357: return $this;
358: }
359:
360: 361: 362: 363: 364: 365:
366: public function reinit($options = array())
367: {
368: $this->_allowCacheForInit = false;
369: $this->_useCache = false;
370: return $this->init($options);
371: }
372:
373: 374: 375: 376: 377: 378: 379: 380:
381: protected function _canUseLocalModules()
382: {
383: if ($this->_canUseLocalModules !== null) {
384: return $this->_canUseLocalModules;
385: }
386:
387: $disableLocalModules = (string)$this->getNode('global/disable_local_modules');
388: if (!empty($disableLocalModules)) {
389: $disableLocalModules = (('true' === $disableLocalModules) || ('1' === $disableLocalModules));
390: } else {
391: $disableLocalModules = false;
392: }
393:
394: if ($disableLocalModules && !defined('COMPILER_INCLUDE_PATH')) {
395: set_include_path(
396:
397: BP . DS . 'app' . DS . 'code' . DS . 'community' . PS .
398: BP . DS . 'app' . DS . 'code' . DS . 'core' . PS .
399: BP . DS . 'lib' . PS .
400: Mage::registry('original_include_path')
401: );
402: }
403: $this->_canUseLocalModules = !$disableLocalModules;
404: return $this->_canUseLocalModules;
405: }
406:
407: 408: 409: 410: 411:
412: protected function _canUseCacheForInit()
413: {
414: return Mage::app()->useCache('config') && $this->_allowCacheForInit
415: && !$this->_loadCache($this->_getCacheLockId());
416: }
417:
418: 419: 420: 421: 422:
423: public function getCache()
424: {
425: return Mage::app()->getCache();
426: }
427:
428: 429: 430: 431: 432:
433: protected function _getCacheLockId()
434: {
435: return $this->getCacheId().'.lock';
436: }
437:
438: 439: 440: 441: 442: 443:
444: public function saveCache($tags=array())
445: {
446: if (!Mage::app()->useCache('config')) {
447: return $this;
448: }
449: if (!in_array(self::CACHE_TAG, $tags)) {
450: $tags[] = self::CACHE_TAG;
451: }
452: $cacheLockId = $this->_getCacheLockId();
453: if ($this->_loadCache($cacheLockId)) {
454: return $this;
455: }
456:
457: if (!empty($this->_cacheSections)) {
458: $xml = clone $this->_xml;
459: foreach ($this->_cacheSections as $sectionName => $level) {
460: $this->_saveSectionCache($this->getCacheId(), $sectionName, $xml, $level, $tags);
461: unset($xml->$sectionName);
462: }
463: $this->_cachePartsForSave[$this->getCacheId()] = $xml->asNiceXml('', false);
464: } else {
465: return parent::saveCache($tags);
466: }
467:
468: $this->_saveCache(time(), $cacheLockId, array(), 60);
469: $this->removeCache();
470: foreach ($this->_cachePartsForSave as $cacheId => $cacheData) {
471: $this->_saveCache($cacheData, $cacheId, $tags, $this->getCacheLifetime());
472: }
473: unset($this->_cachePartsForSave);
474: $this->_removeCache($cacheLockId);
475: return $this;
476: }
477:
478: 479: 480: 481: 482: 483: 484: 485: 486:
487: protected function _saveSectionCache($idPrefix, $sectionName, $source, $recursionLevel=0, $tags=array())
488: {
489: if ($source && $source->$sectionName) {
490: $cacheId = $idPrefix . '_' . $sectionName;
491: if ($recursionLevel > 0) {
492: foreach ($source->$sectionName->children() as $subSectionName => $node) {
493: $this->_saveSectionCache(
494: $cacheId, $subSectionName, $source->$sectionName, $recursionLevel-1, $tags
495: );
496: }
497: }
498: $this->_cachePartsForSave[$cacheId] = $source->$sectionName->asNiceXml('', false);
499: }
500: return $this;
501: }
502:
503: 504: 505: 506: 507: 508:
509: protected function _loadSectionCache($sectionName)
510: {
511: $cacheId = $this->getCacheId() . '_' . $sectionName;
512: $xmlString = $this->_loadCache($cacheId);
513:
514: 515: 516:
517: if (!$xmlString) {
518: $this->_useCache = false;
519: $this->reinit($this->_options);
520: return false;
521: } else {
522: $xml = simplexml_load_string($xmlString, $this->_elementClass);
523: return $xml;
524: }
525: }
526:
527: 528: 529: 530: 531: 532:
533: protected function _loadCache($id)
534: {
535: return Mage::app()->loadCache($id);
536: }
537:
538: 539: 540: 541: 542: 543: 544: 545: 546:
547: protected function _saveCache($data, $id, $tags=array(), $lifetime=false)
548: {
549: return Mage::app()->saveCache($data, $id, $tags, $lifetime);
550: }
551:
552: 553: 554: 555: 556: 557:
558: protected function _removeCache($id)
559: {
560: return Mage::app()->removeCache($id);
561: }
562:
563: 564: 565: 566: 567:
568: public function removeCache()
569: {
570: Mage::app()->cleanCache(array(self::CACHE_TAG));
571: return parent::removeCache();
572: }
573:
574: 575: 576: 577: 578:
579: public function cleanCache()
580: {
581: return $this->reinit();
582: }
583:
584: 585: 586: 587: 588: 589:
590: protected function _getSectionConfig($path)
591: {
592: $section = $path[0];
593: if (!isset($this->_cacheSections[$section])) {
594: return false;
595: }
596: $sectionPath = array_slice($path, 0, $this->_cacheSections[$section]+1);
597: $sectionKey = implode('_', $sectionPath);
598:
599: if (!isset($this->_cacheLoadedSections[$sectionKey])) {
600: Varien_Profiler::start('init_config_section:' . $sectionKey);
601: $this->_cacheLoadedSections[$sectionKey] = $this->_loadSectionCache($sectionKey);
602: Varien_Profiler::stop('init_config_section:' . $sectionKey);
603: }
604:
605: if ($this->_cacheLoadedSections[$sectionKey] === false) {
606: return false;
607: }
608: return $this->_cacheLoadedSections[$sectionKey];
609: }
610:
611: 612: 613: 614: 615: 616:
617: public function getSectionNode($path)
618: {
619: $section = $path[0];
620: $config = $this->_getSectionConfig($path);
621: $path = array_slice($path, $this->_cacheSections[$section] + 1);
622: if ($config) {
623: return $config->descend($path);
624: }
625: return false;
626: }
627:
628: 629: 630: 631: 632: 633: 634: 635:
636: public function getNode($path=null, $scope='', $scopeCode=null)
637: {
638: if ($scope !== '') {
639: if (('store' === $scope) || ('website' === $scope)) {
640: $scope .= 's';
641: }
642: if (('default' !== $scope) && is_int($scopeCode)) {
643: if ('stores' == $scope) {
644: $scopeCode = Mage::app()->getStore($scopeCode)->getCode();
645: } elseif ('websites' == $scope) {
646: $scopeCode = Mage::app()->getWebsite($scopeCode)->getCode();
647: } else {
648: Mage::throwException(Mage::helper('core')->__('Unknown scope "%s".', $scope));
649: }
650: }
651: $path = $scope . ($scopeCode ? '/' . $scopeCode : '' ) . (empty($path) ? '' : '/' . $path);
652: }
653:
654: 655: 656:
657: if ($this->_useCache && ($path !== null)) {
658: $path = explode('/', $path);
659: $section= $path[0];
660: if (isset($this->_cacheSections[$section])) {
661: $res = $this->getSectionNode($path);
662: if ($res !== false) {
663: return $res;
664: }
665: }
666: }
667: return parent::getNode($path);
668: }
669:
670: 671: 672: 673: 674: 675: 676: 677:
678: public function setNode($path, $value, $overwrite = true)
679: {
680: if ($this->_useCache && ($path !== null)) {
681: $sectionPath = explode('/', $path);
682: $config = $this->_getSectionConfig($sectionPath);
683: if ($config) {
684: $sectionPath = array_slice($sectionPath, $this->_cacheSections[$sectionPath[0]]+1);
685: $sectionPath = implode('/', $sectionPath);
686: $config->setNode($sectionPath, $value, $overwrite);
687: }
688: }
689: return parent::setNode($path, $value, $overwrite);
690: }
691:
692:
693: 694: 695: 696: 697:
698: protected function _getDeclaredModuleFiles()
699: {
700: $etcDir = $this->getOptions()->getEtcDir();
701: $moduleFiles = glob($etcDir . DS . 'modules' . DS . '*.xml');
702:
703: if (!$moduleFiles) {
704: return false;
705: }
706:
707: $collectModuleFiles = array(
708: 'base' => array(),
709: 'mage' => array(),
710: 'custom' => array()
711: );
712:
713: foreach ($moduleFiles as $v) {
714: $name = explode(DIRECTORY_SEPARATOR, $v);
715: $name = substr($name[count($name) - 1], 0, -4);
716:
717: if ($name == 'Mage_All') {
718: $collectModuleFiles['base'][] = $v;
719: } else if (substr($name, 0, 5) == 'Mage_') {
720: $collectModuleFiles['mage'][] = $v;
721: } else {
722: $collectModuleFiles['custom'][] = $v;
723: }
724: }
725:
726: return array_merge(
727: $collectModuleFiles['base'],
728: $collectModuleFiles['mage'],
729: $collectModuleFiles['custom']
730: );
731: }
732:
733: 734: 735: 736: 737: 738:
739: public function addAllowedModules($module)
740: {
741: if (is_array($module)) {
742: foreach ($module as $moduleName) {
743: $this->addAllowedModules($moduleName);
744: }
745: } elseif (!in_array($module, $this->_allowedModules)) {
746: $this->_allowedModules[] = $module;
747: }
748:
749: return $this;
750: }
751:
752: 753: 754: 755: 756: 757:
758: protected function _isAllowedModule($moduleName)
759: {
760: if (empty($this->_allowedModules)) {
761: return true;
762: } else {
763: return in_array($moduleName, $this->_allowedModules);
764: }
765: }
766:
767: 768: 769: 770: 771: 772:
773: protected function _loadDeclaredModules($mergeConfig = null)
774: {
775: $moduleFiles = $this->_getDeclaredModuleFiles();
776: if (!$moduleFiles) {
777: return ;
778: }
779:
780: Varien_Profiler::start('config/load-modules-declaration');
781:
782: $unsortedConfig = new Mage_Core_Model_Config_Base();
783: $unsortedConfig->loadString('<config/>');
784: $fileConfig = new Mage_Core_Model_Config_Base();
785:
786:
787: foreach ($moduleFiles as $file) {
788: $fileConfig->loadFile($file);
789: $unsortedConfig->extend($fileConfig);
790: }
791:
792: $moduleDepends = array();
793: foreach ($unsortedConfig->getNode('modules')->children() as $moduleName => $moduleNode) {
794: if (!$this->_isAllowedModule($moduleName)) {
795: continue;
796: }
797:
798: $depends = array();
799: if ($moduleNode->depends) {
800: foreach ($moduleNode->depends->children() as $depend) {
801: $depends[$depend->getName()] = true;
802: }
803: }
804: $moduleDepends[$moduleName] = array(
805: 'module' => $moduleName,
806: 'depends' => $depends,
807: 'active' => ('true' === (string)$moduleNode->active ? true : false),
808: );
809: }
810:
811:
812: $moduleDepends = $this->_sortModuleDepends($moduleDepends);
813:
814:
815: $sortedConfig = new Mage_Core_Model_Config_Base();
816: $sortedConfig->loadString('<config><modules/></config>');
817:
818: foreach ($unsortedConfig->getNode()->children() as $nodeName => $node) {
819: if ($nodeName != 'modules') {
820: $sortedConfig->getNode()->appendChild($node);
821: }
822: }
823:
824: foreach ($moduleDepends as $moduleProp) {
825: $node = $unsortedConfig->getNode('modules/'.$moduleProp['module']);
826: $sortedConfig->getNode('modules')->appendChild($node);
827: }
828:
829: $this->extend($sortedConfig);
830:
831: Varien_Profiler::stop('config/load-modules-declaration');
832: return $this;
833: }
834:
835: 836: 837: 838: 839: 840:
841: protected function _sortModuleDepends($modules)
842: {
843: foreach ($modules as $moduleName => $moduleProps) {
844: $depends = $moduleProps['depends'];
845: foreach ($moduleProps['depends'] as $depend => $true) {
846: if ($moduleProps['active'] && ((!isset($modules[$depend])) || empty($modules[$depend]['active']))) {
847: Mage::throwException(
848: Mage::helper('core')->__('Module "%1$s" requires module "%2$s".', $moduleName, $depend)
849: );
850: }
851: $depends = array_merge($depends, $modules[$depend]['depends']);
852: }
853: $modules[$moduleName]['depends'] = $depends;
854: }
855: $modules = array_values($modules);
856:
857: $size = count($modules) - 1;
858: for ($i = $size; $i >= 0; $i--) {
859: for ($j = $size; $i < $j; $j--) {
860: if (isset($modules[$i]['depends'][$modules[$j]['module']])) {
861: $value = $modules[$i];
862: $modules[$i] = $modules[$j];
863: $modules[$j] = $value;
864: }
865: }
866: }
867:
868: $definedModules = array();
869: foreach ($modules as $moduleProp) {
870: foreach ($moduleProp['depends'] as $dependModule => $true) {
871: if (!isset($definedModules[$dependModule])) {
872: Mage::throwException(
873: Mage::helper('core')->__('Module "%1$s" cannot depend on "%2$s".', $moduleProp['module'], $dependModule)
874: );
875: }
876: }
877: $definedModules[$moduleProp['module']] = true;
878: }
879:
880: return $modules;
881: }
882:
883: 884: 885: 886: 887: 888: 889: 890:
891: public function determineOmittedNamespace($name, $asFullModuleName = false)
892: {
893: if (null === $this->_moduleNamespaces) {
894: $this->_moduleNamespaces = array();
895: foreach ($this->_xml->xpath('modules/*') as $m) {
896: if ((string)$m->active == 'true') {
897: $moduleName = $m->getName();
898: $module = strtolower($moduleName);
899: $this->_moduleNamespaces[substr($module, 0, strpos($module, '_'))][$module] = $moduleName;
900: }
901: }
902: }
903:
904: $name = explode('_', strtolower($name));
905: $partsNum = count($name);
906: $defaultNamespaceFlag = false;
907: foreach ($this->_moduleNamespaces as $namespaceName => $namespace) {
908:
909: if ($defaultNamespaceFlag === false) {
910: $defaultNamespaceFlag = true;
911: $defaultNS = $namespaceName . '_' . $name[0];
912: if (isset($namespace[$defaultNS])) {
913: return $asFullModuleName ? $namespace[$defaultNS] : $name[0];
914: }
915: }
916:
917: if(isset($name[1])) {
918: $fullNS = $name[0] . '_' . $name[1];
919: if (2 <= $partsNum && isset($namespace[$fullNS])) {
920: return $asFullModuleName ? $namespace[$fullNS] : $fullNS;
921: }
922: }
923: }
924: return '';
925: }
926:
927: 928: 929: 930: 931: 932: 933: 934:
935: public function loadModulesConfiguration($fileName, $mergeToObject = null, $mergeModel=null)
936: {
937: $disableLocalModules = !$this->_canUseLocalModules();
938:
939: if ($mergeToObject === null) {
940: $mergeToObject = clone $this->_prototype;
941: $mergeToObject->loadString('<config/>');
942: }
943: if ($mergeModel === null) {
944: $mergeModel = clone $this->_prototype;
945: }
946: $modules = $this->getNode('modules')->children();
947: foreach ($modules as $modName=>$module) {
948: if ($module->is('active')) {
949: if ($disableLocalModules && ('local' === (string)$module->codePool)) {
950: continue;
951: }
952: if (!is_array($fileName)) {
953: $fileName = array($fileName);
954: }
955:
956: foreach ($fileName as $configFile) {
957: $configFile = $this->getModuleDir('etc', $modName).DS.$configFile;
958: if ($mergeModel->loadFile($configFile)) {
959: $mergeToObject->extend($mergeModel, true);
960: }
961: }
962: }
963: }
964: return $mergeToObject;
965: }
966:
967: 968: 969: 970: 971:
972: public function getTempVarDir()
973: {
974: return $this->getOptions()->getVarDir();
975: }
976:
977: 978: 979: 980: 981:
982: public function getDistroServerVars()
983: {
984: if (!$this->_distroServerVars) {
985:
986: if (isset($_SERVER['SCRIPT_NAME']) && isset($_SERVER['HTTP_HOST'])) {
987: $secure = (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!='off')) || $_SERVER['SERVER_PORT']=='443';
988: $scheme = ($secure ? 'https' : 'http') . '://' ;
989:
990: $hostArr = explode(':', $_SERVER['HTTP_HOST']);
991: $host = $hostArr[0];
992: $port = isset(
993: $hostArr[1]) && (!$secure && $hostArr[1]!=80 || $secure && $hostArr[1]!=443
994: ) ? ':'.$hostArr[1] : '';
995: $path = Mage::app()->getRequest()->getBasePath();
996:
997: $baseUrl = $scheme.$host.$port.rtrim($path, '/').'/';
998: } else {
999: $baseUrl = 'http://localhost/';
1000: }
1001:
1002: $options = $this->getOptions();
1003: $this->_distroServerVars = array(
1004: 'root_dir' => $options->getBaseDir(),
1005: 'app_dir' => $options->getAppDir(),
1006: 'var_dir' => $options->getVarDir(),
1007: 'base_url' => $baseUrl,
1008: );
1009:
1010: foreach ($this->_distroServerVars as $k=>$v) {
1011: $this->_substServerVars['{{'.$k.'}}'] = $v;
1012: }
1013: }
1014: return $this->_distroServerVars;
1015: }
1016:
1017: public function substDistroServerVars($data)
1018: {
1019: $this->getDistroServerVars();
1020: return str_replace(
1021: array_keys($this->_substServerVars),
1022: array_values($this->_substServerVars),
1023: $data
1024: );
1025: }
1026:
1027: 1028: 1029: 1030: 1031: 1032:
1033: function getModuleConfig($moduleName='')
1034: {
1035: $modules = $this->getNode('modules');
1036: if (''===$moduleName) {
1037: return $modules;
1038: } else {
1039: return $modules->$moduleName;
1040: }
1041: }
1042:
1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050:
1051: function getModuleSetup($module='')
1052: {
1053: $className = 'Mage_Core_Setup';
1054: if (''!==$module) {
1055: if (is_string($module)) {
1056: $module = $this->getModuleConfig($module);
1057: }
1058: if (isset($module->setup)) {
1059: $moduleClassName = $module->setup->getClassName();
1060: if (!empty($moduleClassName)) {
1061: $className = $moduleClassName;
1062: }
1063: }
1064: }
1065: return new $className($module);
1066: }
1067:
1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077:
1078: public function getBaseDir($type='base')
1079: {
1080: return $this->getOptions()->getDir($type);
1081: }
1082:
1083: 1084: 1085: 1086: 1087: 1088: 1089:
1090: public function getVarDir($path=null, $type='var')
1091: {
1092: $dir = Mage::getBaseDir($type).($path!==null ? DS.$path : '');
1093: if (!$this->createDirIfNotExists($dir)) {
1094: return false;
1095: }
1096: return $dir;
1097: }
1098:
1099: public function createDirIfNotExists($dir)
1100: {
1101: return $this->getOptions()->createDirIfNotExists($dir);
1102: }
1103:
1104: 1105: 1106: 1107: 1108: 1109: 1110:
1111: public function getModuleDir($type, $moduleName)
1112: {
1113: $codePool = (string)$this->getModuleConfig($moduleName)->codePool;
1114: $dir = $this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);
1115:
1116: switch ($type) {
1117: case 'etc':
1118: $dir .= DS.'etc';
1119: break;
1120:
1121: case 'controllers':
1122: $dir .= DS.'controllers';
1123: break;
1124:
1125: case 'sql':
1126: $dir .= DS.'sql';
1127: break;
1128: case 'data':
1129: $dir .= DS.'data';
1130: break;
1131:
1132: case 'locale':
1133: $dir .= DS.'locale';
1134: break;
1135: }
1136:
1137: $dir = str_replace('/', DS, $dir);
1138: return $dir;
1139: }
1140:
1141: 1142: 1143: 1144: 1145: 1146:
1147: public function loadEventObservers($area)
1148: {
1149: $events = $this->getNode("$area/events");
1150: if ($events) {
1151: $events = $events->children();
1152: } else {
1153: return false;
1154: }
1155:
1156: foreach ($events as $event) {
1157: $eventName = $event->getName();
1158: $observers = $event->observers->children();
1159: foreach ($observers as $observer) {
1160: switch ((string)$observer->type) {
1161: case 'singleton':
1162: $callback = array(
1163: Mage::getSingleton((string)$observer->class),
1164: (string)$observer->method
1165: );
1166: break;
1167: case 'object':
1168: case 'model':
1169: $callback = array(
1170: Mage::getModel((string)$observer->class),
1171: (string)$observer->method
1172: );
1173: break;
1174: default:
1175: $callback = array($observer->getClassName(), (string)$observer->method);
1176: break;
1177: }
1178:
1179: $args = (array)$observer->args;
1180: $observerClass = $observer->observer_class ? (string)$observer->observer_class : '';
1181: Mage::addObserver($eventName, $callback, $args, $observer->getName(), $observerClass);
1182: }
1183: }
1184: return true;
1185: }
1186:
1187: 1188: 1189: 1190: 1191: 1192: 1193: 1194:
1195: public function getPathVars($args=null)
1196: {
1197: $path = array();
1198:
1199: $path['baseUrl'] = Mage::getBaseUrl();
1200: $path['baseSecureUrl'] = Mage::getBaseUrl('link', true);
1201:
1202: return $path;
1203: }
1204:
1205: 1206: 1207: 1208: 1209: 1210: 1211: 1212:
1213: public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
1214: {
1215: if (empty($groupRootNode)) {
1216: $groupRootNode = 'global/'.$groupType.'s';
1217: }
1218:
1219: $classArr = explode('/', trim($classId));
1220: $group = $classArr[0];
1221: $class = !empty($classArr[1]) ? $classArr[1] : null;
1222:
1223: if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
1224: return $this->_classNameCache[$groupRootNode][$group][$class];
1225: }
1226:
1227: $config = $this->_xml->global->{$groupType.'s'}->{$group};
1228:
1229:
1230: $className = null;
1231: if (isset($config->rewrite->$class)) {
1232: $className = (string)$config->rewrite->$class;
1233: } else {
1234: 1235: 1236: 1237: 1238:
1239: if ($config->deprecatedNode) {
1240: $deprecatedNode = $config->deprecatedNode;
1241: $configOld = $this->_xml->global->{$groupType.'s'}->$deprecatedNode;
1242: if (isset($configOld->rewrite->$class)) {
1243: $className = (string) $configOld->rewrite->$class;
1244: }
1245: }
1246: }
1247:
1248:
1249: if (empty($className)) {
1250: if (!empty($config)) {
1251: $className = $config->getClassName();
1252: }
1253: if (empty($className)) {
1254: $className = 'mage_'.$group.'_'.$groupType;
1255: }
1256: if (!empty($class)) {
1257: $className .= '_'.$class;
1258: }
1259: $className = uc_words($className);
1260: }
1261:
1262: $this->_classNameCache[$groupRootNode][$group][$class] = $className;
1263: return $className;
1264: }
1265:
1266: 1267: 1268: 1269: 1270: 1271:
1272: public function getBlockClassName($blockType)
1273: {
1274: if (strpos($blockType, '/')===false) {
1275: return $blockType;
1276: }
1277: return $this->getGroupedClassName('block', $blockType);
1278: }
1279:
1280: 1281: 1282: 1283: 1284: 1285:
1286: public function getHelperClassName($helperName)
1287: {
1288: if (strpos($helperName, '/') === false) {
1289: $helperName .= '/data';
1290: }
1291: return $this->getGroupedClassName('helper', $helperName);
1292: }
1293:
1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302: 1303:
1304: public function getResourceHelper($moduleName)
1305: {
1306: $connectionModel = $this->_getResourceConnectionModel($moduleName);
1307: $helperClass = sprintf('%s/helper_%s', $moduleName, $connectionModel);
1308: $helperClassName = $this->_getResourceModelFactoryClassName($helperClass);
1309: if ($helperClassName) {
1310: return $this->getModelInstance($helperClassName, $moduleName);
1311: }
1312:
1313: return false;
1314: }
1315:
1316: 1317: 1318: 1319: 1320: 1321:
1322: public function getModelClassName($modelClass)
1323: {
1324: $modelClass = trim($modelClass);
1325: if (strpos($modelClass, '/')===false) {
1326: return $modelClass;
1327: }
1328: return $this->getGroupedClassName('model', $modelClass);
1329: }
1330:
1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342:
1343: public function getModelInstance($modelClass='', $constructArguments=array())
1344: {
1345: $className = $this->getModelClassName($modelClass);
1346: if (class_exists($className)) {
1347: Varien_Profiler::start('CORE::create_object_of::'.$className);
1348: $obj = new $className($constructArguments);
1349: Varien_Profiler::stop('CORE::create_object_of::'.$className);
1350: return $obj;
1351: } else {
1352: return false;
1353: }
1354: }
1355:
1356: public function getNodeClassInstance($path)
1357: {
1358: $config = Mage::getConfig()->getNode($path);
1359: if (!$config) {
1360: return false;
1361: } else {
1362: $className = $config->getClassName();
1363: return new $className();
1364: }
1365: }
1366:
1367: 1368: 1369: 1370: 1371: 1372: 1373:
1374: public function getResourceModelInstance($modelClass='', $constructArguments=array())
1375: {
1376: $factoryName = $this->_getResourceModelFactoryClassName($modelClass);
1377: if (!$factoryName) {
1378: return false;
1379: }
1380: return $this->getModelInstance($factoryName, $constructArguments);
1381: }
1382:
1383: 1384: 1385: 1386: 1387: 1388:
1389: public function getResourceConfig($name)
1390: {
1391: return $this->_xml->global->resources->{$name};
1392: }
1393:
1394: 1395: 1396: 1397: 1398: 1399:
1400: public function getResourceConnectionConfig($name)
1401: {
1402: $config = $this->getResourceConfig($name);
1403: if ($config) {
1404: $conn = $config->connection;
1405: if ($conn) {
1406: if (!empty($conn->use)) {
1407: return $this->getResourceConnectionConfig((string)$conn->use);
1408: } else {
1409: return $conn;
1410: }
1411: }
1412: }
1413: return false;
1414: }
1415:
1416: 1417: 1418: 1419: 1420: 1421:
1422: public function getResourceTypeConfig($type)
1423: {
1424: return $this->_xml->global->resource->connection->types->{$type};
1425: }
1426:
1427: 1428: 1429: 1430: 1431: 1432: 1433: 1434: 1435: 1436: 1437:
1438: public function getStoresConfigByPath($path, $allowValues = array(), $useAsKey = 'id')
1439: {
1440: $storeValues = array();
1441: $stores = $this->getNode('stores');
1442: foreach ($stores->children() as $code => $store) {
1443: switch ($useAsKey) {
1444: case 'id':
1445: $key = (int) $store->descend('system/store/id');
1446: break;
1447:
1448: case 'code':
1449: $key = $code;
1450: break;
1451:
1452: case 'name':
1453: $key = (string) $store->descend('system/store/name');
1454: }
1455: if ($key === false) {
1456: continue;
1457: }
1458:
1459: $pathValue = (string) $store->descend($path);
1460:
1461: if (empty($allowValues)) {
1462: $storeValues[$key] = $pathValue;
1463: } else if (in_array($pathValue, $allowValues)) {
1464: $storeValues[$key] = $pathValue;
1465: }
1466: }
1467: return $storeValues;
1468: }
1469:
1470: 1471: 1472: 1473: 1474: 1475: 1476:
1477: public function shouldUrlBeSecure($url)
1478: {
1479: if (!Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_SECURE_IN_FRONTEND)) {
1480: return false;
1481: }
1482:
1483: if (!isset($this->_secureUrlCache[$url])) {
1484: $this->_secureUrlCache[$url] = false;
1485: $secureUrls = $this->getNode('frontend/secure_url');
1486: foreach ($secureUrls->children() as $match) {
1487: if (strpos($url, (string)$match) === 0) {
1488: $this->_secureUrlCache[$url] = true;
1489: break;
1490: }
1491: }
1492: }
1493:
1494: return $this->_secureUrlCache[$url];
1495: }
1496:
1497: 1498: 1499: 1500: 1501:
1502: public function getTablePrefix()
1503: {
1504: return $this->_xml->global->resources->db->table_prefix;
1505: }
1506:
1507: 1508: 1509: 1510: 1511: 1512: 1513:
1514: public function getEventConfig($area, $eventName)
1515: {
1516:
1517: if (!isset($this->_eventAreas[$area])) {
1518: $this->_eventAreas[$area] = $this->getNode($area)->events;
1519: }
1520: return $this->_eventAreas[$area]->{$eventName};
1521: }
1522:
1523: 1524: 1525: 1526: 1527: 1528: 1529: 1530: 1531:
1532: public function saveConfig($path, $value, $scope = 'default', $scopeId = 0)
1533: {
1534: $resource = $this->getResourceModel();
1535: $resource->saveConfig(rtrim($path, '/'), $value, $scope, $scopeId);
1536:
1537: return $this;
1538: }
1539:
1540: 1541: 1542: 1543: 1544: 1545: 1546: 1547:
1548: public function deleteConfig($path, $scope = 'default', $scopeId = 0)
1549: {
1550: $resource = $this->getResourceModel();
1551: $resource->deleteConfig(rtrim($path, '/'), $scope, $scopeId);
1552:
1553: return $this;
1554: }
1555:
1556: 1557: 1558: 1559: 1560: 1561: 1562:
1563: public function getFieldset($name, $root = 'global')
1564: {
1565: $rootNode = $this->getNode($root.'/fieldsets');
1566: if (!$rootNode) {
1567: return null;
1568: }
1569: return $rootNode->$name ? $rootNode->$name->children() : null;
1570: }
1571:
1572: 1573: 1574: 1575: 1576: 1577:
1578: protected function _getResourceConnectionModel($moduleName = null)
1579: {
1580: $config = null;
1581: if (!is_null($moduleName)) {
1582: $setupResource = $moduleName . '_setup';
1583: $config = $this->getResourceConnectionConfig($setupResource);
1584: }
1585: if (!$config) {
1586: $config = $this->getResourceConnectionConfig(Mage_Core_Model_Resource::DEFAULT_SETUP_RESOURCE);
1587: }
1588:
1589: return (string)$config->model;
1590: }
1591:
1592: 1593: 1594: 1595: 1596: 1597:
1598: protected function _getResourceModelFactoryClassName($modelClass)
1599: {
1600: $classArray = explode('/', $modelClass);
1601: if (count($classArray) != 2) {
1602: return false;
1603: }
1604:
1605: list($module, $model) = $classArray;
1606: if (!isset($this->_xml->global->models->{$module})) {
1607: return false;
1608: }
1609:
1610: $moduleNode = $this->_xml->global->models->{$module};
1611: if (!empty($moduleNode->resourceModel)) {
1612: $resourceModel = (string)$moduleNode->resourceModel;
1613: } else {
1614: return false;
1615: }
1616:
1617: return $resourceModel . '/' . $model;
1618: }
1619:
1620: 1621: 1622: 1623: 1624: 1625:
1626: public function getResourceModelClassName($modelClass)
1627: {
1628: $factoryName = $this->_getResourceModelFactoryClassName($modelClass);
1629: if ($factoryName) {
1630: return $this->getModelClassName($factoryName);
1631: }
1632: return false;
1633: }
1634: }
1635: