croogo /Croogo/Lib/Croogo.php

Language PHP Lines 292
MD5 Hash abf943a61d45c84c174d7fb9703f5974
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php

App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');

/**
 * Croogo
 *
 * @package  Croogo.Croogo.Lib
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class Croogo {

/**
 * Loads plugin's routes.php from app/config/routes.php.
 *
 * Plugin name is added to Hook.routes key of Configure object.
 *
 * @param string $pluginName plugin name
 * @deprecated Will be removed in the future.
 */
	public static function hookRoutes($pluginName) {
		$hooks = Configure::read('Hook.routes');
		if (!$hooks || !is_array($hooks)) {
			$hooks = array();
		}
		$hooks[] = $pluginName;
		Configure::write('Hook.routes', $hooks);
	}

/**
 * Loads as a normal component from controller.
 *
 * @param string $controllerName Controller Name
 * @param mixed $componentName  Component name or array of Component and settings
 */
	public static function hookComponent($controllerName, $componentName) {
		if (is_string($componentName)) {
			$componentName = array($componentName);
		}
		self::hookControllerProperty($controllerName, '_appComponents', $componentName);
	}

/**
 * Loads an API component to a controller during route setup.
 *
 * @param string $controllerName Controller Name
 * @param mixed $componentName  Component name or array of Component and settings
 */
	public static function hookApiComponent($controllerName, $componentName) {
		$defaults = array(
			'priority' => 8,
		);
		if (is_string($componentName)) {
			$component = array($componentName => $defaults);
		} else {
			$cName = key($componentName);
			$settings = Hash::merge($defaults, $componentName[$cName]);
			$component = array($cName => $settings);
		}
		self::hookControllerProperty($controllerName, '_apiComponents', $component);
	}

/**
 * Attaches Behavior to a Model whenever loaded.
 *
 * @param string $modelName
 * @param string $behaviorName
 * @param array  $config
 */
	public static function hookBehavior($modelName, $behaviorName, $config = array()) {
		self::hookModelProperty($modelName, 'actsAs', array($behaviorName => $config));
	}

/**
 * Loads as a normal helper via controller.
 *
 * @param string $controllerName
 * @param mixed $helperName Helper name or array of Helper and settings
 */
	public static function hookHelper($controllerName, $helperName) {
		if (is_string($helperName)) {
			$helperName = array($helperName);
		}
		self::hookControllerProperty($controllerName, 'helpers', $helperName);
	}

/**
 * Shows plugin's admin_menu element in admin navigation under Extensions.
 *
 * @param string $pluginName
 */
	public static function hookAdminMenu($pluginName) {
		$pluginName = Inflector::underscore($pluginName);
		Configure::write('Admin.menus.' . $pluginName, 1);
	}

/**
 * In admin panel for the provided action, the link will appear in table rows under 'Actions' column.
 *
 * @param string $action in the format ControllerName/action_name
 * @param string $title Link title
 * @param string $url
 */
	public static function hookAdminRowAction($action, $title, $url) {
		$rowActions = Configure::read('Admin.rowActions');
		if (!is_array($rowActions)) {
			$rowActions = array();
		}
		if (!isset($rowActions[$action])) {
			$rowActions[$action] = array();
		}
		$rowActions[$action][$title] = $url;
		Configure::write('Admin.rowActions', $rowActions);
	}

/**
 * Admin tab
 *
 * @param string $action  in the format ControllerName/action_name
 * @param string $title   Tab title
 * @param string $element element name, like plugin_name.element_name
 * @param array  $options array with options for the hook to take effect
 */
	public static function hookAdminTab($action, $title, $element, $options = array()) {
		self::_hookAdminBlock('Admin.tabs', $action, $title, $element, $options);
	}

/**
 * Admin box
 *
 * @param string $action  in the format ControllerName/action_name
 * @param string $title   Box title
 * @param string $element element name, like plugin_name.element_name
 * @param array  $options array with options for the hook to take effect
 */
	public static function hookAdminBox($action, $title, $element, $options = array()) {
		self::_hookAdminBlock('Admin.boxes', $action, $title, $element, $options);
	}

	protected static function _hookAdminBlock($key, $action, $title, $element, $options = array()) {
		$tabs = Configure::read($key);
		if (!is_array($tabs)) {
			$tabs = array();
		}
		if (!isset($tabs[$action])) {
			$tabs[$action] = array();
		}
		$tabs[$action][$title]['element'] = $element;
		$tabs[$action][$title]['options'] = $options;
		Configure::write($key, $tabs);
	}

