module_class.php
2.55 KB
1
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
 * Tiny - A PHP Framework For Web Artisans
 * @author Tiny <tinylofty@gmail.com>
 * @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
 * @version 1.0
 */
/**
 * 模块类
 * 
 * @author Tiny
 * @class Module
 */
abstract class Module extends Object
{
	//父模块
	private $parentModule;
	//此模块所拥有的子模块
	private $modules = array();
	//模块唯一标识
	private $id;
	//基本路径
	private $basePath;
	//构造函数
	public function __construct($id, $parent, $config=null)
	{
		$this->id = $id;
		$this->parentModule = $parent;
		if(is_string($config)) $config = require($config);
		if(isset($config['basePath']))
		{
			$this->setBasePath($config['basePath']);
			unset($config['basePath']);
		}
		$this->configure($config);
		Tiny::setPath($id,$this->getBasePath());
	}
    /**
     * 配制设置
     * 
     * @access public
     * @param mixed $config
     * @return mixed
     */
	public function configure($config)
    {
    	if(is_array($config))
    	{
    		foreach($config as $key=>$value) $this->$key=$value;
    	}
    }
    /**
     * 得到当前模块的ID
     * 
     * @access public
     * @return mixed
     */
	public function getId()
	{
		return $this->id;
	}
    /**
     * 设定当前模块的ID
     * 
     * @access public
     * @param mixed $id
     * @return mixed
     */
	public function setId($id)
	{
		$this->id = $id;
	}
	/**
     * 得到模块的基本路径
	 * 
	 * @access public
	 * @return mixed
	 */
	public function getBasePath()
	{
		return $this->basePath;
	}
    /**
     * 设定模块的基本路径
     * 
     * @access public
     * @param mixed $basePath
     * @return mixed
     */
	public function setBasePath($basePath)
	{
		$this->basePath = $basePath;
	}
    /**
     * 得到模块的父模块
     * 
     * @access public
     * @return mixed
     */
	public function getParentModule()
	{
		return $this->parentModule;
	}
    /**
     * 查询模块是否包含对应的子模块
     * 
     * @access public
     * @param mixed $id
     * @return mixed
     */
	public function hasModule($id)
	{
		return isset($this->modules[$id]);
	}
    /**
     * 根据模块id取得对应的子模块
     * 
     * @access public
     * @param mixed $id
     * @return mixed
     */
	public function getModule($id)
	{
		if($this->hasModule($id)) return $this->getModule();
		else if(isset($this->parentModule)) return $this->parentModule->__get($id);
		else false;
	}
}