/base.php

https://github.com/mjphaynes/core · PHP · 230 lines · 122 code · 27 blank · 81 comment · 17 complexity · 5c0c0f939d29869d4d55af3af22d06ac MD5 · raw file

  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. /**
  13. * Loads in a core class and optionally an app class override if it exists.
  14. *
  15. * @param string $path
  16. * @param string $folder
  17. * @return void
  18. */
  19. if ( ! function_exists('import'))
  20. {
  21. function import($path, $folder = 'classes')
  22. {
  23. $path = str_replace('/', DIRECTORY_SEPARATOR, $path);
  24. require_once COREPATH.$folder.DIRECTORY_SEPARATOR.$path.'.php';
  25. if (is_file(APPPATH.$folder.DIRECTORY_SEPARATOR.$path.'.php'))
  26. {
  27. require_once APPPATH.$folder.DIRECTORY_SEPARATOR.$path.'.php';
  28. }
  29. }
  30. }
  31. if ( ! function_exists('logger'))
  32. {
  33. function logger($level, $msg, $method = null)
  34. {
  35. if ($level > \Config::get('log_threshold'))
  36. {
  37. return false;
  38. }
  39. ! class_exists('Fuel\\Core\\Log') and import('log');
  40. ! class_exists('Log') and class_alias('Fuel\\Core\\Log', 'Log');
  41. return \Log::write($level, $msg, $method);
  42. }
  43. }
  44. /**
  45. * Takes an array of attributes and turns it into a string for an html tag
  46. *
  47. * @param array $attr
  48. * @return string
  49. */
  50. if ( ! function_exists('array_to_attr'))
  51. {
  52. function array_to_attr($attr)
  53. {
  54. $attr_str = '';
  55. if ( ! is_array($attr))
  56. {
  57. $attr = (array) $attr;
  58. }
  59. foreach ($attr as $property => $value)
  60. {
  61. // Ignore null values
  62. if (is_null($value))
  63. {
  64. continue;
  65. }
  66. // If the key is numeric then it must be something like selected="selected"
  67. if (is_numeric($property))
  68. {
  69. $property = $value;
  70. }
  71. $attr_str .= $property.'="'.$value.'" ';
  72. }
  73. // We strip off the last space for return
  74. return trim($attr_str);
  75. }
  76. }
  77. /**
  78. * Create a XHTML tag
  79. *
  80. * @param string The tag name
  81. * @param array|string The tag attributes
  82. * @param string|bool The content to place in the tag, or false for no closing tag
  83. * @return string
  84. */
  85. if ( ! function_exists('html_tag'))
  86. {
  87. function html_tag($tag, $attr = array(), $content = false)
  88. {
  89. $has_content = (bool) ($content !== false and $content !== null);
  90. $html = '<'.$tag;
  91. $html .= ( ! empty($attr)) ? ' '.(is_array($attr) ? array_to_attr($attr) : $attr) : '';
  92. $html .= $has_content ? '>' : ' />';
  93. $html .= $has_content ? $content.'</'.$tag.'>' : '';
  94. return $html;
  95. }
  96. }
  97. /**
  98. * A case-insensitive version of in_array.
  99. *
  100. * @param mixed $needle
  101. * @param array $haystack
  102. * @return bool
  103. */
  104. if ( ! function_exists('in_arrayi'))
  105. {
  106. function in_arrayi($needle, $haystack)
  107. {
  108. return in_array(strtolower($needle), array_map('strtolower', $haystack));
  109. }
  110. }
  111. /**
  112. * Gets all the public vars for an object. Use this if you need to get all the
  113. * public vars of $this inside an object.
  114. *
  115. * @return array
  116. */
  117. if ( ! function_exists('get_object_public_vars'))
  118. {
  119. function get_object_public_vars($obj)
  120. {
  121. return get_object_vars($obj);
  122. }
  123. }
  124. /**
  125. * Renders a view and returns the output.
  126. *
  127. * @param string The view name/path
  128. * @param array The data for the view
  129. * @return string
  130. */
  131. if ( ! function_exists('render'))
  132. {
  133. function render($view, $data = array())
  134. {
  135. return \View::forge($view, $data)->render();
  136. }
  137. }
  138. /**
  139. * A wrapper function for Lang::get()
  140. *
  141. * @param mixed The string to translate
  142. * @param array The parameters
  143. * @return string
  144. */
  145. if ( ! function_exists('__'))
  146. {
  147. function __($string, $params = array(), $default = null)
  148. {
  149. return \Lang::get($string, $params, $default);
  150. }
  151. }
  152. /**
  153. * Encodes the given string. This is just a wrapper function for Security::htmlentities()
  154. *
  155. * @param mixed The string to encode
  156. * @return string
  157. */
  158. if ( ! function_exists('e'))
  159. {
  160. function e($string)
  161. {
  162. return Security::htmlentities($string);
  163. }
  164. }
  165. /**
  166. * Takes a classname and returns the actual classname for an alias or just the classname
  167. * if it's a normal class.
  168. *
  169. * @param string classname to check
  170. * @return string real classname
  171. */
  172. if ( ! function_exists('get_real_class'))
  173. {
  174. function get_real_class($class)
  175. {
  176. static $classes = array();
  177. if ( ! array_key_exists($class, $classes))
  178. {
  179. $reflect = new ReflectionClass($class);
  180. $classes[$class] = $reflect->getName();
  181. }
  182. return $classes[$class];
  183. }
  184. }
  185. /**
  186. * Loads in the classes used for the error handlers. The class_exists() calls
  187. * will trigger the autoloader if it is loaded, if not, then it will import
  188. * the classes and do the work itself.
  189. *
  190. * @return void
  191. */
  192. if ( ! function_exists('load_error_classes'))
  193. {
  194. function load_error_classes()
  195. {
  196. ! class_exists('Fuel\\Core\\Error') and import('error');
  197. ! class_exists('Error') and class_alias('Fuel\\Core\\Error', 'Error');
  198. ! class_exists('Fuel\\Core\\Debug') and import('debug');
  199. ! class_exists('Debug') and class_alias('Fuel\\Core\\Debug', 'Debug');
  200. ! class_exists('Fuel\\Core\\View') and import('view');
  201. ! class_exists('View') and class_alias('Fuel\\Core\\View', 'View');
  202. }
  203. }