-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCtkFactory.php
261 lines (237 loc) · 6.58 KB
/
CtkFactory.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* Base factory class for view objects.
*
* PHP 5
*
* Cake Toolkit (http://caketoolkit.org)
* Copyright 2012, James Watts (http://github.com/jameswatts)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2012, James Watts (http://github.com/jameswatts)
* @link http://caketoolkit.org Cake Toolkit
* @package Ctk.Lib
* @since CakePHP(tm) v 2.2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Set', 'Utility');
App::uses('HelperCollection', 'View');
App::uses('CtkLoadable', 'Ctk.Lib');
App::uses('CtkObject', 'Ctk.Lib');
App::uses('CtkView', 'Ctk.View');
App::uses('CtkHelper', 'Ctk.View');
/**
* Abstract class for defining view object factories.
*
* @package Ctk.Lib
*/
abstract class CtkFactory extends CtkObject implements CtkLoadable {
/**
* Settings for this factory.
*
* @var array
*/
public $settings = array();
/**
* An array of names of element factories to include.
*
* @var mixed A single name as a string or a list of names as an array.
*/
public $factories = array();
/**
* An array containing the names of helpers this factory uses.
*
* @var mixed A single name as a string or a list of names as an array.
*/
public $helpers = array();
/**
* The current view calling the factory.
*
* @var CtkView The current view.
*/
protected $_view = null;
/**
* The name of this factory.
*
* @var string The name of the factory.
*/
protected $_name = null;
/**
* The plugin for this factory.
*
* @var string The plugin for the factory.
*/
protected $_plugin = null;
/**
* Sub-factories defined to use in this factory.
*
* @var array
*/
protected $_factories = array();
/**
* The helpers available.
*
* @var array
*/
protected $_helpers = array();
/**
* Contructor
*
* Creates a new factory with a reference to the current view.
*
* @param CtkView $view The current view.
* @param string $name The name of the factory.
* @param string $plugin The name of the plugin if it exists.
* @param array $settings Configuration settings for the factory.
*/
final public function __construct(CtkView $view, $name, $plugin = null, $settings = null) {
parent::__construct();
$this->_view = $view;
$this->_name = (string) $name;
$this->_plugin = (string) $plugin;
if ($settings) {
$this->settings = Set::merge($this->settings, (array) $settings);
}
$this->_inheritArrayProperties(array('factories', 'helpers'));
$this->_factories = (empty($this->factories))? array() : Set::normalize((array) $this->factories);
foreach ($this->_factories as $key => $value) {
$isAlias = (is_array($value) && isset($value['className']));
list($plugin, $name) = pluginSplit(($isAlias)? $value['className'] : $key);
$class = $name . 'Factory';
App::uses($class, ((!empty($plugin))? $plugin . '.' : '') . 'View/Factory');
if (!class_exists($class)) {
throw new CakeException(sprintf('Unknown factory: %s', $class));
}
$property = ($isAlias)? $key : $name;
$this->$property = new $class($this->_view, $name, $plugin, $value);
$this->$property->setup();
}
$helpers = HelperCollection::normalizeObjectArray((empty($this->helpers))? array() : Set::normalize((array) $this->helpers));
$helperCollection = new HelperCollection($this->_view->getBaseView());
foreach ($helpers as $name => $properties) {
list($plugin, $class) = pluginSplit($properties['class']);
$this->_helpers[$class] = new CtkHelper($class, $helperCollection->load($properties['class'], $properties['settings']), $this->_view);
}
}
/**
* Returns a helper or setting from the factory.
*
* @param string $name Name of helper or setting.
* @return mixed
* @throws CakeException if the helper or setting is not found.
*/
final public function __get($name) {
if (isset($this->_helpers[$name])) {
return $this->_helpers[$name];
}
if (array_key_exists($name, $this->settings)) {
return $this->settings[$name];
}
throw new CakeException(sprintf('Unknown helper or setting: %s', $name));
}
/**
* Adds a sub-factory to this factory.
*
* @param string $name Name of the factory.
* @param CtkFactory $factory The factory object.
* @throws CakeException if factory has not been previously included.
*/
final public function __set($name, $factory) {
if (is_object($factory) && $factory instanceof CtkFactory) {
$this->$name = $factory;
} else {
throw new CakeException(sprintf('Invalid factory: %s', $name));
}
}
/**
* Returns a new instance of the given object name.
*
* @param string $name Name of object to create.
* @param array $arguments The method arguments.
* @return CtkNode
* @throws CakeException if object class is not found.
*/
final public function __call($name, $arguments) {
$class = $this->_name . $name;
App::uses($class, ((!empty($this->_plugin))? $this->_plugin . '.' : '') . 'View/Factory/' . $this->_name . '/Objects/');
if (!class_exists($class)) {
throw new CakeException(sprintf('Unknown object: %s', $class));
}
return new $class($this, (count($arguments) > 0)? $arguments[0] : array());
}
/**
* Returns the sub-factories used by this factory.
*
* @return array
*/
final public function getFactories() {
return $this->_factories;
}
/**
* Returns the helpers used by this factory.
*
* @return array
*/
final public function getHelpers() {
return $this->_helpers;
}
/**
* Returns the view object for this factory.
*
* @return CtkView
*/
final public function getView() {
return $this->_view;
}
/**
* Returns the name of the current factory.
*
* @return string
*/
final public function getName() {
return $this->_name;
}
/**
* Returns the plugin for the current factory.
*
* @return string
*/
final public function getPlugin() {
return $this->_plugin;
}
/**
* Determines if the factory has the specified object.
*
* @param string $name Name of object to search for.
* @return boolean
*/
final public function hasObject($name) {
$class = $this->_name . $name;
App::uses($class, ((!empty($this->_plugin))? $this->_plugin . '.' : '') . 'View/Factory/' . $this->_name . '/Objects/');
return class_exists($class);
}
/**
* Sets the factory as loaded.
*
* @return void
*/
final public function load() {
self::registerClass($this);
}
/**
* Determines if the factory has been previously loaded.
*
* @return boolean
*/
final public function isLoaded() {
return self::isClassRegistered($this);
}
/**
* Abstract method used to setup additional resources for the factory.
*
* @return void
*/
abstract public function setup();
}