PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Lib/Croogo.php

https://github.com/kareypowell/croogo
PHP | 291 lines | 149 code | 20 blank | 122 comment | 28 complexity | abf943a61d45c84c174d7fb9703f5974 MD5 | raw file
  1. <?php
  2. App::uses('CakeEvent', 'Event');
  3. App::uses('CakeEventManager', 'Event');
  4. /**
  5. * Croogo
  6. *
  7. * @package Croogo.Croogo.Lib
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class Croogo {
  14. /**
  15. * Loads plugin's routes.php from app/config/routes.php.
  16. *
  17. * Plugin name is added to Hook.routes key of Configure object.
  18. *
  19. * @param string $pluginName plugin name
  20. * @deprecated Will be removed in the future.
  21. */
  22. public static function hookRoutes($pluginName) {
  23. $hooks = Configure::read('Hook.routes');
  24. if (!$hooks || !is_array($hooks)) {
  25. $hooks = array();
  26. }
  27. $hooks[] = $pluginName;
  28. Configure::write('Hook.routes', $hooks);
  29. }
  30. /**
  31. * Loads as a normal component from controller.
  32. *
  33. * @param string $controllerName Controller Name
  34. * @param mixed $componentName Component name or array of Component and settings
  35. */
  36. public static function hookComponent($controllerName, $componentName) {
  37. if (is_string($componentName)) {
  38. $componentName = array($componentName);
  39. }
  40. self::hookControllerProperty($controllerName, '_appComponents', $componentName);
  41. }
  42. /**
  43. * Loads an API component to a controller during route setup.
  44. *
  45. * @param string $controllerName Controller Name
  46. * @param mixed $componentName Component name or array of Component and settings
  47. */
  48. public static function hookApiComponent($controllerName, $componentName) {
  49. $defaults = array(
  50. 'priority' => 8,
  51. );
  52. if (is_string($componentName)) {
  53. $component = array($componentName => $defaults);
  54. } else {
  55. $cName = key($componentName);
  56. $settings = Hash::merge($defaults, $componentName[$cName]);
  57. $component = array($cName => $settings);
  58. }
  59. self::hookControllerProperty($controllerName, '_apiComponents', $component);
  60. }
  61. /**
  62. * Attaches Behavior to a Model whenever loaded.
  63. *
  64. * @param string $modelName
  65. * @param string $behaviorName
  66. * @param array $config
  67. */
  68. public static function hookBehavior($modelName, $behaviorName, $config = array()) {
  69. self::hookModelProperty($modelName, 'actsAs', array($behaviorName => $config));
  70. }
  71. /**
  72. * Loads as a normal helper via controller.
  73. *
  74. * @param string $controllerName
  75. * @param mixed $helperName Helper name or array of Helper and settings
  76. */
  77. public static function hookHelper($controllerName, $helperName) {
  78. if (is_string($helperName)) {
  79. $helperName = array($helperName);
  80. }
  81. self::hookControllerProperty($controllerName, 'helpers', $helperName);
  82. }
  83. /**
  84. * Shows plugin's admin_menu element in admin navigation under Extensions.
  85. *
  86. * @param string $pluginName
  87. */
  88. public static function hookAdminMenu($pluginName) {
  89. $pluginName = Inflector::underscore($pluginName);
  90. Configure::write('Admin.menus.' . $pluginName, 1);
  91. }
  92. /**
  93. * In admin panel for the provided action, the link will appear in table rows under 'Actions' column.
  94. *
  95. * @param string $action in the format ControllerName/action_name
  96. * @param string $title Link title
  97. * @param string $url
  98. */
  99. public static function hookAdminRowAction($action, $title, $url) {
  100. $rowActions = Configure::read('Admin.rowActions');
  101. if (!is_array($rowActions)) {
  102. $rowActions = array();
  103. }
  104. if (!isset($rowActions[$action])) {
  105. $rowActions[$action] = array();
  106. }
  107. $rowActions[$action][$title] = $url;
  108. Configure::write('Admin.rowActions', $rowActions);
  109. }
  110. /**
  111. * Admin tab
  112. *
  113. * @param string $action in the format ControllerName/action_name
  114. * @param string $title Tab title
  115. * @param string $element element name, like plugin_name.element_name
  116. * @param array $options array with options for the hook to take effect
  117. */
  118. public static function hookAdminTab($action, $title, $element, $options = array()) {
  119. self::_hookAdminBlock('Admin.tabs', $action, $title, $element, $options);
  120. }
  121. /**
  122. * Admin box
  123. *
  124. * @param string $action in the format ControllerName/action_name
  125. * @param string $title Box title
  126. * @param string $element element name, like plugin_name.element_name
  127. * @param array $options array with options for the hook to take effect
  128. */
  129. public static function hookAdminBox($action, $title, $element, $options = array()) {
  130. self::_hookAdminBlock('Admin.boxes', $action, $title, $element, $options);
  131. }
  132. protected static function _hookAdminBlock($key, $action, $title, $element, $options = array()) {
  133. $tabs = Configure::read($key);
  134. if (!is_array($tabs)) {
  135. $tabs = array();
  136. }
  137. if (!isset($tabs[$action])) {
  138. $tabs[$action] = array();
  139. }
  140. $tabs[$action][$title]['element'] = $element;
  141. $tabs[$action][$title]['options'] = $options;
  142. Configure::write($key, $tabs);
  143. }
  144. /**
  145. * Hook model property
  146. *
  147. * Useful when models need to be associated to another one, setting Behaviors, disabling cache, etc.
  148. *
  149. * @param string $modelName Model name (for e.g., Node)
  150. * @param string $property for e.g., actsAs
  151. * @param string $value array or string
  152. */
  153. public static function hookModelProperty($modelName, $property, $value) {
  154. $configKeyPrefix = 'Hook.model_properties';
  155. self::_hookProperty($configKeyPrefix, $modelName, $property, $value);
  156. }
  157. /**
  158. * Hook controller property
  159. *
  160. * @param string $controllerName Controller name (for e.g., Nodes)
  161. * @param string $property for e.g., components
  162. * @param string $value array or string
  163. */
  164. public static function hookControllerProperty($controllerName, $property, $value) {
  165. $configKeyPrefix = 'Hook.controller_properties';
  166. self::_hookProperty($configKeyPrefix, $controllerName, $property, $value);
  167. }
  168. /**
  169. * Hook property
  170. *
  171. * @param string $configKeyPrefix
  172. * @param string $name
  173. * @param string $property
  174. * @param string $value
  175. */
  176. protected static function _hookProperty($configKeyPrefix, $name, $property, $value) {
  177. $propertyValue = Configure::read($configKeyPrefix . '.' . $name . '.' . $property);
  178. if (!is_array($propertyValue)) {
  179. $propertyValue = null;
  180. }
  181. if (is_array($value)) {
  182. if (is_array($propertyValue)) {
  183. $propertyValue = Hash::merge($propertyValue, $value);
  184. } else {
  185. $propertyValue = $value;
  186. }
  187. } else {
  188. $propertyValue = $value;
  189. }
  190. Configure::write($configKeyPrefix . '.' . $name . '.' . $property, $propertyValue);
  191. }
  192. /**
  193. * Applies properties set from hooks to an object in __construct()
  194. *
  195. * @param string $configKey
  196. */
  197. public static function applyHookProperties($configKey, &$object = null) {
  198. if (empty($object)) {
  199. $object = self;
  200. }
  201. $objectName = empty($object->name) ? get_class($object) : $object->name;
  202. $hookProperties = Configure::read($configKey . '.' . $objectName);
  203. if (is_array(Configure::read($configKey . '.*'))) {
  204. $hookProperties = Hash::merge(Configure::read($configKey . '.*'), $hookProperties);
  205. }
  206. if (is_array($hookProperties)) {
  207. foreach ($hookProperties as $property => $value) {
  208. if (!isset($object->$property)) {
  209. $object->$property = $value;
  210. } else {
  211. if (is_array($object->$property)) {
  212. if (is_array($value)) {
  213. $object->$property = Hash::merge($object->$property, $value);
  214. } else {
  215. $object->$property = $value;
  216. }
  217. } else {
  218. $object->$property = $value;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Convenience method to dispatch event.
  226. *
  227. * Creates, dispatches, and returns a new CakeEvent object.
  228. *
  229. * @see CakeEvent::__construct()
  230. * @param string $name Name of the event
  231. * @param object $subject the object that this event applies to
  232. * @param mixed $data any value you wish to be transported with this event
  233. */
  234. public static function dispatchEvent($name, $subject = null, $data = null) {
  235. $event = new CakeEvent($name, $subject, $data);
  236. if ($subject) {
  237. $event = $subject->getEventManager()->dispatch($event);
  238. } else {
  239. $event = CakeEventManager::instance()->dispatch($event);
  240. }
  241. return $event;
  242. }
  243. /**
  244. * Get URL relative to the app
  245. *
  246. * @param array $url
  247. * @return array
  248. */
  249. public static function getRelativePath($url = '/') {
  250. if (is_array($url)) {
  251. $absoluteUrl = Router::url($url, true);
  252. } else {
  253. $absoluteUrl = Router::url('/' . $url, true);
  254. }
  255. $path = '/' . str_replace(Router::url('/', true), '', $absoluteUrl);
  256. return $path;
  257. }
  258. /**
  259. * Merge Configuration
  260. *
  261. * @param string $key Configure key
  262. * @param array $config New configuration to merge
  263. * @param return array Array of merged configurations
  264. */
  265. public static function mergeConfig($key, $config) {
  266. $values = Configure::read($key);
  267. $values = Hash::merge((array)$values, $config);
  268. Configure::write($key, $values);
  269. return $values;
  270. }
  271. }