1.
/**
* Retrieve config value for store by path
*
* @param string $path
* @param mixed $store
* @return mixed
*/
public static function getStoreConfig($path, $store = null)
{
return self::app()->getStore($store)->getConfig($path);
}
2
public static function app($code = '', $type = 'store', $options = array())
{
if (null === self::$_app) {
self::$_app = new Mage_Core_Model_App();
self::setRoot();
self::$_events = new Varien_Event_Collection();
self::$_config = new Mage_Core_Model_Config();
Varien_Profiler::start('self::app::init');
self::$_app->init($code, $type, $options);
Varien_Profiler::stop('self::app::init');
self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
}
return self::$_app;
}
3
mage_core_model_app
public function init($code, $type=null, $options=array())
{
$this->_initEnvironment();
if (is_string($options)) {
$options = array('etc_dir'=>$options);
}
Varien_Profiler::start('mage::app::init::config');
$this->_config = Mage::getConfig();
$this->_initBaseConfig();
$this->_initCache();
$this->_config->init($options);
Varien_Profiler::stop('mage::app::init::config');
if (Mage::isInstalled($options)) {
$this->_initCurrentStore($code, $type);
$this->_initRequest();
}
return $this;
}
在初始化函数里面,将初始化好的mage.php中的$_config传入app对象中,赋值于内部的$this->_config
3.1
//错误处理handle的指定。默认时区指定为UTC。
$this->_initEnvironment();
4
//Initialize PHP environment。@return Mage_Core_Model_App
protected function _initEnvironment()
{
$this->setErrorHandler(self::DEFAULT_ERROR_HANDLER);
date_default_timezone_set(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
return $this;
}
4.1
const DEFAULT_ERROR_HANDLER = 'mageCoreErrorHandler';
public function setErrorHandler($handler)
{
set_error_handler($handler);
return $this;
}
//Sets a user-defined error handler function
set_error_handler($handler);
4.2
const DEFAULT_TIMEZONE = 'UTC';
//Sets the default timezone used by all date/time functions in a script
date_default_timezone_set(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
4.3
$this->_initCache();
protected function _initCache()
{
$options = $this->_config->getNode('global/cache');
if ($options) {
$options = $options->asArray();
} else {
$options = array();
}
$this->_cache = Mage::getModel('core/cache', $options);
return $this;
}
$options 为$this->config->getNode('global/cache')->asArray();是数组形式的配置信息。
4.3.1
public static function getModel($modelClass = '', $arguments = array())
{
return self::getConfig()->getModelInstance($modelClass, $arguments);
}
4.3.2
public function getModelInstance($modelClass='', $constructArguments=array())
{
$className = $this->getModelClassName($modelClass);
if (class_exists($className)) {
Varien_Profiler::start('CORE::create_object_of::'.$className);
$obj = new $className($constructArguments);
Varien_Profiler::stop('CORE::create_object_of::'.$className);
return $obj;
} else {
#throw Mage::exception('Mage_Core', Mage::helper('core')->__('Model class does not exist: %s.', $modelClass));
return false;
}
}
4.4
故
$this->_initCache(); ------>$this->_cache =?
得到的是
$this->_config->getNode('global/cache')
$this->_cache = Mage::getModel('core/cache', $options);
core模块下的cache块。
5
$this->_config->init($options);
/**
* Initialization of core configuration
*
* @return Mage_Core_Model_Config
*/
public function init($options=array())
{
$this->setCacheChecksum(null);
$this->_cacheLoadedSections = array();
$this->setOptions($options);
$this->loadBase();
$cacheLoad = $this->loadModulesCache();
if ($cacheLoad) {
return $this;
}
$this->loadModules();
$this->loadDb();
$this->saveCache();
return $this;
}
初始化core配置。
5.1
$this->setOptions($options);
/**
* Set configuration options
*
* @param array $options
* @return Mage_Core_Model_Config
*/
public function setOptions($options)
{
if (is_array($options)) {
$this->getOptions()->addData($options);
}
return $this;
}
5.2
//取到一些文件里面的配置文件通过$this->loadFile()函数
//mage/core/model/config.php,$etcDir由options.php文件里面的数组得到, $etcDir = $this->getOptions()->getEtcDir();
public function loadBase()
{
$etcDir = $this->getOptions()->getEtcDir();
$files = glob($etcDir.DS.'*.xml');
$this->loadFile(current($files));
while ($file = next($files)) {
$merge = clone $this->_prototype;
$merge->loadFile($file);
$this->extend($merge);
}
if (in_array($etcDir.DS.'local.xml', $files)) {
$this->_isLocalConfigLoaded = true;
}
return $this;
}
5.3
$etcDir = $this->getOptions()->getEtcDir();
public function setOptions($options)
{
if (is_array($options)) {
$this->getOptions()->addData($options);
}
return $this;
}
5.4
public function getEtcDir()
{
//return $this->getDataSetDefault('etc_dir', $this->getAppDir().DS.'etc');
return $this->_data['etc_dir'];
}
5.5
$files = glob($etcDir.DS.'*.xml');
glob()函数的作用是:以数组的形式返回与指定模式相匹配的文件名或目录。
6
if (Mage::isInstalled($options)) {
$this->_initCurrentStore($code, $type);
$this->_initRequest();
}
Mage::isInstalled($options)
6.1
* Retrieve application installation flag
public static function isInstalled($options = array())
{
if (self::$_isInstalled === null) {
self::setRoot();
if (is_string($options)) {
$options = array('etc_dir' => $options);
}
$etcDir = 'etc';
if (!empty($options['etc_dir'])) {
$etcDir = $options['etc_dir'];
}
$localConfigFile = self::getRoot() . DS . $etcDir . DS . 'local.xml';
self::$_isInstalled = false;
if (is_readable($localConfigFile)) {
$localConfig = simplexml_load_file($localConfigFile);
date_default_timezone_set('UTC');
if (($date = $localConfig->global->install->date) && strtotime($date)) {
self::$_isInstalled = true;
}
}
}
return self::$_isInstalled;
}
6.2
public static function setRoot($appRoot = '')
{
if (self::$_appRoot) {
return ;
}
if ('' === $appRoot) {
// automagically find application root by dirname of Mage.php
$appRoot = dirname(__FILE__);
}
$appRoot = realpath($appRoot);
if (is_dir($appRoot) and is_readable($appRoot)) {
self::$_appRoot = $appRoot;
} else {
self::throwException($appRoot . ' is not a directory or not readable by this user');
}
}
6.3
$this->_initCurrentStore($code, $type);
/**
* Initialize currently ran store
*
* @param string $scopeCode code of default scope (website/store_group/store code)
* @param string $scopeType type of default scope (website/group/store)
* @return unknown_type
*/
protected function _initCurrentStore($scopeCode, $scopeType)
{
Varien_Profiler::start('mage::app::init::stores');
$this->_initStores();
Varien_Profiler::stop('mage::app::init::stores');
if (empty($scopeCode) && !is_null($this->_website)) {
$scopeCode = $this->_website->getCode();
$scopeType = 'website';
}
switch ($scopeType) {
case 'store':
$this->_currentStore = $scopeCode;
break;
case 'group':
$this->_currentStore = $this->_getStoreByGroup($scopeCode);
break;
case 'website':
$this->_currentStore = $this->_getStoreByWebsite($scopeCode);
break;
default:
$this->throwStoreException();
}
if (!empty($this->_currentStore)) {
$this->_checkCookieStore($scopeType);
$this->_checkGetStore($scopeType);
}
return $this;
}
6.4
mage/CORE/MODEL/APP
$this->_initStores();
/**
* Init store, group and website collections
*
*/
protected function _initStores()
{
$this->_stores = array();
$this->_groups = array();
$this->_website = null;
$this->_websites = array();
$websiteCollection = Mage::getModel('core/website')->getCollection()
->initCache($this->getCache(), 'app', array(Mage_Core_Model_Website::CACHE_TAG))
->setLoadDefault(true);
$groupCollection = Mage::getModel('core/store_group')->getCollection()
->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store_Group::CACHE_TAG))
->setLoadDefault(true);
$storeCollection = Mage::getModel('core/store')->getCollection()
->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store::CACHE_TAG))
->setLoadDefault(true);
$this->_isSingleStore = false;
if ($this->_isSingleStoreAllowed) {
$this->_isSingleStore = $storeCollection->count() < 3;
}
$websiteStores = array();
$websiteGroups = array();
$groupStores = array();
foreach ($storeCollection as $store) {
/* @var $store Mage_Core_Model_Store */
$store->initConfigCache();
$store->setWebsite($websiteCollection->getItemById($store->getWebsiteId()));
$store->setGroup($groupCollection->getItemById($store->getGroupId()));
$this->_stores[$store->getId()] = $store;
$this->_stores[$store->getCode()] = $store;
$websiteStores[$store->getWebsiteId()][$store->getId()] = $store;
$groupStores[$store->getGroupId()][$store->getId()] = $store;
if (is_null($this->_store) && $store->getId()) {
$this->_store = $store;
}
}
foreach ($groupCollection as $group) {
/* @var $group Mage_Core_Model_Store_Group */
if (!isset($groupStores[$group->getId()])) {
$groupStores[$group->getId()] = array();
}
$group->setStores($groupStores[$group->getId()]);
$group->setWebsite($websiteCollection->getItemById($group->getWebsiteId()));
$websiteGroups[$group->getWebsiteId()][$group->getId()] = $group;
$this->_groups[$group->getId()] = $group;
}
foreach ($websiteCollection as $website) {
/* @var $website Mage_Core_Model_Website */
if (!isset($websiteGroups[$website->getId()])) {
$websiteGroups[$website->getId()] = array();
}
if (!isset($websiteStores[$website->getId()])) {
$websiteStores[$website->getId()] = array();
}
if ($website->getIsDefault()) {
$this->_website = $website;
}
$website->setGroups($websiteGroups[$website->getId()]);
$website->setStores($websiteStores[$website->getId()]);
$this->_websites[$website->getId()] = $website;
$this->_websites[$website->getCode()] = $website;
}
}
7
/**
* Init request object
*
* @return Mage_Core_Model_App
*/
protected function _initRequest()
{
$this->getRequest()->setPathInfo();
return $this;
}
7.1
/**
* Retrieve request object
*
* @return Mage_Core_Controller_Request_Http
*/
public function getRequest()
{
if (empty($this->_request)) {
$this->_request = new Mage_Core_Controller_Request_Http();
}
return $this->_request;
}
7.2
/**
* Set the PATH_INFO string
* Set the ORIGINAL_PATH_INFO string
*
* @param string|null $pathInfo
* @return Zend_Controller_Request_Http
*/
public function setPathInfo($pathInfo = null)
{
if ($pathInfo === null) {
$requestUri = $this->getRequestUri();
if (null === $requestUri) {
return $this;
}
// Remove the query string from REQUEST_URI
$pos = strpos($requestUri, '?');
if ($pos) {
$requestUri = substr($requestUri, 0, $pos);
}
$baseUrl = $this->getBaseUrl();
$pathInfo = substr($requestUri, strlen($baseUrl));
if ((null !== $baseUrl) && (false === $pathInfo)) {
$pathInfo = '';
} elseif (null === $baseUrl) {
$pathInfo = $requestUri;
}
if ($this->_canBeStoreCodeInUrl()) {
$pathParts = explode('/', ltrim($pathInfo, '/'), 2);
$storeCode = $pathParts[0];
if (!$this->isDirectAccessFrontendName($storeCode)) {
$stores = Mage::app()->getStores(true, true);
if ($storeCode!=='' && isset($stores[$storeCode])) {
Mage::app()->setCurrentStore($storeCode);
$pathInfo = '/'.(isset($pathParts[1]) ? $pathParts[1] : '');
}
elseif ($storeCode !== '') {
$this->setActionName('noRoute');
}
}
}
$this->_originalPathInfo = (string) $pathInfo;
$this->_requestString = $pathInfo . ($pos!==false ? substr($requestUri, $pos) : '');
}
$this->_pathInfo = (string) $pathInfo;
return $this;
}
7.2.1
/**
* Returns the REQUEST_URI taking into account
* platform differences between Apache and IIS
*
* @return string
*/
public function getRequestUri()
{
if (empty($this->_requestUri)) {
$this->setRequestUri();
}
return $this->_requestUri;
}
8
/**
* Retrieve application store object
*
* @return Mage_Core_Model_Store
*/
public function getStore($id=null)
{
if (!Mage::isInstalled() || $this->getUpdateMode()) {
return $this->_getDefaultStore();
}
if ($id === true && $this->isSingleStoreMode()) {
return $this->_store;
}
if (is_null($id) || ''===$id || $id === true) {
$id = $this->_currentStore;
}
if ($id instanceof Mage_Core_Model_Store) {
return $id;
}
if (is_null($id)) {
$this->throwStoreException();
}
if (empty($this->_stores[$id])) {
$store = Mage::getModel('core/store');
/* @var $store Mage_Core_Model_Store */
if (is_numeric($id)) {
$store->load($id);
} elseif (is_string($id)) {
$store->load($id, 'code');
}
if (!$store->getCode()) {
$this->throwStoreException();
}
$this->_stores[$store->getStoreId()] = $store;
$this->_stores[$store->getCode()] = $store;
}
return $this->_stores[$id];
}
8.1
return $this->_getDefaultStore();
protected function _getDefaultStore()
{
if (empty($this->_store)) {
$this->_store = Mage::getModel('core/store')
->setId(self::DISTRO_STORE_ID)
->setCode(self::DISTRO_STORE_CODE);
}
return $this->_store;
}
/**
* Default store Id (for install)
*/
const DISTRO_STORE_ID = 1;
/**
* Default store code (for install)
*
*/
const DISTRO_STORE_CODE = 'default';
8.2
Mage_Core_Model_Store
public function getConfig($path)
{
if (isset($this->_configCache[$path])) {
return $this->_configCache[$path];
}
$config = Mage::getConfig();
$fullPath = 'stores/'.$this->getCode().'/'.$path;
$data = $config->getNode($fullPath);
if (!$data && !Mage::isInstalled()) {
$data = $config->getNode('default/' . $path);
}
if (!$data) {
return null;
}
return $this->_processConfigValue($fullPath, $path, $data);
}
初始化步骤
首先在mage::函数里面定义$_events,$_config,加载event,
初始化:
$_config:
1
$this->setCacheId('config_global');
$this->_options = new Mage_Core_Model_Config_Options();
$this->_prototype = new Mage_Core_Model_Config_Base();
$this->_cacheChecksum = null;
2
$this->_elementClass = 'Mage_Core_Model_Config_Element';
3
$this->setXml($sourceData);//此时为空。
4
$this->_config->loadBase();//得到etc文件,并加载。等价于 $this->_config->init();没有参数的config初始化函数
5
Initialize application cache instance
_initCache()
6
$this->_config->init($options);带参数的config初始化函数。
我理解成对配置文件的读入,然后加载。。。不知道对不???
7
Initialize currently ran store
$this->_initCurrentStore($code, $type)
8
Init request object
$this->_initRequest();
****************************************************************************
website,store_group,store等模块的初始化函数MAGE/CORE/MODEL/APP.php下的_initStores()函数,
$websiteCollection = Mage::getModel('core/website')->getCollection()
->initCache($this->getCache(), 'app', array(Mage_Core_Model_Website::CACHE_TAG))
->setLoadDefault(true);
$groupCollection = Mage::getModel('core/store_group')->getCollection()
->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store_Group::CACHE_TAG))
->setLoadDefault(true);
$storeCollection = Mage::getModel('core/store')->getCollection()
->initCache($this->getCache(), 'app', array(Mage_Core_Model_Store::CACHE_TAG))
->setLoadDefault(true);