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
00036 class Mage_Core_Model_App
00037 {
00038
00039 const XML_PATH_INSTALL_DATE = 'global/install/date';
00040
00041 const DEFAULT_ERROR_HANDLER = 'mageCoreErrorHandler';
00042
00043 const DISTRO_LOCALE_CODE = 'en_US';
00044
00045
00046
00047
00048
00049 const CACHE_TAG = 'MAGE';
00050
00051
00052
00053
00054 const DISTRO_STORE_ID = 1;
00055
00056
00057
00058
00059
00060 const DISTRO_STORE_CODE = 'default';
00061
00062
00063
00064
00065
00066 const ADMIN_STORE_ID = 0;
00067
00068
00069
00070
00071
00072
00073 protected $_areas = array();
00074
00075
00076
00077
00078
00079
00080 protected $_store;
00081
00082
00083
00084
00085
00086
00087 protected $_website;
00088
00089
00090
00091
00092
00093
00094 protected $_locale;
00095
00096
00097
00098
00099
00100
00101 protected $_translator;
00102
00103
00104
00105
00106
00107
00108 protected $_design;
00109
00110
00111
00112
00113
00114
00115 protected $_layout;
00116
00117
00118
00119
00120
00121
00122 protected $_config;
00123
00124
00125
00126
00127
00128
00129 protected $_frontController;
00130
00131
00132
00133
00134
00135
00136 protected $_cache;
00137
00138
00139
00140
00141
00142
00143 protected $_helpers = array();
00144
00145
00146
00147
00148
00149
00150 protected $_useCache;
00151
00152
00153
00154
00155
00156
00157 protected $_websites = array();
00158
00159
00160
00161
00162
00163
00164 protected $_groups = array();
00165
00166
00167
00168
00169
00170
00171 protected $_stores = array();
00172
00173
00174
00175
00176
00177
00178 protected $_isSingleStore;
00179
00180
00181
00182
00183 protected $_isSingleStoreAllowed = true;
00184
00185
00186
00187
00188
00189
00190 protected $_currentStore;
00191
00192
00193
00194
00195
00196
00197 protected $_request;
00198
00199
00200
00201
00202
00203
00204 protected $_response;
00205
00206
00207
00208
00209
00210
00211
00212 protected $_events = array();
00213
00214
00215
00216
00217
00218
00219 protected $_updateMode = false;
00220
00221
00222
00223
00224
00225
00226
00227 protected $_useSessionInUrl = true;
00228
00229
00230
00231
00232
00233
00234 protected $_useSessionVar = false;
00235
00236 protected $_isCacheLocked = null;
00237
00238
00239
00240
00241
00242 public function __construct() {}
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 public function init($code, $type=null, $options=array())
00253 {
00254 $this->setErrorHandler(self::DEFAULT_ERROR_HANDLER);
00255 date_default_timezone_set(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
00256
00257 if (is_string($options)) {
00258 $options = array('etc_dir'=>$options);
00259 }
00260
00261 Varien_Profiler::start('mage::app::init::config');
00262 $this->_config = Mage::getConfig();
00263 $this->_config->init($options);
00264 Varien_Profiler::stop('mage::app::init::config');
00265
00266 if (Mage::isInstalled($options)) {
00267 Varien_Profiler::start('mage::app::init::stores');
00268 $this->_initStores();
00269 Varien_Profiler::stop('mage::app::init::stores');
00270
00271 if (empty($code) && !is_null($this->_website)) {
00272 $code = $this->_website->getCode();
00273 $type = 'website';
00274 }
00275 switch ($type) {
00276 case 'store':
00277 $this->_currentStore = $code;
00278 break;
00279 case 'group':
00280 $this->_currentStore = $this->_getStoreByGroup($code);
00281 break;
00282 case 'website':
00283 $this->_currentStore = $this->_getStoreByWebsite($code);
00284 break;
00285 default:
00286 $this->throwStoreException();
00287 }
00288
00289 if (!empty($this->_currentStore)) {
00290 $this->_checkCookieStore($type);
00291 $this->_checkGetStore($type);
00292 }
00293 $this->getRequest()->setPathInfo();
00294 }
00295 return $this;
00296 }
00297
00298
00299
00300
00301
00302
00303 public function getCookie()
00304 {
00305 return Mage::getSingleton('core/cookie');
00306 }
00307
00308
00309
00310
00311
00312
00313 protected function _checkGetStore($type)
00314 {
00315 if (empty($_GET)) {
00316 return $this;
00317 }
00318
00319
00320
00321
00322
00323 if (!isset($_GET['___store'])) {
00324 return $this;
00325 }
00326
00327 $store = $_GET['___store'];
00328 if (!isset($this->_stores[$store])) {
00329 return $this;
00330 }
00331
00332 $storeObj = $this->_stores[$store];
00333 if (!$storeObj->getId() || !$storeObj->getIsActive()) {
00334 return $this;
00335 }
00336
00337
00338
00339
00340
00341 $curStoreObj = $this->_stores[$this->_currentStore];
00342 if ($type == 'website' && $storeObj->getWebsiteId() == $curStoreObj->getWebsiteId()) {
00343 $this->_currentStore = $store;
00344 }
00345 elseif ($type == 'group' && $storeObj->getGroupId() == $curStoreObj->getGroupId()) {
00346 $this->_currentStore = $store;
00347 }
00348 elseif ($type == 'store') {
00349 $this->_currentStore = $store;
00350 }
00351
00352 if ($this->_currentStore == $store) {
00353 $this->getCookie()->set('store', $this->_currentStore, true);
00354 }
00355 return $this;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364 protected function _checkCookieStore($type)
00365 {
00366 if (!$this->getCookie()->get()) {
00367 return $this;
00368 }
00369
00370 $store = $this->getCookie()->get('store');
00371 if ($store && isset($this->_stores[$store])
00372 && $this->_stores[$store]->getId()
00373 && $this->_stores[$store]->getIsActive()) {
00374 if ($type == 'website'
00375 && $this->_stores[$store]->getWebsiteId() == $this->_stores[$this->_currentStore]->getWebsiteId()) {
00376 $this->_currentStore = $store;
00377 }
00378 if ($type == 'group'
00379 && $this->_stores[$store]->getGroupId() == $this->_stores[$this->_currentStore]->getGroupId()) {
00380 $this->_currentStore = $store;
00381 }
00382 if ($type == 'store') {
00383 $this->_currentStore = $store;
00384 }
00385 }
00386 return $this;
00387 }
00388
00389 public function reinitStores()
00390 {
00391 return $this->_initStores();
00392 }
00393
00394
00395
00396
00397
00398 protected function _initStores()
00399 {
00400 $this->_stores = array();
00401 $this->_groups = array();
00402 $this->_website = null;
00403 $this->_websites = array();
00404
00405 $websiteCollection = Mage::getModel('core/website')->getCollection()
00406 ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Website::CACHE_TAG))
00407 ->setLoadDefault(true);
00408 $groupCollection = Mage::getModel('core/store_group')->getCollection()
00409 ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store_Group::CACHE_TAG))
00410 ->setLoadDefault(true);
00411 $storeCollection = Mage::getModel('core/store')->getCollection()
00412 ->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store::CACHE_TAG))
00413 ->setLoadDefault(true);
00414
00415 $this->_isSingleStore = false;
00416 if ($this->_isSingleStoreAllowed) {
00417 $this->_isSingleStore = $storeCollection->count() < 3;
00418 }
00419
00420 $websiteStores = array();
00421 $websiteGroups = array();
00422 $groupStores = array();
00423
00424 foreach ($storeCollection as $store) {
00425
00426 $store->initConfigCache();
00427 $store->setWebsite($websiteCollection->getItemById($store->getWebsiteId()));
00428 $store->setGroup($groupCollection->getItemById($store->getGroupId()));
00429
00430 $this->_stores[$store->getId()] = $store;
00431 $this->_stores[$store->getCode()] = $store;
00432
00433 $websiteStores[$store->getWebsiteId()][$store->getId()] = $store;
00434 $groupStores[$store->getGroupId()][$store->getId()] = $store;
00435
00436 if (is_null($this->_store) && $store->getId()) {
00437 $this->_store = $store;
00438 }
00439 }
00440
00441 foreach ($groupCollection as $group) {
00442
00443 if (!isset($groupStores[$group->getId()])) {
00444 $groupStores[$group->getId()] = array();
00445 }
00446 $group->setStores($groupStores[$group->getId()]);
00447 $group->setWebsite($websiteCollection->getItemById($group->getWebsiteId()));
00448
00449 $websiteGroups[$group->getWebsiteId()][$group->getId()] = $group;
00450
00451 $this->_groups[$group->getId()] = $group;
00452 }
00453
00454 foreach ($websiteCollection as $website) {
00455
00456 if (!isset($websiteGroups[$website->getId()])) {
00457 $websiteGroups[$website->getId()] = array();
00458 }
00459 if (!isset($websiteStores[$website->getId()])) {
00460 $websiteStores[$website->getId()] = array();
00461 }
00462 if ($website->getIsDefault()) {
00463 $this->_website = $website;
00464 }
00465 $website->setGroups($websiteGroups[$website->getId()]);
00466 $website->setStores($websiteStores[$website->getId()]);
00467
00468 $this->_websites[$website->getId()] = $website;
00469 $this->_websites[$website->getCode()] = $website;
00470 }
00471 }
00472
00473
00474
00475
00476
00477
00478 public function isSingleStoreMode()
00479 {
00480 if (!Mage::isInstalled()) {
00481 return false;
00482 }
00483 return $this->_isSingleStore;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492 protected function _getStoreByGroup($group)
00493 {
00494 if (!isset($this->_groups[$group])) {
00495 return null;
00496 }
00497 if (!$this->_groups[$group]->getDefaultStoreId()) {
00498 return null;
00499 }
00500 return $this->_stores[$this->_groups[$group]->getDefaultStoreId()]->getCode();
00501 }
00502
00503
00504
00505
00506
00507
00508
00509 protected function _getStoreByWebsite($website)
00510 {
00511 if (!isset($this->_websites[$website])) {
00512 return null;
00513 }
00514 if (!$this->_websites[$website]->getDefaultGroupId()) {
00515 return null;
00516 }
00517 return $this->_getStoreByGroup($this->_websites[$website]->getDefaultGroupId());
00518 }
00519
00520
00521
00522
00523
00524
00525
00526 public function setCurrentStore($store)
00527 {
00528 $this->_currentStore = $store;
00529 return $this;
00530 }
00531
00532
00533
00534
00535
00536
00537 protected function _initFrontController()
00538 {
00539 $this->_frontController = new Mage_Core_Controller_Varien_Front();
00540 Mage::register('controller', $this->_frontController);
00541 Varien_Profiler::start('mage::app::init_front_controller');
00542 $this->_frontController->init();
00543 Varien_Profiler::stop('mage::app::init_front_controller');
00544 return $this;
00545 }
00546
00547
00548
00549
00550
00551
00552
00553 public function setErrorHandler($handler)
00554 {
00555 set_error_handler($handler);
00556 return $this;
00557 }
00558
00559
00560
00561
00562
00563
00564
00565 public function loadArea($code)
00566 {
00567 $this->getArea($code)->load();
00568 return $this;
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
00578 public function loadAreaPart($area, $part)
00579 {
00580 $this->getArea($area)->load($part);
00581 return $this;
00582 }
00583
00584
00585
00586
00587
00588
00589
00590 public function getArea($code)
00591 {
00592 if (!isset($this->_areas[$code])) {
00593 $this->_areas[$code] = new Mage_Core_Model_App_Area($code, $this);
00594 }
00595 return $this->_areas[$code];
00596 }
00597
00598
00599
00600
00601
00602
00603 public function getStore($id=null)
00604 {
00605 if (!Mage::isInstalled() || $this->getUpdateMode()) {
00606 return $this->_getDefaultStore();
00607 }
00608
00609 if ($id === true && $this->isSingleStoreMode()) {
00610 return $this->_store;
00611 }
00612
00613 if (is_null($id) || ''===$id || $id === true) {
00614 $id = $this->_currentStore;
00615 }
00616 if ($id instanceof Mage_Core_Model_Store) {
00617 return $id;
00618 }
00619 if (is_null($id)) {
00620 $this->throwStoreException();
00621 }
00622
00623 if (empty($this->_stores[$id])) {
00624 $store = Mage::getModel('core/store');
00625
00626 if (is_numeric($id)) {
00627 $store->load($id);
00628 } elseif (is_string($id)) {
00629 $store->load($id, 'code');
00630 }
00631
00632 if (!$store->getCode()) {
00633 $this->throwStoreException();
00634 }
00635 $this->_stores[$store->getStoreId()] = $store;
00636 $this->_stores[$store->getCode()] = $store;
00637 }
00638 return $this->_stores[$id];
00639 }
00640
00641
00642
00643
00644
00645
00646
00647 public function getSafeStore($id = null)
00648 {
00649 try {
00650 return $this->getStore($id);
00651 }
00652 catch (Exception $e) {
00653 if ($this->_currentStore) {
00654 $this->getRequest()->setActionName('noRoute');
00655 return new Varien_Object();
00656 }
00657 else {
00658 Mage::throwException(Mage::helper('core')->__('Requested invalid store "%s"', $id));
00659 }
00660 }
00661 }
00662
00663
00664
00665
00666
00667
00668
00669
00670 public function getStores($withDefault = false, $codeKey = false)
00671 {
00672 $stores = array();
00673 foreach ($this->_stores as $store) {
00674 if (!$withDefault && $store->getId() == 0) {
00675 continue;
00676 }
00677 if ($codeKey) {
00678 $stores[$store->getCode()] = $store;
00679 }
00680 else {
00681 $stores[$store->getId()] = $store;
00682 }
00683 }
00684
00685 return $stores;
00686 }
00687
00688 protected function _getDefaultStore()
00689 {
00690 if (empty($this->_store)) {
00691 $this->_store = Mage::getModel('core/store')
00692 ->setId(self::DISTRO_STORE_ID)
00693 ->setCode(self::DISTRO_STORE_CODE);
00694 }
00695 return $this->_store;
00696 }
00697
00698
00699
00700
00701
00702
00703 public function getDefaultStoreView()
00704 {
00705 foreach ($this->getWebsites() as $_website) {
00706 if ($_website->getIsDefault()) {
00707 if ($_defaultStore = $this->getGroup($_website->getDefaultGroupId())->getDefaultStore()) {
00708 return $_defaultStore;
00709 }
00710 }
00711 }
00712 return null;
00713 }
00714
00715 public function getDistroLocaleCode()
00716 {
00717 return self::DISTRO_LOCALE_CODE;
00718 }
00719
00720
00721
00722
00723
00724
00725 public function getWebsite($id=null)
00726 {
00727 if (is_null($id)) {
00728 $id = $this->getStore()->getWebsiteId();
00729 } elseif ($id instanceof Mage_Core_Model_Website) {
00730 return $id;
00731 } elseif ($id === true) {
00732 return $this->_website;
00733 }
00734
00735 if (empty($this->_websites[$id])) {
00736 $website = Mage::getModel('core/website');
00737 if (is_numeric($id)) {
00738 $website->load($id);
00739 if (!$website->hasWebsiteId()) {
00740 throw Mage::exception('Mage_Core', 'Invalid website id requested.');
00741 }
00742 } elseif (is_string($id)) {
00743 $websiteConfig = Mage::getConfig()->getNode('websites/'.$id);
00744 if (!$websiteConfig) {
00745 throw Mage::exception('Mage_Core', 'Invalid website code requested: '.$id);
00746 }
00747 $website->loadConfig($id);
00748 }
00749 $this->_websites[$website->getWebsiteId()] = $website;
00750 $this->_websites[$website->getCode()] = $website;
00751 }
00752 return $this->_websites[$id];
00753 }
00754
00755 public function getWebsites($withDefault = false, $codeKey = false)
00756 {
00757 $websites = array();
00758 if (is_array($this->_websites)) {
00759 foreach ($this->_websites as $website) {
00760 if (!$withDefault && $website->getId() == 0) {
00761 continue;
00762 }
00763 if ($codeKey) {
00764 $websites[$website->getCode()] = $website;
00765 }
00766 else {
00767 $websites[$website->getId()] = $website;
00768 }
00769 }
00770 }
00771
00772 return $websites;
00773 }
00774
00775
00776
00777
00778
00779
00780
00781 public function getGroup($id=null)
00782 {
00783 if (is_null($id)) {
00784 $id = $this->getStore()->getGroup()->getId();
00785 } elseif ($id instanceof Mage_Core_Model_Store_Group) {
00786 return $id;
00787 }
00788 if (empty($this->_groups[$id])) {
00789 $group = Mage::getModel('core/store_group');
00790 if (is_numeric($id)) {
00791 $group->load($id);
00792 if (!$group->hasGroupId()) {
00793 throw Mage::exception('Mage_Core', 'Invalid store group id requested.');
00794 }
00795 }
00796 $this->_groups[$group->getGroupId()] = $group;
00797 }
00798 return $this->_groups[$id];
00799 }
00800
00801
00802
00803
00804
00805
00806 public function getLocale()
00807 {
00808 if (!$this->_locale) {
00809 $this->_locale = Mage::getSingleton('core/locale');
00810 }
00811 return $this->_locale;
00812 }
00813
00814
00815
00816
00817
00818
00819 public function getLayout()
00820 {
00821 if (!$this->_layout) {
00822 $this->_layout = ($this->getFrontController()->getAction()
00823 ? $this->getFrontController()->getAction()->getLayout()
00824 : Mage::getSingleton('core/layout'));
00825 }
00826 return $this->_layout;
00827 }
00828
00829
00830
00831
00832
00833
00834 public function getTranslator()
00835 {
00836 if (!$this->_translator) {
00837 $this->_translator = Mage::getSingleton('core/translate');
00838 }
00839 return $this->_translator;
00840 }
00841
00842
00843
00844
00845
00846
00847
00848 public function getHelper($name)
00849 {
00850 if (strpos($name, '/') === false) {
00851 $name .= '/data';
00852 }
00853 if (!isset($this->_helpers[$name])) {
00854 $class = Mage::getConfig()->getHelperClassName($name);
00855 $this->_helpers[$name] = new $class();
00856 }
00857 return $this->_helpers[$name];
00858 }
00859
00860
00861
00862
00863
00864
00865 public function getBaseCurrencyCode()
00866 {
00867
00868 return (string) Mage::app()->getConfig()->getNode('default/'.Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
00869 }
00870
00871
00872
00873
00874
00875
00876 public function getConfig()
00877 {
00878 return $this->_config;
00879 }
00880
00881
00882
00883
00884
00885
00886 public function getFrontController()
00887 {
00888 if (!$this->_frontController) {
00889 $this->_initFrontController();
00890 }
00891
00892 return $this->_frontController;
00893 }
00894
00895
00896
00897
00898
00899
00900
00901 public function isInstalled()
00902 {
00903 return Mage::isInstalled();
00904 }
00905
00906
00907
00908
00909
00910
00911
00912 protected function _getCacheId($id=null)
00913 {
00914 if ($id) {
00915 $id = $this->prepareCacheId($id);
00916 }
00917 return $id;
00918 }
00919
00920
00921
00922
00923
00924
00925
00926 public function prepareCacheId($id)
00927 {
00928 $id = strtoupper($id);
00929 $id = preg_replace('/([^a-zA-Z0-9_]{1,1})/', '_', $id);
00930 return $id;
00931 }
00932
00933
00934
00935
00936
00937
00938
00939
00940 protected function _getCacheTags($tags=array())
00941 {
00942 foreach ($tags as $index=>$value) {
00943 $tags[$index] = $this->_getCacheId($value);
00944 }
00945 return $tags;
00946 }
00947
00948
00949
00950
00951
00952
00953 public function getCache()
00954 {
00955 if (!$this->_cache) {
00956 $backend = strtolower((string)Mage::getConfig()->getNode('global/cache/backend'));
00957 if (!$cachePrefix = (string)Mage::getConfig()->getNode('global/cache/prefix')) {
00958 $cachePrefix = md5(Mage::getConfig()->getBaseDir());
00959 }
00960 if (extension_loaded('apc') && ini_get('apc.enabled') && $backend == 'apc') {
00961 $backend = 'Apc';
00962 $backendAttributes = array(
00963 'cache_prefix' => $cachePrefix
00964 );
00965 } elseif (extension_loaded('eaccelerator') && ini_get('eaccelerator.enable') && $backend=='eaccelerator') {
00966 $backend = 'Eaccelerator';
00967 $backendAttributes = array(
00968 'cache_prefix' => $cachePrefix
00969 );
00970 } elseif ('memcached' == $backend && extension_loaded('memcache')) {
00971 $backend = 'Memcached';
00972 $memcachedConfig = Mage::getConfig()->getNode('global/cache/memcached');
00973 $backendAttributes = array(
00974 'compression' => (bool)$memcachedConfig->compression,
00975 'cache_dir' => (string)$memcachedConfig->cache_dir,
00976 'hashed_directory_level' => (string)$memcachedConfig->hashed_directory_level,
00977 'hashed_directory_umask' => (string)$memcachedConfig->hashed_directory_umask,
00978 'file_name_prefix' => (string)$memcachedConfig->file_name_prefix,
00979 'servers' => array(),
00980 );
00981 foreach ($memcachedConfig->servers->children() as $serverConfig) {
00982 $backendAttributes['servers'][] = array(
00983 'host' => (string)$serverConfig->host,
00984 'port' => (string)$serverConfig->port,
00985 'persistent' => (string)$serverConfig->persistent,
00986 );
00987 }
00988 } else {
00989 $backend = 'File';
00990 $backendAttributes = array(
00991 'cache_dir' => Mage::getBaseDir('cache'),
00992 'hashed_directory_level' => 1,
00993 'hashed_directory_umask' => 0777,
00994 'file_name_prefix' => 'mage',
00995 );
00996 }
00997 $lifetime = Mage::getConfig()->getNode('global/cache/lifetime');
00998 if ($lifetime !== false) {
00999 $lifetime = (int) $lifetime;
01000 }
01001 else {
01002 $lifetime = 7200;
01003 }
01004 $this->_cache = Zend_Cache::factory('Core', $backend,
01005 array(
01006 'caching' => true,
01007 'lifetime' => $lifetime,
01008 'automatic_cleaning_factor' => 0,
01009 ),
01010 $backendAttributes
01011 );
01012 }
01013 return $this->_cache;
01014 }
01015
01016
01017
01018
01019
01020
01021
01022 public function loadCache($id)
01023 {
01024 return $this->getCache()->load($this->_getCacheId($id));
01025 }
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035 public function saveCache($data, $id, $tags=array(), $lifeTime=false)
01036 {
01037 $tags = $this->_getCacheTags($tags);
01038
01039
01040
01041
01042 if (!in_array($this->_getCacheId(Mage_Core_Model_Config::CACHE_TAG), $tags)) {
01043 $tags[] = self::CACHE_TAG;
01044 }
01045 $this->getCache()->save((string)$data, $this->_getCacheId($id), $tags, $lifeTime);
01046 return $this;
01047 }
01048
01049
01050
01051
01052
01053
01054
01055 public function removeCache($id)
01056 {
01057 $this->getCache()->remove($this->_getCacheId($id));
01058 return $this;
01059 }
01060
01061
01062
01063
01064
01065
01066
01067 public function cleanCache($tags=array())
01068 {
01069 if (!empty($tags)) {
01070 if (!is_array($tags)) {
01071 $tags = array($tags);
01072 }
01073 $tags = $this->_getCacheTags($tags);
01074 $cacheTag = $this->_getCacheId(Mage_Core_Model_Config::CACHE_TAG);
01075 $this->getCache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags);
01076 } else {
01077 $this->getCache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array(self::CACHE_TAG));
01078
01079
01080
01081 Mage::getConfig()->cleanCache();
01082 }
01083
01084 Mage::dispatchEvent('application_clean_cache', array('tags' => $tags));
01085 return $this;
01086 }
01087
01088
01089
01090
01091
01092
01093 public function getUseCacheFilename()
01094 {
01095 return Mage::getConfig()->getOptions()->getEtcDir().DS.'use_cache.ser';
01096 }
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109 public function useCache($type=null)
01110 {
01111 if (is_null($this->_useCache)) {
01112 $filename = $this->getUseCacheFilename();
01113 if (is_readable($filename)) {
01114 $this->_useCache = unserialize(file_get_contents($filename));
01115 } else {
01116 $data = Mage::getConfig()->getNode('global/use_cache');
01117 if (!empty($data)) {
01118 $this->_useCache = (array)$data;
01119 } else {
01120 $this->_useCache = array();
01121 }
01122 }
01123 }
01124 if (empty($type)) {
01125 return $this->_useCache;
01126 } else {
01127 return isset($this->_useCache[$type]) ? (bool)$this->_useCache[$type] : false;
01128 }
01129 }
01130
01131 public function saveUseCache($data)
01132 {
01133
01134
01135 $filename = $this->getUseCacheFilename();
01136 if (!$fp = @fopen($filename, 'w')) {
01137 Mage::throwException($filename.' is not writable, unable to save cache settings');
01138 }
01139 @fwrite($fp, serialize($data));
01140 @fclose($fp);
01141 @chmod($filename, 0666);
01142 return $this;
01143 }
01144
01145
01146
01147
01148
01149 public function cleanAllSessions()
01150 {
01151 if (session_module_name()=='files') {
01152 $dir = session_save_path();
01153 mageDelTree($dir);
01154 }
01155 return $this;
01156 }
01157
01158
01159
01160
01161
01162
01163 public function getRequest()
01164 {
01165 if (empty($this->_request)) {
01166 $this->_request = new Mage_Core_Controller_Request_Http();
01167 }
01168 return $this->_request;
01169 }
01170
01171
01172
01173
01174
01175
01176 public function getResponse()
01177 {
01178 if (empty($this->_response)) {
01179 $this->_response = new Mage_Core_Controller_Response_Http();
01180 $this->_response->headersSentThrowsException = Mage::$headersSentThrowsException;
01181 $this->_response->setHeader("Content-Type", "text/html; charset=UTF-8");
01182 }
01183 return $this->_response;
01184 }
01185
01186 public function addEventArea($area)
01187 {
01188 if (!isset($this->_events[$area])) {
01189 $this->_events[$area] = array();
01190 }
01191 return $this;
01192 }
01193
01194 public function dispatchEvent($eventName, $args)
01195 {
01196 foreach ($this->_events as $area=>$events) {
01197 if (!isset($events[$eventName])) {
01198 $eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
01199 if (!$eventConfig) {
01200 $this->_events[$area][$eventName] = false;
01201 continue;
01202 }
01203 $observers = array();
01204 foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
01205 $observers[$obsName] = array(
01206 'type' => $obsConfig->type ? (string)$obsConfig->type : 'singleton',
01207 'model' => $obsConfig->class ? (string)$obsConfig->class : $obsConfig->getClassName(),
01208 'method' => (string)$obsConfig->method,
01209 'args' => (array)$obsConfig->args,
01210 );
01211 }
01212 $events[$eventName]['observers'] = $observers;
01213 $this->_events[$area][$eventName]['observers'] = $observers;
01214 }
01215 if (false===$events[$eventName]) {
01216 continue;
01217 } else {
01218 $event = new Varien_Event($args);
01219 $event->setName($eventName);
01220 $observer = new Varien_Event_Observer();
01221 }
01222
01223 foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
01224 $observer->setData(array('event'=>$event));
01225 Varien_Profiler::start('OBSERVER: '.$obsName);
01226 switch ($obs['type']) {
01227 case 'singleton':
01228 $method = $obs['method'];
01229 $observer->addData($args);
01230 $object = Mage::getSingleton($obs['model']);
01231 $object->$method($observer);
01232 break;
01233
01234 case 'object': case 'model':
01235 $method = $obs['method'];
01236 $observer->addData($args);
01237 $object = Mage::getModel($obs['model']);
01238 $object->$method($observer);
01239 break;
01240 }
01241 Varien_Profiler::stop('OBSERVER: '.$obsName);
01242 }
01243 }
01244 return $this;
01245 }
01246
01247 public function setUpdateMode($value)
01248 {
01249 $this->_updateMode = $value;
01250 }
01251
01252 public function getUpdateMode()
01253 {
01254 return $this->_updateMode;
01255 }
01256
01257 public function throwStoreException()
01258 {
01259 throw new Mage_Core_Model_Store_Exception('');
01260 }
01261
01262
01263
01264
01265
01266
01267
01268 public function setUseSessionVar($var)
01269 {
01270 $this->_useSessionVar = (bool)$var;
01271 return $this;
01272 }
01273
01274
01275
01276
01277
01278
01279 public function getUseSessionVar()
01280 {
01281 return $this->_useSessionVar;
01282 }
01283
01284
01285
01286
01287
01288
01289 public function getAnyStoreView()
01290 {
01291 if ($store = $this->getDefaultStoreView()) {
01292 return $store;
01293 }
01294 foreach ($this->getStores() as $store) {
01295 return $store;
01296 }
01297 }
01298
01299
01300
01301
01302
01303
01304
01305 public function setUseSessionInUrl($flag = true)
01306 {
01307 $this->_useSessionInUrl = (bool)$flag;
01308 return $this;
01309 }
01310
01311
01312
01313
01314
01315
01316 public function getUseSessionInUrl()
01317 {
01318 return $this->_useSessionInUrl;
01319 }
01320
01321
01322
01323
01324
01325
01326
01327 public function setIsSingleStoreModeAllowed($value)
01328 {
01329 $this->_isSingleStoreAllowed = (bool)$value;
01330 return $this;
01331 }
01332 }