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:
36: class Mage_Core_Model_App
37: {
38:
39: const XML_PATH_INSTALL_DATE = 'global/install/date';
40:
41: const XML_PATH_SKIP_PROCESS_MODULES_UPDATES = 'global/skip_process_modules_updates';
42:
43: 44: 45:
46: const XML_PATH_IGNORE_DEV_MODE = 'global/skip_process_modules_updates_ignore_dev_mode';
47:
48: const DEFAULT_ERROR_HANDLER = 'mageCoreErrorHandler';
49:
50: const DISTRO_LOCALE_CODE = 'en_US';
51:
52: 53: 54: 55:
56: const CACHE_TAG = 'MAGE';
57:
58: 59: 60:
61: const DISTRO_STORE_ID = 1;
62:
63: 64: 65: 66:
67: const DISTRO_STORE_CODE = 'default';
68:
69: 70: 71: 72:
73: const ADMIN_STORE_ID = 0;
74:
75: 76: 77: 78: 79:
80: protected $_areas = array();
81:
82: 83: 84: 85: 86:
87: protected $_store;
88:
89: 90: 91: 92: 93:
94: protected $_website;
95:
96: 97: 98: 99: 100:
101: protected $_locale;
102:
103: 104: 105: 106: 107:
108: protected $_translator;
109:
110: 111: 112: 113: 114:
115: protected $_design;
116:
117: 118: 119: 120: 121:
122: protected $_layout;
123:
124: 125: 126: 127: 128:
129: protected $_config;
130:
131: 132: 133: 134: 135:
136: protected $_frontController;
137:
138: 139: 140: 141: 142:
143: protected $_cache;
144:
145: 146: 147: 148: 149:
150: protected $_useCache;
151:
152: 153: 154: 155: 156:
157: protected $_websites = array();
158:
159: 160: 161: 162: 163:
164: protected $_groups = array();
165:
166: 167: 168: 169: 170:
171: protected $_stores = array();
172:
173: 174: 175: 176: 177:
178: protected $_isSingleStore;
179:
180: 181: 182:
183: protected $_isSingleStoreAllowed = true;
184:
185: 186: 187: 188: 189:
190: protected $_currentStore;
191:
192: 193: 194: 195: 196:
197: protected $_request;
198:
199: 200: 201: 202: 203:
204: protected $_response;
205:
206:
207: 208: 209: 210: 211:
212: protected $_events = array();
213:
214: 215: 216: 217: 218:
219: protected $_updateMode = false;
220:
221: 222: 223: 224: 225: 226:
227: protected $_useSessionInUrl = true;
228:
229: 230: 231: 232: 233:
234: protected $_useSessionVar = false;
235:
236: 237: 238: 239: 240:
241: protected $_isCacheLocked = null;
242:
243: 244: 245:
246: public function __construct()
247: {
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257:
258: public function init($code, $type = null, $options = array())
259: {
260: $this->_initEnvironment();
261: if (is_string($options)) {
262: $options = array('etc_dir'=>$options);
263: }
264:
265: Varien_Profiler::start('mage::app::init::config');
266: $this->_config = Mage::getConfig();
267: $this->_config->setOptions($options);
268: $this->_initBaseConfig();
269: $this->_initCache();
270: $this->_config->init($options);
271: Varien_Profiler::stop('mage::app::init::config');
272:
273: if (Mage::isInstalled($options)) {
274: $this->_initCurrentStore($code, $type);
275: $this->_initRequest();
276: }
277: return $this;
278: }
279:
280: 281: 282: 283: 284: 285:
286: public function baseInit($options)
287: {
288: $this->_initEnvironment();
289:
290: $this->_config = Mage::getConfig();
291: $this->_config->setOptions($options);
292:
293: $this->_initBaseConfig();
294: $cacheInitOptions = is_array($options) && array_key_exists('cache', $options) ? $options['cache'] : array();
295: $this->_initCache($cacheInitOptions);
296:
297: return $this;
298: }
299:
300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310:
311: public function initSpecified($scopeCode, $scopeType = null, $options = array(), $modules = array())
312: {
313: $this->baseInit($options);
314:
315: if (!empty($modules)) {
316: $this->_config->addAllowedModules($modules);
317: }
318: $this->_initModules();
319: $this->_initCurrentStore($scopeCode, $scopeType);
320:
321: return $this;
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function run($params)
335: {
336: $options = isset($params['options']) ? $params['options'] : array();
337: $this->baseInit($options);
338: Mage::register('application_params', $params);
339:
340: if ($this->_cache->processRequest()) {
341: $this->getResponse()->sendResponse();
342: } else {
343: $this->_initModules();
344: $this->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
345:
346: if ($this->_config->isLocalConfigLoaded()) {
347: $scopeCode = isset($params['scope_code']) ? $params['scope_code'] : '';
348: $scopeType = isset($params['scope_type']) ? $params['scope_type'] : 'store';
349: $this->_initCurrentStore($scopeCode, $scopeType);
350: $this->_initRequest();
351: Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
352: }
353:
354: $this->getFrontController()->dispatch();
355: }
356: return $this;
357: }
358:
359: 360: 361: 362: 363:
364: protected function _initEnvironment()
365: {
366: $this->setErrorHandler(self::DEFAULT_ERROR_HANDLER);
367: date_default_timezone_set(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
368: return $this;
369: }
370:
371: 372: 373: 374: 375: 376:
377: protected function _initBaseConfig()
378: {
379: Varien_Profiler::start('mage::app::init::system_config');
380: $this->_config->loadBase();
381: Varien_Profiler::stop('mage::app::init::system_config');
382: return $this;
383: }
384:
385: 386: 387: 388: 389: 390:
391: protected function _initCache(array $cacheInitOptions = array())
392: {
393: $this->_isCacheLocked = true;
394: $options = $this->_config->getNode('global/cache');
395: if ($options) {
396: $options = $options->asArray();
397: } else {
398: $options = array();
399: }
400: $options = array_merge($options, $cacheInitOptions);
401: $this->_cache = Mage::getModel('core/cache', $options);
402: $this->_isCacheLocked = false;
403: return $this;
404: }
405:
406: 407: 408: 409: 410:
411: protected function _initModules()
412: {
413: if (!$this->_config->loadModulesCache()) {
414: $this->_config->loadModules();
415: if ($this->_config->isLocalConfigLoaded() && !$this->_shouldSkipProcessModulesUpdates()) {
416: Varien_Profiler::start('mage::app::init::apply_db_schema_updates');
417: Mage_Core_Model_Resource_Setup::applyAllUpdates();
418: Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
419: }
420: $this->_config->loadDb();
421: $this->_config->saveCache();
422: }
423: return $this;
424: }
425:
426: 427: 428: 429: 430:
431: protected function _shouldSkipProcessModulesUpdates()
432: {
433: if (!Mage::isInstalled()) {
434: return false;
435: }
436:
437: $ignoreDevelopmentMode = (bool)(string)$this->_config->getNode(self::XML_PATH_IGNORE_DEV_MODE);
438: if (Mage::getIsDeveloperMode() && !$ignoreDevelopmentMode) {
439: return false;
440: }
441:
442: return (bool)(string)$this->_config->getNode(self::XML_PATH_SKIP_PROCESS_MODULES_UPDATES);
443: }
444:
445: 446: 447: 448: 449:
450: protected function _initRequest()
451: {
452: $this->getRequest()->setPathInfo();
453: return $this;
454: }
455:
456: 457: 458: 459: 460: 461: 462:
463: protected function _initCurrentStore($scopeCode, $scopeType)
464: {
465: Varien_Profiler::start('mage::app::init::stores');
466: $this->_initStores();
467: Varien_Profiler::stop('mage::app::init::stores');
468:
469: if (empty($scopeCode) && !is_null($this->_website)) {
470: $scopeCode = $this->_website->getCode();
471: $scopeType = 'website';
472: }
473: switch ($scopeType) {
474: case 'store':
475: $this->_currentStore = $scopeCode;
476: break;
477: case 'group':
478: $this->_currentStore = $this->_getStoreByGroup($scopeCode);
479: break;
480: case 'website':
481: $this->_currentStore = $this->_getStoreByWebsite($scopeCode);
482: break;
483: default:
484: $this->throwStoreException();
485: }
486:
487: if (!empty($this->_currentStore)) {
488: $this->_checkCookieStore($scopeType);
489: $this->_checkGetStore($scopeType);
490: }
491: $this->_useSessionInUrl = $this->getStore()->getConfig(
492: Mage_Core_Model_Session_Abstract::XML_PATH_USE_FRONTEND_SID);
493: return $this;
494: }
495:
496: 497: 498: 499: 500:
501: public function getCookie()
502: {
503: return Mage::getSingleton('core/cookie');
504: }
505:
506: 507: 508: 509: 510:
511: protected function _checkGetStore($type)
512: {
513: if (empty($_GET)) {
514: return $this;
515: }
516:
517: 518: 519:
520: if (!isset($_GET['___store'])) {
521: return $this;
522: }
523:
524: $store = $_GET['___store'];
525: if (!isset($this->_stores[$store])) {
526: return $this;
527: }
528:
529: $storeObj = $this->_stores[$store];
530: if (!$storeObj->getId() || !$storeObj->getIsActive()) {
531: return $this;
532: }
533:
534: 535: 536: 537:
538: $curStoreObj = $this->_stores[$this->_currentStore];
539: if ($type == 'website' && $storeObj->getWebsiteId() == $curStoreObj->getWebsiteId()) {
540: $this->_currentStore = $store;
541: }
542: elseif ($type == 'group' && $storeObj->getGroupId() == $curStoreObj->getGroupId()) {
543: $this->_currentStore = $store;
544: }
545: elseif ($type == 'store') {
546: $this->_currentStore = $store;
547: }
548:
549: if ($this->_currentStore == $store) {
550: $store = $this->getStore($store);
551: if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
552: $this->getCookie()->delete(Mage_Core_Model_Store::COOKIE_NAME);
553: } else {
554: $this->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $this->_currentStore, true);
555: }
556: }
557: return $this;
558: }
559:
560: 561: 562: 563: 564: 565:
566: protected function _checkCookieStore($type)
567: {
568: if (!$this->getCookie()->get()) {
569: return $this;
570: }
571:
572: $store = $this->getCookie()->get(Mage_Core_Model_Store::COOKIE_NAME);
573: if ($store && isset($this->_stores[$store])
574: && $this->_stores[$store]->getId()
575: && $this->_stores[$store]->getIsActive()) {
576: if ($type == 'website'
577: && $this->_stores[$store]->getWebsiteId() == $this->_stores[$this->_currentStore]->getWebsiteId()) {
578: $this->_currentStore = $store;
579: }
580: if ($type == 'group'
581: && $this->_stores[$store]->getGroupId() == $this->_stores[$this->_currentStore]->getGroupId()) {
582: $this->_currentStore = $store;
583: }
584: if ($type == 'store') {
585: $this->_currentStore = $store;
586: }
587: }
588: return $this;
589: }
590:
591: public function reinitStores()
592: {
593: return $this->_initStores();
594: }
595:
596: 597: 598: 599:
600: protected function _initStores()
601: {
602: $this->_stores = array();
603: $this->_groups = array();
604: $this->_website = null;
605: $this->_websites = array();
606:
607:
608: $websiteCollection = Mage::getModel('core/website')->getCollection()
609: ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Website::CACHE_TAG))
610: ->setLoadDefault(true);
611:
612:
613: $groupCollection = Mage::getModel('core/store_group')->getCollection()
614: ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store_Group::CACHE_TAG))
615: ->setLoadDefault(true);
616:
617:
618: $storeCollection = Mage::getModel('core/store')->getCollection()
619: ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store::CACHE_TAG))
620: ->setLoadDefault(true);
621:
622: $this->_isSingleStore = false;
623: if ($this->_isSingleStoreAllowed) {
624: $this->_isSingleStore = $storeCollection->count() < 3;
625: }
626:
627: $websiteStores = array();
628: $websiteGroups = array();
629: $groupStores = array();
630:
631: foreach ($storeCollection as $store) {
632:
633: $store->initConfigCache();
634: $store->setWebsite($websiteCollection->getItemById($store->getWebsiteId()));
635: $store->setGroup($groupCollection->getItemById($store->getGroupId()));
636:
637: $this->_stores[$store->getId()] = $store;
638: $this->_stores[$store->getCode()] = $store;
639:
640: $websiteStores[$store->getWebsiteId()][$store->getId()] = $store;
641: $groupStores[$store->getGroupId()][$store->getId()] = $store;
642:
643: if (is_null($this->_store) && $store->getId()) {
644: $this->_store = $store;
645: }
646: }
647:
648: foreach ($groupCollection as $group) {
649:
650: if (!isset($groupStores[$group->getId()])) {
651: $groupStores[$group->getId()] = array();
652: }
653: $group->setStores($groupStores[$group->getId()]);
654: $group->setWebsite($websiteCollection->getItemById($group->getWebsiteId()));
655:
656: $websiteGroups[$group->getWebsiteId()][$group->getId()] = $group;
657:
658: $this->_groups[$group->getId()] = $group;
659: }
660:
661: foreach ($websiteCollection as $website) {
662:
663: if (!isset($websiteGroups[$website->getId()])) {
664: $websiteGroups[$website->getId()] = array();
665: }
666: if (!isset($websiteStores[$website->getId()])) {
667: $websiteStores[$website->getId()] = array();
668: }
669: if ($website->getIsDefault()) {
670: $this->_website = $website;
671: }
672: $website->setGroups($websiteGroups[$website->getId()]);
673: $website->setStores($websiteStores[$website->getId()]);
674:
675: $this->_websites[$website->getId()] = $website;
676: $this->_websites[$website->getCode()] = $website;
677: }
678: }
679:
680: 681: 682: 683: 684:
685: public function isSingleStoreMode()
686: {
687: if (!Mage::isInstalled()) {
688: return false;
689: }
690: return $this->_isSingleStore;
691: }
692:
693: 694: 695: 696: 697: 698:
699: protected function _getStoreByGroup($group)
700: {
701: if (!isset($this->_groups[$group])) {
702: return null;
703: }
704: if (!$this->_groups[$group]->getDefaultStoreId()) {
705: return null;
706: }
707: return $this->_stores[$this->_groups[$group]->getDefaultStoreId()]->getCode();
708: }
709:
710: 711: 712: 713: 714: 715:
716: protected function _getStoreByWebsite($website)
717: {
718: if (!isset($this->_websites[$website])) {
719: return null;
720: }
721: if (!$this->_websites[$website]->getDefaultGroupId()) {
722: return null;
723: }
724: return $this->_getStoreByGroup($this->_websites[$website]->getDefaultGroupId());
725: }
726:
727: 728: 729: 730: 731: 732:
733: public function setCurrentStore($store)
734: {
735: $this->_currentStore = $store;
736: return $this;
737: }
738:
739: 740: 741: 742: 743:
744: protected function _initFrontController()
745: {
746: $this->_frontController = new Mage_Core_Controller_Varien_Front();
747: Mage::register('controller', $this->_frontController);
748: Varien_Profiler::start('mage::app::init_front_controller');
749: $this->_frontController->init();
750: Varien_Profiler::stop('mage::app::init_front_controller');
751: return $this;
752: }
753:
754: 755: 756: 757: 758: 759:
760: public function setErrorHandler($handler)
761: {
762: set_error_handler($handler);
763: return $this;
764: }
765:
766: 767: 768: 769: 770: 771:
772: public function loadArea($code)
773: {
774: $this->getArea($code)->load();
775: return $this;
776: }
777:
778: 779: 780: 781: 782: 783: 784:
785: public function loadAreaPart($area, $part)
786: {
787: $this->getArea($area)->load($part);
788: return $this;
789: }
790:
791: 792: 793: 794: 795: 796:
797: public function getArea($code)
798: {
799: if (!isset($this->_areas[$code])) {
800: $this->_areas[$code] = new Mage_Core_Model_App_Area($code, $this);
801: }
802: return $this->_areas[$code];
803: }
804:
805: 806: 807: 808: 809: 810: 811:
812: public function getStore($id = null)
813: {
814: if (!Mage::isInstalled() || $this->getUpdateMode()) {
815: return $this->_getDefaultStore();
816: }
817:
818: if ($id === true && $this->isSingleStoreMode()) {
819: return $this->_store;
820: }
821:
822: if (!isset($id) || ''===$id || $id === true) {
823: $id = $this->_currentStore;
824: }
825: if ($id instanceof Mage_Core_Model_Store) {
826: return $id;
827: }
828: if (!isset($id)) {
829: $this->throwStoreException();
830: }
831:
832: if (empty($this->_stores[$id])) {
833: $store = Mage::getModel('core/store');
834:
835: if (is_numeric($id)) {
836: $store->load($id);
837: } elseif (is_string($id)) {
838: $store->load($id, 'code');
839: }
840:
841: if (!$store->getCode()) {
842: $this->throwStoreException();
843: }
844: $this->_stores[$store->getStoreId()] = $store;
845: $this->_stores[$store->getCode()] = $store;
846: }
847: return $this->_stores[$id];
848: }
849:
850: 851: 852: 853: 854: 855:
856: public function getSafeStore($id = null)
857: {
858: try {
859: return $this->getStore($id);
860: }
861: catch (Exception $e) {
862: if ($this->_currentStore) {
863: $this->getRequest()->setActionName('noRoute');
864: return new Varien_Object();
865: }
866: else {
867: Mage::throwException(Mage::helper('core')->__('Requested invalid store "%s"', $id));
868: }
869: }
870: }
871:
872: 873: 874: 875: 876: 877: 878:
879: public function getStores($withDefault = false, $codeKey = false)
880: {
881: $stores = array();
882: foreach ($this->_stores as $store) {
883: if (!$withDefault && $store->getId() == 0) {
884: continue;
885: }
886: if ($codeKey) {
887: $stores[$store->getCode()] = $store;
888: }
889: else {
890: $stores[$store->getId()] = $store;
891: }
892: }
893:
894: return $stores;
895: }
896:
897: protected function _getDefaultStore()
898: {
899: if (empty($this->_store)) {
900: $this->_store = Mage::getModel('core/store')
901: ->setId(self::DISTRO_STORE_ID)
902: ->setCode(self::DISTRO_STORE_CODE);
903: }
904: return $this->_store;
905: }
906:
907: 908: 909: 910: 911:
912: public function getDefaultStoreView()
913: {
914: foreach ($this->getWebsites() as $_website) {
915: if ($_website->getIsDefault()) {
916: $_defaultStore = $this->getGroup($_website->getDefaultGroupId())->getDefaultStore();
917: if ($_defaultStore) {
918: return $_defaultStore;
919: }
920: }
921: }
922: return null;
923: }
924:
925: public function getDistroLocaleCode()
926: {
927: return self::DISTRO_LOCALE_CODE;
928: }
929:
930: 931: 932: 933: 934:
935: public function getWebsite($id=null)
936: {
937: if (is_null($id)) {
938: $id = $this->getStore()->getWebsiteId();
939: } elseif ($id instanceof Mage_Core_Model_Website) {
940: return $id;
941: } elseif ($id === true) {
942: return $this->_website;
943: }
944:
945: if (empty($this->_websites[$id])) {
946: $website = Mage::getModel('core/website');
947: if (is_numeric($id)) {
948: $website->load($id);
949: if (!$website->hasWebsiteId()) {
950: throw Mage::exception('Mage_Core', 'Invalid website id requested.');
951: }
952: } elseif (is_string($id)) {
953: $websiteConfig = $this->_config->getNode('websites/'.$id);
954: if (!$websiteConfig) {
955: throw Mage::exception('Mage_Core', 'Invalid website code requested: '.$id);
956: }
957: $website->loadConfig($id);
958: }
959: $this->_websites[$website->getWebsiteId()] = $website;
960: $this->_websites[$website->getCode()] = $website;
961: }
962: return $this->_websites[$id];
963: }
964:
965: public function getWebsites($withDefault = false, $codeKey = false)
966: {
967: $websites = array();
968: if (is_array($this->_websites)) {
969: foreach ($this->_websites as $website) {
970: if (!$withDefault && $website->getId() == 0) {
971: continue;
972: }
973: if ($codeKey) {
974: $websites[$website->getCode()] = $website;
975: }
976: else {
977: $websites[$website->getId()] = $website;
978: }
979: }
980: }
981:
982: return $websites;
983: }
984:
985: 986: 987: 988: 989:
990: public function getGroup($id=null)
991: {
992: if (is_null($id)) {
993: $id = $this->getStore()->getGroup()->getId();
994: } elseif ($id instanceof Mage_Core_Model_Store_Group) {
995: return $id;
996: }
997: if (empty($this->_groups[$id])) {
998: $group = Mage::getModel('core/store_group');
999: if (is_numeric($id)) {
1000: $group->load($id);
1001: if (!$group->hasGroupId()) {
1002: throw Mage::exception('Mage_Core', 'Invalid store group id requested.');
1003: }
1004: }
1005: $this->_groups[$group->getGroupId()] = $group;
1006: }
1007: return $this->_groups[$id];
1008: }
1009:
1010: 1011: 1012: 1013: 1014:
1015: public function getLocale()
1016: {
1017: if (!$this->_locale) {
1018: $this->_locale = Mage::getSingleton('core/locale');
1019: }
1020: return $this->_locale;
1021: }
1022:
1023: 1024: 1025: 1026: 1027:
1028: public function getLayout()
1029: {
1030: if (!$this->_layout) {
1031: if ($this->getFrontController()->getAction()) {
1032: $this->_layout = $this->getFrontController()->getAction()->getLayout();
1033: } else {
1034: $this->_layout = Mage::getSingleton('core/layout');
1035: }
1036: }
1037: return $this->_layout;
1038: }
1039:
1040: 1041: 1042: 1043: 1044:
1045: public function getTranslator()
1046: {
1047: if (!$this->_translator) {
1048: $this->_translator = Mage::getSingleton('core/translate');
1049: }
1050: return $this->_translator;
1051: }
1052:
1053: 1054: 1055: 1056: 1057: 1058:
1059: public function getHelper($name)
1060: {
1061: return Mage::helper($name);
1062: }
1063:
1064: 1065: 1066: 1067: 1068:
1069: public function getBaseCurrencyCode()
1070: {
1071:
1072: return (string) Mage::app()->getConfig()
1073: ->getNode('default/' . Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
1074: }
1075:
1076: 1077: 1078: 1079: 1080:
1081: public function getConfig()
1082: {
1083: return $this->_config;
1084: }
1085:
1086: 1087: 1088: 1089: 1090:
1091: public function getFrontController()
1092: {
1093: if (!$this->_frontController) {
1094: $this->_initFrontController();
1095: }
1096:
1097: return $this->_frontController;
1098: }
1099:
1100: 1101: 1102: 1103: 1104:
1105: public function getCacheInstance()
1106: {
1107: if (!$this->_cache) {
1108: $this->_initCache();
1109: }
1110: return $this->_cache;
1111: }
1112:
1113: 1114: 1115: 1116: 1117:
1118: public function getCache()
1119: {
1120: if (!$this->_cache) {
1121: $this->_initCache();
1122: }
1123: return $this->_cache->getFrontend();
1124: }
1125:
1126: 1127: 1128: 1129: 1130: 1131:
1132: public function loadCache($id)
1133: {
1134: return $this->_cache->load($id);
1135: }
1136:
1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144:
1145: public function saveCache($data, $id, $tags=array(), $lifeTime=false)
1146: {
1147: $this->_cache->save($data, $id, $tags, $lifeTime);
1148: return $this;
1149: }
1150:
1151: 1152: 1153: 1154: 1155: 1156:
1157: public function removeCache($id)
1158: {
1159: $this->_cache->remove($id);
1160: return $this;
1161: }
1162:
1163: 1164: 1165: 1166: 1167: 1168:
1169: public function cleanCache($tags=array())
1170: {
1171: $this->_cache->clean($tags);
1172: Mage::dispatchEvent('application_clean_cache', array('tags' => $tags));
1173: return $this;
1174: }
1175:
1176: 1177: 1178: 1179: 1180:
1181: public function useCache($type=null)
1182: {
1183: return $this->_cache->canUse($type);
1184: }
1185:
1186: 1187: 1188: 1189: 1190: 1191:
1192: public function saveUseCache($data)
1193: {
1194: $this->_cache->saveOptions($data);
1195: return $this;
1196: }
1197:
1198: 1199: 1200: 1201:
1202: public function cleanAllSessions()
1203: {
1204: if (session_module_name()=='files') {
1205: $dir = session_save_path();
1206: mageDelTree($dir);
1207: }
1208: return $this;
1209: }
1210:
1211: 1212: 1213: 1214: 1215:
1216: public function getRequest()
1217: {
1218: if (empty($this->_request)) {
1219: $this->_request = new Mage_Core_Controller_Request_Http();
1220: }
1221: return $this->_request;
1222: }
1223:
1224: 1225: 1226: 1227: 1228: 1229:
1230: public function setRequest(Mage_Core_Controller_Request_Http $request)
1231: {
1232: $this->_request = $request;
1233: return $this;
1234: }
1235:
1236: 1237: 1238: 1239: 1240:
1241: public function getResponse()
1242: {
1243: if (empty($this->_response)) {
1244: $this->_response = new Mage_Core_Controller_Response_Http();
1245: $this->_response->headersSentThrowsException = Mage::$headersSentThrowsException;
1246: $this->_response->setHeader("Content-Type", "text/html; charset=UTF-8");
1247: }
1248: return $this->_response;
1249: }
1250:
1251: 1252: 1253: 1254: 1255: 1256:
1257: public function setResponse(Mage_Core_Controller_Response_Http $response)
1258: {
1259: $this->_response = $response;
1260: return $this;
1261: }
1262:
1263: public function addEventArea($area)
1264: {
1265: if (!isset($this->_events[$area])) {
1266: $this->_events[$area] = array();
1267: }
1268: return $this;
1269: }
1270:
1271: public function dispatchEvent($eventName, $args)
1272: {
1273: foreach ($this->_events as $area=>$events) {
1274: if (!isset($events[$eventName])) {
1275: $eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
1276: if (!$eventConfig) {
1277: $this->_events[$area][$eventName] = false;
1278: continue;
1279: }
1280: $observers = array();
1281: foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
1282: $observers[$obsName] = array(
1283: 'type' => (string)$obsConfig->type,
1284: 'model' => $obsConfig->class ? (string)$obsConfig->class : $obsConfig->getClassName(),
1285: 'method'=> (string)$obsConfig->method,
1286: 'args' => (array)$obsConfig->args,
1287: );
1288: }
1289: $events[$eventName]['observers'] = $observers;
1290: $this->_events[$area][$eventName]['observers'] = $observers;
1291: }
1292: if (false===$events[$eventName]) {
1293: continue;
1294: } else {
1295: $event = new Varien_Event($args);
1296: $event->setName($eventName);
1297: $observer = new Varien_Event_Observer();
1298: }
1299:
1300: foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
1301: $observer->setData(array('event'=>$event));
1302: Varien_Profiler::start('OBSERVER: '.$obsName);
1303: switch ($obs['type']) {
1304: case 'disabled':
1305: break;
1306: case 'object':
1307: case 'model':
1308: $method = $obs['method'];
1309: $observer->addData($args);
1310: $object = Mage::getModel($obs['model']);
1311: $this->_callObserverMethod($object, $method, $observer);
1312: break;
1313: default:
1314: $method = $obs['method'];
1315: $observer->addData($args);
1316: $object = Mage::getSingleton($obs['model']);
1317: $this->_callObserverMethod($object, $method, $observer);
1318: break;
1319: }
1320: Varien_Profiler::stop('OBSERVER: '.$obsName);
1321: }
1322: }
1323: return $this;
1324: }
1325:
1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333: 1334:
1335: protected function _callObserverMethod($object, $method, $observer)
1336: {
1337: if (method_exists($object, $method)) {
1338: $object->$method($observer);
1339: } elseif (Mage::getIsDeveloperMode()) {
1340: Mage::throwException('Method "'.$method.'" is not defined in "'.get_class($object).'"');
1341: }
1342: return $this;
1343: }
1344:
1345: public function setUpdateMode($value)
1346: {
1347: $this->_updateMode = $value;
1348: }
1349:
1350: public function getUpdateMode()
1351: {
1352: return $this->_updateMode;
1353: }
1354:
1355: public function throwStoreException()
1356: {
1357: throw new Mage_Core_Model_Store_Exception('');
1358: }
1359:
1360: 1361: 1362: 1363: 1364: 1365:
1366: public function setUseSessionVar($var)
1367: {
1368: $this->_useSessionVar = (bool)$var;
1369: return $this;
1370: }
1371:
1372: 1373: 1374: 1375: 1376:
1377: public function getUseSessionVar()
1378: {
1379: return $this->_useSessionVar;
1380: }
1381:
1382: 1383: 1384: 1385: 1386:
1387: public function getAnyStoreView()
1388: {
1389: $store = $this->getDefaultStoreView();
1390: if ($store) {
1391: return $store;
1392: }
1393: foreach ($this->getStores() as $store) {
1394: return $store;
1395: }
1396: }
1397:
1398: 1399: 1400: 1401: 1402: 1403:
1404: public function setUseSessionInUrl($flag = true)
1405: {
1406: $this->_useSessionInUrl = (bool)$flag;
1407: return $this;
1408: }
1409:
1410: 1411: 1412: 1413: 1414:
1415: public function getUseSessionInUrl()
1416: {
1417: return $this->_useSessionInUrl;
1418: }
1419:
1420: 1421: 1422: 1423: 1424: 1425:
1426: public function setIsSingleStoreModeAllowed($value)
1427: {
1428: $this->_isSingleStoreAllowed = (bool)$value;
1429: return $this;
1430: }
1431:
1432: 1433: 1434: 1435: 1436: 1437: 1438: 1439: 1440:
1441: public function getGroups($withDefault = false, $codeKey = false)
1442: {
1443: $groups = array();
1444: if (is_array($this->_groups)) {
1445: foreach ($this->_groups as $group) {
1446: if (!$withDefault && $group->getId() == 0) {
1447: continue;
1448: }
1449: if ($codeKey) {
1450: $groups[$group->getCode()] = $group;
1451: }
1452: else {
1453: $groups[$group->getId()] = $group;
1454: }
1455: }
1456: }
1457:
1458: return $groups;
1459: }
1460:
1461:
1462:
1463:
1464: 1465: 1466: 1467: 1468: 1469:
1470: public function isInstalled()
1471: {
1472: return Mage::isInstalled();
1473: }
1474:
1475: 1476: 1477: 1478: 1479: 1480: 1481: 1482:
1483: protected function _getCacheTags($tags=array())
1484: {
1485: foreach ($tags as $index=>$value) {
1486: $tags[$index] = $this->_getCacheId($value);
1487: }
1488: return $tags;
1489: }
1490:
1491: 1492: 1493: 1494: 1495: 1496:
1497: public function getUseCacheFilename()
1498: {
1499: return $this->_config->getOptions()->getEtcDir().DS.'use_cache.ser';
1500: }
1501:
1502: 1503: 1504: 1505: 1506: 1507: 1508:
1509: protected function _getCacheId($id=null)
1510: {
1511: if ($id) {
1512: $id = $this->prepareCacheId($id);
1513: }
1514: return $id;
1515: }
1516:
1517: 1518: 1519: 1520: 1521: 1522: 1523:
1524: public function prepareCacheId($id)
1525: {
1526: $id = strtoupper($id);
1527: $id = preg_replace('/([^a-zA-Z0-9_]{1,1})/', '_', $id);
1528: return $id;
1529: }
1530:
1531: 1532: 1533: 1534: 1535:
1536: public function getIsCacheLocked()
1537: {
1538: return (bool)$this->_isCacheLocked;
1539: }
1540:
1541: 1542: 1543: 1544: 1545: 1546:
1547: public function clearWebsiteCache($id = null)
1548: {
1549: if (is_null($id)) {
1550: $id = $this->getStore()->getWebsiteId();
1551: } elseif ($id instanceof Mage_Core_Model_Website) {
1552: $id = $id->getId();
1553: } elseif ($id === true) {
1554: $id = $this->_website->getId();
1555: }
1556:
1557: if (!empty($this->_websites[$id])) {
1558: $website = $this->_websites[$id];
1559:
1560: unset($this->_websites[$website->getWebsiteId()]);
1561: unset($this->_websites[$website->getCode()]);
1562: }
1563: }
1564: }
1565: