CI框架源码解析3全局函数库文件Common.php

Common.php定义一系列全局函数

全局函数的定义方式为


 

if(! function_exists(fun_name))
{
}

这样做可以防止定义重名函数,我们自己在封装全局函数时候也可以使用这种方式

1、is_php()

判断当前php版本是否是$version以上的

static 避免每次调用都去version_compare()

// 确定最新的php版本大于该值
if ( ! function_exists('is_php'))
{
    // 判断当前php版本是否大于$version version_compare回去比较
	function is_php($version)
	{
		static $_is_php;
		$version = (string) $version;

		// PHP_VERSION获取当前php版本
		if ( ! isset($_is_php[$version]))
		{
			$_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
		}

		return $_is_php[$version];
	}
}

2、is_ready_writable()

判断文件是否可以写入

兼容了Linux/Unix/Windows系统

// 文件的写入性能测试
if ( ! function_exists('is_really_writable'))
{

    //该函数和php官方手册上面写的差不多,兼容linux/Unix和windows系统。
    //可以查看手册:http://www.php.net/manual/en/function.is-writable.php
	function is_really_writable($file)
	{
	    // DIRECTORY_SEPARATOR是系统目录分隔符  可以判断是Linux 系统
		if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR ! ini_get('safe_mode')))
		{
			return is_writable($file);
		}

        // 对于windows系统
		if (is_dir($file))
		{
		    // 如果是目录文件 创建一个随机生成的文件
			$file = rtrim($file, '/').'/'.md5(mt_rand());
			// 如果不可创建 则返回不可写
			if (($fp = @fopen($file, 'ab')) === FALSE)
			{
				return FALSE;
			}

			fclose($fp);
			// 删除原来的文件
			@chmod($file, 0777);
			@unlink($file);
			return TRUE;
		}
		elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE)
		{
		    // 如果是一个文件 通过写入方式打不开判断不可写
			return FALSE;
		}

		fclose($fp);
		return TRUE;
	}
}

3、load_class()

 


if ( ! function_exists('load_class'))
{
    // 加载类 默认加载libraries里的  如果需要加载core 则$directory='core'
    // $param 实例化参数
	function &load_class($class, $directory = 'libraries', $param = NULL)
	{
	    // 用一个静态数组保存已经加载过的类 避免重复加载消耗资源 实现单例化
		static $_classes = array();

		// 加载过的类直接返回
		if (isset($_classes[$class]))
		{
			return $_classes[$class];
		}

		$name = FALSE;

        // 应用目录下和系统目录下分别加载  优先加载应用目录,也就是用户自定义的
		foreach (array(APPPATH, BASEPATH) as $path)
		{
			if (file_exists($path.$directory.'/'.$class.'.php'))
			{
				$name = 'CI_'.$class;

				if (class_exists($name, FALSE) === FALSE)
				{
					require_once($path.$directory.'/'.$class.'.php');
				}

				break;
			}
		}

		// Is the request a class extension? If so we load it too
		if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
		{
			$name = config_item('subclass_prefix').$class;

			if (class_exists($name, FALSE) === FALSE)
			{
				require_once(APPPATH.$directory.'/'.$name.'.php');
			}
		}

		if ($name === FALSE)
		{
			set_status_header(503);
			echo 'Unable to locate the specified class: '.$class.'.php';
			exit(5); // EXIT_UNK_CLASS
		}

		// 记录加载过的类名
		is_loaded($class);

		$_classes[$class] = isset($param)
			? new $name($param)
			: new $name();
		return $_classes[$class];
	}
}

注:

(1)&load_class() 返回的是一个class实例的引用

(2) static数组缓存已经实例化的类 实现方式类似单例模式

(3)函数优先查找APPPATH和BASEPATH中查找类,然后才从$directory中查找类,这意味着,如果directory中存在着同名的类(指除去前缀之后同名),CI加载的实际上是该扩展类。这也意味着,可以对CI的核心进行修改或者扩展。

4、is_loaded()


if ( ! function_exists('is_loaded'))
{

    // 记录已经加载过的类 用在load_class
	function &is_loaded($class = '')
	{
		static $_is_loaded = array();

		if ($class !== '')
		{
			$_is_loaded[strtolower($class)] = $class;
		}

		return $_is_loaded;
	}
}

5、& get_config() config_item() & get_mimes()

(1) get_config()


if ( ! function_exists('get_config'))
{

    // 加载配置文件config.php
	function &get_config(Array $replace = array())
	{
		static $config;

		if (empty($config))
		{
			$file_path = APPPATH.'config/config.php';
			$found = FALSE;
			if (file_exists($file_path))
			{
				$found = TRUE;
				require($file_path);
			}

			if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
			{
				require($file_path);
			}
			elseif ( ! $found)
			{
				set_status_header(503);
				echo 'The configuration file does not exist.';
				exit(3); // EXIT_CONFIG
			}

			if ( ! isset($config) OR ! is_array($config))
			{
				set_status_header(503);
				echo 'Your config file does not appear to be formatted correctly.';
				exit(3); // EXIT_CONFIG
			}
		}


		foreach ($replace as $key => $val)
		{
			$config[$key] = $val;
		}

		return $config;
	}
}

(2) config_item()


if ( ! function_exists('config_item'))
{

    // 获取配置数组中的某个元素
	function config_item($item)
	{
		static $_config;

		if (empty($_config))
		{
		    // 引用不能直接分配给静态变量 所以我们使用一个数组
			$_config[0] =& get_config();
		}

		return isset($_config[0][$item]) ? $_config[0][$item] : NULL;
	}
}

(3) get_mimes()


 


if ( ! function_exists('get_mimes'))
{

    // 获取mimes.php中的配置数组
	function &get_mimes()
	{
		static $_mimes;

		if (empty($_mimes))
		{
			$_mimes = file_exists(APPPATH.'config/mimes.php')
				? include(APPPATH.'config/mimes.php')
				: array();

			if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
			{
				$_mimes = array_merge($_mimes, include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'));
			}
		}

		return $_mimes;
	}
}

猜你喜欢

转载自blog.csdn.net/u011028179/article/details/86634800