1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | /** |
---|
5 | * Base Factory abstract class. |
---|
6 | * This class implements all the common behaviour of |
---|
7 | * it's specialized classes (ProcessFactory and WorkflowFactory). |
---|
8 | * |
---|
9 | * @author Pedro Eugênio Rocha |
---|
10 | * @package Factory |
---|
11 | * @abstract |
---|
12 | */ |
---|
13 | abstract class BaseFactory { |
---|
14 | |
---|
15 | /** |
---|
16 | * @var array Store what classes we can instantiate. |
---|
17 | * @access private |
---|
18 | */ |
---|
19 | private $_fileInfo; |
---|
20 | |
---|
21 | |
---|
22 | /** |
---|
23 | * Registers classes into '$_fileInfo' private |
---|
24 | * atribute. |
---|
25 | * |
---|
26 | * @todo Maybe here we should receive more parameters... |
---|
27 | * @access protected |
---|
28 | * @return void |
---|
29 | */ |
---|
30 | protected final function registerFileInfo($className, $fileName, $relativePath) { |
---|
31 | |
---|
32 | /* default is to override */ |
---|
33 | $this->_fileInfo[$className] = array( 'filename' => $fileName, |
---|
34 | 'path' => $relativePath, |
---|
35 | 'instance' => null); |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * Here we should do all the factory stuff. |
---|
41 | * The classes instatiated here will be stored |
---|
42 | * into our private object cache. If there is no |
---|
43 | * object of the given type into the cache, 'newInstance' |
---|
44 | * will be called. |
---|
45 | * |
---|
46 | * @access public |
---|
47 | * @return object |
---|
48 | */ |
---|
49 | public function &getInstance(){ |
---|
50 | |
---|
51 | /* at least the class name */ |
---|
52 | if (func_num_args() <= 0) |
---|
53 | return null; |
---|
54 | |
---|
55 | $args = func_get_args(); |
---|
56 | |
---|
57 | /* recovering class data */ |
---|
58 | if ($entry = $this->_getEntry($args[0])) { |
---|
59 | |
---|
60 | if (is_null($entry['instance'])) { |
---|
61 | |
---|
62 | /* must instantiate it */ |
---|
63 | if (($obj = call_user_func_array(array($this, "newInstance"), $args)) == null) |
---|
64 | throw new Exception("Unable to instantiate '".$args[0]."'"); |
---|
65 | |
---|
66 | /* saving the object reference */ |
---|
67 | $this->_setEntryInstance($args[0], $obj); |
---|
68 | return $obj; |
---|
69 | } |
---|
70 | return $entry['instance']; |
---|
71 | |
---|
72 | } |
---|
73 | /* class not allowed */ |
---|
74 | else |
---|
75 | throw new Exception("You are not allowed to instantiate class '".$args[0]."'."); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Instantiate classes. |
---|
80 | * |
---|
81 | * @todo I think that we don't need to use the reflection |
---|
82 | * class here. Future work... |
---|
83 | * @access public |
---|
84 | * @return object |
---|
85 | */ |
---|
86 | public function &newInstance(){ |
---|
87 | |
---|
88 | /* at least the class name */ |
---|
89 | if (func_num_args() <= 0) |
---|
90 | return null; |
---|
91 | |
---|
92 | $args = func_get_args(); |
---|
93 | |
---|
94 | $className = array_shift($args); |
---|
95 | if(!$this->_import($className)) |
---|
96 | return null; |
---|
97 | |
---|
98 | /* I dont know.. maybe we could do better here.. */ |
---|
99 | $reflectionObj = new ReflectionClass($className); |
---|
100 | |
---|
101 | if (count($args) == 0) |
---|
102 | return $reflectionObj->newInstance(); |
---|
103 | return $reflectionObj->newInstanceArgs($args); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * Private stuff. Handles the cache and information |
---|
109 | * '$_fileInfo' array. |
---|
110 | * |
---|
111 | * @access private |
---|
112 | * @return array |
---|
113 | */ |
---|
114 | private function _getEntry($className){ |
---|
115 | return $this->_fileInfo[$className]; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | * Stores the given object into the internal cache |
---|
121 | * for upcoming requests. |
---|
122 | * |
---|
123 | * @access private |
---|
124 | * @return boolean |
---|
125 | */ |
---|
126 | private function _setEntryInstance($className, $obj){ |
---|
127 | |
---|
128 | if (is_array($this->_fileInfo[$className])) { |
---|
129 | $this->_fileInfo[$className]['instance'] = $obj; |
---|
130 | return true; |
---|
131 | } |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | * Including (requiring_once ;P ) the file(s) itself. Ideally, |
---|
138 | * it could be the only place to include files. |
---|
139 | * |
---|
140 | * @access private |
---|
141 | * @return boolean |
---|
142 | */ |
---|
143 | private function _import($className){ |
---|
144 | |
---|
145 | /* not found */ |
---|
146 | if (!($entry = $this->_getEntry($className))) |
---|
147 | throw new Exception('You are not allowed to instantiate '.$className.' class.'); |
---|
148 | |
---|
149 | $fullPath = WF_SERVER_ROOT . $entry['path'] . SEP . $entry['filename']; |
---|
150 | |
---|
151 | /* file not found */ |
---|
152 | if (!file_exists($fullPath)) |
---|
153 | throw new Exception("File '".$fullPath."' not found."); |
---|
154 | |
---|
155 | /* including file */ |
---|
156 | require_once $fullPath; |
---|
157 | return true; |
---|
158 | } |
---|
159 | } |
---|
160 | ?> |
---|