Skip to content

Commit

Permalink
core类库更新Core::change_project()方法
Browse files Browse the repository at this point in the history
Bootstrap完善载入类库的
index.php完善说明
config.new.php完善说明
welcome.view.php完善
  • Loading branch information
breath-co2 committed Jan 24, 2013
1 parent 2a9ccf7 commit 1572de6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 39 deletions.
35 changes: 29 additions & 6 deletions config.new.php
Expand Up @@ -3,19 +3,41 @@
* 项目配置
*
* 建议将default项目放在最后面
* url 可以是字符串也可以是数组,可以/开头,也可以http://开头。结尾不需要加/
*
* @var array
*/
$config['projects'] = array
(
// 请在这边加入其它项目,参照default
/*
'admin' => array
(
'name' => '独立后台',
'dir' => 'admin',
'isuse' => true,
'url' => '/admin/',
'url_admin' => '/',
),
*/

// 请确保Default放在最后
'default' => array
(
'name' => '默认项目',
'isuse' => true,
'dir' => 'default',
'url' => '/',
'url_admin' => '/admin/',
'name' => '默认项目', //名称
'dir' => 'default', //目录
'isuse' => true, //是否启用
'url' => '/', //URL根目录,可以是字符串也可以是数组,可以/开头,也可以http://开头。
'url_admin' => '/admin/', //后台URL根目录,同上
/*
// 此项目有的加载类库配置
'libraries' => array
(
'autoload' => array(),
'cli' => array(),
'debug' => array(),
'admin' => array(),
),
*/
),
);

Expand Down Expand Up @@ -157,6 +179,7 @@

/**
* 语言包
*
* @var string
*/
$config['lang'] = 'zh-cn';
Expand Down
18 changes: 12 additions & 6 deletions core/bootstrap.php
Expand Up @@ -402,7 +402,7 @@ public static function setup($auto_execute = true)
# 读取配置
if ( !is_file(DIR_SYSTEM.'config'.EXT) )
{
self::_show_error( __('Please rename the file config.new:EXT to config:EXT' , array(':EXT'=>EXT)) );
self::_show_error(__('Please rename the file config.new:EXT to config:EXT', array(':EXT'=>EXT)));
}

__include_config_file(self::$core_config, DIR_SYSTEM.'config'.EXT);
Expand Down Expand Up @@ -487,7 +487,7 @@ public static function setup($auto_execute = true)

if (!isset(self::$core_config['projects'][$project]))
{
self::_show_error( __('not found the project: :project',array(':project'=>$project)) );
self::_show_error(__('not found the project: :project', array(':project'=>$project)));
}

// 如果有设置项目
Expand Down Expand Up @@ -536,6 +536,11 @@ public static function setup($auto_execute = true)
}
}

if ( isset(self::$core_config['projects'][self::$project]['isuse']) && !self::$core_config['projects'][self::$project]['isuse'] )
{
self::_show_error(__('the project: :project is not open.' , array(':project'=>$project)));
}

