OSDN Git Service

#17549 - Remove constructor with reference.
[mulab/sd2nd.git] / trust_path / modules / sd2nd / class / AssetManager.class.php
1 <?php
2 /**
3  * @file
4  * @package sd2nd
5  * @version $Id$
6 **/
7
8 if(!defined('XOOPS_ROOT_PATH'))
9 {
10     exit;
11 }
12
13 /**
14  * Sd2nd_AssetManager
15 **/
16 class Sd2nd_AssetManager
17 {
18     #region for Enum
19     const HANDLER_ABILITY = 'Ability';
20     const HANDLER_ACTIVE_ABILITY = 'ActiveAbility';
21     const HANDLER_CHAR = 'Char';
22     const HANDLER_COSTUME = 'Costume';
23     const HANDLER_ENO_PNO_LINK = 'EnoPnoLink';
24     const HANDLER_ICON = 'Icon';
25     const HANDLER_IMAGE = 'Image';
26     const HANDLER_KIND = 'Kind';
27     const HANDLER_LEARNED_ABILITY = 'LearnedAbility';
28     const HANDLER_LEARNED_COSTUME = 'LearnedCostume';
29     const HANDLER_LEARNED_SKILL = 'LearnedSkill';
30     const HANDLER_MAP = 'Map';
31     const HANDLER_MAP_CACHE = 'MapCache';
32     const HANDLER_MAP_NAME = 'MapName';
33     const HANDLER_MAP_SPOT = 'MapSpot';
34     const HANDLER_RACE = 'Race';
35     const HANDLER_SKILL = 'Skill';
36     const HANDLER_SKILL_ELEMENT_LINK = 'SkillElementLink';
37     const HANDLER_STATUS = 'Status';
38     const HANDLER_TARGET = 'Target';
39     const HANDLER_UPDATE = 'Update';
40     #endregion
41     
42     public /*** string ***/ $mDirname = '';
43     public /*** string ***/ $mTrustDirname = 'sd2nd';
44     public /*** string[][][] ***/ $mAssetList = array();
45     private /*** object[][] ***/ $_mCache = array();
46
47     /**
48      * __construct
49      * 
50      * @param   string  $dirname
51      * 
52      * @return  void
53     **/
54     public function __construct(/*** string ***/ $dirname)
55     {
56         $this->mDirname = $dirname;
57     }
58
59     /**
60      * &getInstance
61      * 
62      * @param   string  $dirname
63      * 
64      * @return  Sd2nd_AssetManager
65     **/
66     public function &getInstance(/*** string ***/ $dirname)
67     {
68         /**
69          *  @var    Sd2nd_AssetManager[]
70         **/
71         static $instance = array();
72     
73         if(!isset($instance[$dirname]))
74         {
75             $instance[$dirname] = new Sd2nd_AssetManager($dirname);
76         }
77     
78         return $instance[$dirname];
79     }
80
81     /**
82      * &getObject
83      * 
84      * @param   string  $type
85      * @param   string  $name
86      * @param   bool  $isAdmin
87      * @param   string  $mode
88      * 
89      * @return  &object<XCube_ActionFilter,XCube_ActionForm,XoopsObjectGenericHandler>
90     **/
91     public function &getObject(/*** string ***/ $type,/*** string ***/ $name,/*** bool ***/ $isAdmin = false,/*** string ***/ $mode = null)
92     {
93         if(isset($this->_mCache[$type][$name]))
94         {
95             return $this->_mCache[$type][$name];
96         }
97     
98         $instance = null;
99         
100         $methodName = 'create' . ucfirst($name) . ucfirst($mode) . ucfirst($type);
101         if(method_exists($this,$methodName))
102         {
103             $instance =& $this->$methodName();
104         }
105     
106         if($instance === null)
107         {
108             $instance =& $this->_fallbackCreate($type,$name,$isAdmin,$mode);
109         }
110     
111         $this->_mCache[$type][$name] =& $instance;
112     
113         return $instance;
114     }
115
116     /**
117      * getRoleName
118      * 
119      * @param   string  $role
120      * 
121      * @return  string
122     **/
123     public function getRoleName(/*** string ***/ $role)
124     {
125         return 'Module.' . $this->mDirname . '.' . $role;
126     }
127
128     /**
129      * &_fallbackCreate
130      * 
131      * @param   string  $type
132      * @param   string  $name
133      * @param   bool  $isAdmin
134      * @param   string  $mode
135      * 
136      * @return  &object<XCube_ActionFilter,XCube_ActionForm,XoopsObjectGenericHandler>
137     **/
138     private function &_fallbackCreate(/*** string ***/ $type,/*** string ***/ $name,/*** bool ***/ $isAdmin = false,/*** string ***/ $mode = null)
139     {
140         $className = null;
141         $instance = null;
142     
143         if(isset($this->mAssetList[$type][$name]['class']))
144         {
145             $asset = $this->mAssetList[$type][$name];
146             if(isset($asset['absPath']) && $this->_loadClassFile($asset['absPath'],$asset['class']))
147             {
148                 $className = $asset['class'];
149             }
150     
151             if($className == null && isset($asset['path']))
152             {
153                 if($this->_loadClassFile($this->_getPublicPath() . $asset['path'],$asset['class']))
154                 {
155                     $className = $asset['class'];
156                 }
157     
158                 if($className == null && $this->_loadClassFile($this->_getTrustPath() . $asset['path'],$asset['class']))
159                 {
160                     $className = $asset['class'];
161                 }
162             }
163         }
164     
165         if($className == null)
166         {
167             switch($type)
168             {
169                 case 'filter':
170                     $className = $this->_getFilterName($name,$isAdmin);
171                     break;
172                 case 'form':
173                     $className = $this->_getActionFormName($name,$isAdmin,$mode);
174                     break;
175                 case 'handler':
176                     $className = $this->_getHandlerName($name);
177                     break;
178                 default:
179                     return $instance;
180             }
181         }
182     
183         if($type == 'handler')
184         {
185             $root =& XCube_Root::getSingleton();
186             $instance = new $className($root->mController->getDB(),$this->mDirname);
187         }
188         else
189         {
190             $instance = new $className();
191         }
192         return $instance;
193     }
194
195     /**
196      * _getFilterName
197      * 
198      * @param   string  $name
199      * @param   bool  $isAdmin
200      * 
201      * @return  string
202     **/
203     private function _getFilterName(/*** string ***/ $name,/*** bool ***/ $isAdmin = false)
204     {
205         $name = ucfirst($name) . 'FilterForm';
206         $path = 'forms/' . $name . '.class.php';
207         $className = ucfirst($this->mTrustDirname) . ($isAdmin ? '_Admin_' : '_') . $name;
208         return (
209             $this->_loadClassFile($this->_getPublicPath($isAdmin) . $path,$className) ||
210             $this->_loadClassFile($this->_getTrustPath($isAdmin) . $path,$className)
211         ) ? $className : null;
212     }
213
214     /**
215      * _getActionFormName
216      * 
217      * @param   string  $name
218      * @param   bool  $isAdmin
219      * @param   string  $mode
220      * 
221      * @return  string
222     **/
223     private function _getActionFormName(/*** string ***/ $name,/*** bool ***/ $isAdmin = false,/*** string ***/ $mode = null)
224     {
225         $name = ucfirst($name) . ucfirst($mode) . 'Form';
226         $path = 'forms/' . $name . '.class.php';
227         $className = ucfirst($this->mTrustDirname) . ($isAdmin ? '_Admin_' : '_') . $name;
228         return (
229             $this->_loadClassFile($this->_getPublicPath($isAdmin) . $path,$className) ||
230             $this->_loadClassFile($this->_getTrustPath($isAdmin) . $path,$className)
231         ) ? $className : null;
232     }
233
234     /**
235      * _getHandlerName
236      * 
237      * @param   string  $name
238      * 
239      * @return  string
240     **/
241     private function _getHandlerName(/*** string ***/ $name)
242     {
243         $path = 'class/handler/' . ucfirst($name) . '.class.php';
244         $className = ucfirst($this->mTrustDirname) . '_' . ucfirst($name) . 'Handler';
245         return (
246             $this->_loadClassFile($this->_getPublicPath() . $path,$className) ||
247             $this->_loadClassFile($this->_getTrustPath() . $path,$className)
248         ) ? $className : null;
249     }
250
251     /**
252      * _loadClassFile
253      * 
254      * @param   string  $path
255      * @param   string  $class
256      * 
257      * @return  bool
258     **/
259     private function _loadClassFile(/*** string ***/ $path,/*** string ***/ $class)
260     {
261         if(!file_exists($path))
262         {
263             return false;
264         }
265         require_once $path;
266     
267         return class_exists($class);
268     }
269
270     /**
271      * _getPublicPath
272      * 
273      * @param   bool  $isAdmin
274      * 
275      * @return  string
276     **/
277     private function _getPublicPath(/*** bool ***/ $isAdmin = false)
278     {
279         return XOOPS_MODULE_PATH . '/' . $this->mDirname . ($isAdmin ? '/admin/' : '/');
280     }
281
282     /**
283      * _getTrustPath
284      * 
285      * @param   bool  $isAdmin
286      * 
287      * @return  string
288     **/
289     private function _getTrustPath(/*** bool ***/ $isAdmin = false)
290     {
291         return SD2ND_TRUST_PATH . ($isAdmin ? '/admin/' : '/');
292     }
293 }
294
295 ?>