/**
 * Hook model property
 *
 * Useful when models need to be associated to another one, setting Behaviors, disabling cache, etc.
 *
 * @param string $modelName Model name (for e.g., Node)
 * @param string $property  for e.g., actsAs
 * @param string $value     array or string
 */
	public static function hookModelProperty($modelName, $property, $value) {
		$configKeyPrefix = 'Hook.model_properties';
		self::_hookProperty($configKeyPrefix, $modelName, $property, $value);
	}

/**
 * Hook controller property
 *
 * @param string $controllerName Controller name (for e.g., Nodes)
 * @param string $property       for e.g., components
 * @param string $value          array or string
 */
	public static function hookControllerProperty($controllerName, $property, $value) {
		$configKeyPrefix = 'Hook.controller_properties';
		self::_hookProperty($configKeyPrefix, $controllerName, $property, $value);
	}

/**
 * Hook property
 *
 * @param string $configKeyPrefix
 * @param string $name
 * @param string $property
 * @param string $value
 */
	protected static function _hookProperty($configKeyPrefix, $name, $property, $value) {
		$propertyValue = Configure::read($configKeyPrefix . '.' . $name . '.' . $property);
		if (!is_array($propertyValue)) {
			$propertyValue = null;
		}
		if (is_array($value)) {
			if (is_array($propertyValue)) {
				$propertyValue = Hash::merge($propertyValue, $value);
			} else {
				$propertyValue = $value;
			}
		} else {
			$propertyValue = $value;
		}
		Configure::write($configKeyPrefix . '.' . $name . '.' . $property, $propertyValue);
	}

/**
 * Applies properties set from hooks to an object in __construct()
 *
 * @param string $configKey
 */
	public static function applyHookProperties($configKey, &$object = null) {
		if (empty($object)) {
			$object = self;
		}
		$objectName = empty($object->name) ? get_class($object) : $object->name;
		$hookProperties = Configure::read($configKey . '.' . $objectName);
		if (is_array(Configure::read($configKey . '.*'))) {
			$hookProperties = Hash::merge(Configure::read($configKey . '.*'), $hookProperties);
		}
		if (is_array($hookProperties)) {
			foreach ($hookProperties as $property => $value) {
				if (!isset($object->$property)) {
					$object->$property = $value;
				} else {
					if (is_array($object->$property)) {
						if (is_array($value)) {
							$object->$property = Hash::merge($object->$property, $value);
						} else {
							$object->$property = $value;
						}
					} else {
						$object->$property = $value;
					}
				}
			}
		}
	}

/**
 * Convenience method to dispatch event.
 *
 * Creates, dispatches, and returns a new CakeEvent object.
 *
 * @see CakeEvent::__construct()
 * @param string $name Name of the event
 * @param object $subject the object that this event applies to
 * @param mixed $data any value you wish to be transported with this event
 */
	public static function dispatchEvent($name, $subject = null, $data = null) {
		$event = new CakeEvent($name, $subject, $data);
		if ($subject) {
			$event = $subject->getEventManager()->dispatch($event);
		} else {
			$event = CakeEventManager::instance()->dispatch($event);
		}
		return $event;
	}

/**
 * Get URL relative to the app
 *
 * @param array $url
 * @return array
 */
	public static function getRelativePath($url = '/') {
		if (is_array($url)) {
			$absoluteUrl = Router::url($url, true);
		} else {
			$absoluteUrl = Router::url('/' . $url, true);
		}
		$path = '/' . str_replace(Router::url('/', true), '', $absoluteUrl);
		return $path;
	}

/**
 * Merge Configuration
 *
 * @param string $key Configure key
 * @param array $config New configuration to merge
 * @param return array Array of merged configurations
 */
	public static function mergeConfig($key, $config) {
		$values = Configure::read($key);
		$values = Hash::merge((array)$values, $config);
		Configure::write($key, $values);
		return $values;
	}

}
Back to Top