/**
* 初始项目名
*
Expand All @@ -562,7 +567,7 @@ public static function setup($auto_execute = true)

if (!is_dir($project_dir))
{
self::_show_error('not found the project: :project', array(':project' => self::$project));
self::_show_error(__('not found the project: :project', array(':project' => self::$project)));
}

self::$include_path['project'] = array($project_dir);
Expand Down Expand Up @@ -1195,9 +1200,10 @@ public static function protocol()
protected static function reload_all_libraries()
{
# 加载类库
$lib_config = self::$core_config['libraries'];
foreach (array('autoload', 'cli', 'admin', 'debug') as $type)
{
if (!isset(self::$core_config['libraries'][$type]) || !self::$core_config['libraries'][$type])continue;
if (!isset($lib_config[$type]) || !$lib_config[$type])continue;

if ($type=='cli')
{
Expand All @@ -1212,7 +1218,7 @@ protected static function reload_all_libraries()
if (!IS_DEBUG)continue;
}

$libs = array_reverse((array)self::$core_config['libraries'][$type]);
$libs = array_reverse((array)$lib_config[$type]);
foreach ($libs as $lib)
{
self::_add_include_path_lib($lib);
Expand All @@ -1226,7 +1232,7 @@ protected static function reload_all_libraries()
$include_path = array_reverse(self::include_path());
foreach ($include_path as $path)
{
$config_file = $path.'config'.EXT;
$config_file = $path . 'config' . EXT;

if ( is_file($config_file) )
{
Expand Down
41 changes: 24 additions & 17 deletions core/classes/core.class.php
Expand Up @@ -1104,29 +1104,26 @@ public static function change_project($project)
);
}

# 设为当前项目
Core::$project = $project;

# 记录debug信息
if (IS_DEBUG)
{
Core::debug()->info($project, '程序已切换到了新项目');
}

if ( isset($all_prjects_setting[$project]) )
{
# 设为当前项目
Core::$project = $project;

# 还原配置
Core::$config = $all_prjects_setting[$project]['config'];
Core::$include_path = $all_prjects_setting[$project]['include_path'];
Core::$file_list = $all_prjects_setting[$project]['file_list'];
Core::$project_dir = $all_prjects_setting[$project]['project_dir'];
Core::$base_url = $all_prjects_setting[$project]['base_url'];

# 清除缓存数据
unset($all_prjects_setting[$project]);
}
else
{
$core_config = Core::$core_config;
$p_config = Core::$core_config['projects'][$project];

if (!isset($core_config['projects'][$project]['dir']) || !$core_config['projects'][$project]['dir'])
if (!isset($p_config['dir']) || !$p_config['dir'])
{
Core::show_500(__('the project ":project" dir is not defined.', array(':project'=>$project)));
}
Expand All @@ -1139,20 +1136,24 @@ public static function change_project($project)
}

# 项目路径
$project_dir = realpath(DIR_PROJECT . $core_config['projects'][$project]['dir']);
$project_dir = realpath(DIR_PROJECT . $p_config['dir']);

if (!$project_dir || !is_dir($project_dir))
{
Core::show_500(__('the project dir :dir is not exist.', array(':dir'=>$core_config['projects'][$project]['dir'])));
Core::show_500(__('the project dir :dir is not exist.', array(':dir'=>$p_config['dir'])));
}

# 设为当前项目
Core::$project = $project;

$project_dir .= DS;

Core::$project_dir = $project_dir;

# 处理base_url
if ( isset($core_config['projects'][$project]['url']) && $core_config['projects'][$project]['url'] )
if ( isset($p_config['url']) && $p_config['url'] )
{
$url = rtrim(current((array)$core_config['projects'][$project]['url']),'/');
$url = rtrim(current((array)$p_config['url']),'/');
}
else
{
Expand All @@ -1161,9 +1162,9 @@ public static function change_project($project)

if (IS_ADMIN_MODE)
{
if (isset($core_config['projects'][$project]['url_admin']) && $core_config['projects'][$project]['url_admin'])
if (isset($p_config['url_admin']) && $p_config['url_admin'])
{
$admin_url = current((array)$core_config['projects'][$project]['url_admin']);
$admin_url = current((array)$p_config['url_admin']);
if (false===strpos($admin_url[0],'://'))
{
$url .= trim($admin_url,'/');
Expand All @@ -1181,6 +1182,12 @@ public static function change_project($project)
Core::reload_all_libraries();
}

# 记录debug信息
if (IS_DEBUG)
{
Core::debug()->info($project, '程序已切换到了新项目');
}

return true;
}

Expand Down
46 changes: 39 additions & 7 deletions index.php
Expand Up @@ -2,11 +2,22 @@
@chdir(dirname(__FILE__));

/**
* 数据目录,支持网络协议格式
* 负载保护值,0表示不启用
* 不支持window系统
*
* ftp://host:port/dir/
* redis://127.0.0.1/test/
* mysql://root:123456@127.0.0.1/test/
* 建议最大负载不要超过3*N核,例如有16核(含8核超线程)则 16*3=48
*
* @var int
*/
$max_load_avg = 48;

/**
* 数据目录,默认为文件目录,支持数据库,缓存,可用于类似BAE,SAE,SEE,ACE等目录无写权限的环境
*
* PS:如果是希望保存到redis里,可使用 cache://redis/ 然后在config中配置一个$config['cache']['redis']的配置
*
* db://default/test //表示用配置为default数据库保存,表名称为test
* cache://test/abc //表示用缓存配置为test的保存,数据前缀为 abc
*
* @var string
* @see http://www.myqee.com/docs/config/index_page/
Expand All @@ -30,16 +41,36 @@


















////////////////////////////////////////////////////////////////以下无需修改




/**
* 服务器负载保护函数,本方法目前不支持window系统
*
* 最大负载不要超过3*N核,例如有16核(含8核超线程)则 16*3=48
*
* @see http://php.net/manual/en/function.sys-getloadavg.php
*/
function _load_protection($max_load_avg=24)
function _load_protection()
{
global $dir_log,$dir_wwwroot;
global $dir_log,$dir_wwwroot,$max_load_avg;
if ( !function_exists('sys_getloadavg') )
{
return false;
Expand Down Expand Up @@ -87,7 +118,8 @@ function _load_protection($max_load_avg=24)
exit( file_get_contents( $dir_wwwroot.'errors/server_overload.html') );
}

_load_protection();
// 执行负载保护脚本
if ($max_load_avg>0)_load_protection();


// 是否直接执行Core::run();
Expand Down
6 changes: 3 additions & 3 deletions projects/default/views/welcome.view.php
Expand Up @@ -240,13 +240,13 @@
</td>
</tr>
<tr>
<td class="td1" align="right">Global目录</td>
<td class="td1" align="right">Team类库目录</td>
<td>
<?php echo DIR_GLOBAL; ?>
<?php echo DIR_TEAM_LIB; ?>
</td>
</tr>
<tr>
<td class="td1" align="right">第三方类库目录</td>
<td class="td1" align="right">第三方开源类库目录</td>
<td>
<?php echo DIR_LIBRARY; ?>
</td>
Expand Down

0 comments on commit 1572de6

Please sign in to comment.