PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/core/common.inc.php

https://github.com/ksv/limb
PHP | 301 lines | 261 code | 23 blank | 17 comment | 23 complexity | ce8119e8e54b8f4507c31c2fb3d20bb1 MD5 | raw file
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * @package core
  11. * @version $Id: common.inc.php 8119 2010-02-01 13:11:44Z korchasa $
  12. */
  13. if(!isset($_ENV['LIMB_LAZY_CLASS_PATHS']))
  14. $_ENV['LIMB_LAZY_CLASS_PATHS'] = array();
  15. if(!isset($_ENV['LIMB_LAZY_TRIED']))
  16. $_ENV['LIMB_LAZY_TRIED'] = array();
  17. define('LIMB_UNDEFINED', 'undefined' . microtime());
  18. define('LIMB_PACKAGES_DIR', dirname(__FILE__) . '/../');
  19. define('LIMB_DUMP_MAX_DEPTH', 7);
  20. lmb_require('limb/core/src/assert.inc.php');
  21. lmb_require('limb/core/src/env.inc.php');
  22. lmb_require('limb/core/src/package.inc.php');
  23. lmb_require('limb/core/src/string.inc.php');
  24. lmb_require('limb/core/src/exception/lmbException.class.php');
  25. lmb_require('limb/core/src/exception/lmbPHPFileNotFoundException.class.php');
  26. lmb_require('limb/core/src/exception/lmbInvalidArgumentException.class.php');
  27. lmb_require('limb/core/src/lmbBacktrace.class.php');
  28. lmb_require('limb/core/src/lmbErrorGuard.class.php');
  29. function lmb_resolve_include_path($path)
  30. {
  31. if(function_exists('stream_resolve_include_path'))
  32. return stream_resolve_include_path($path);
  33. foreach(lmb_get_include_path_items() as $dir)
  34. {
  35. $full_path = "$dir/$path";
  36. if(is_file($full_path) || is_dir($full_path))
  37. return $full_path;
  38. }
  39. return false;
  40. }
  41. function lmb_glob($path)
  42. {
  43. if(lmb_is_path_absolute($path))
  44. return glob($path);
  45. $result = array();
  46. foreach(lmb_get_include_path_items() as $dir)
  47. {
  48. if($res = glob("$dir/$path"))
  49. {
  50. foreach($res as $item)
  51. $result[] = $item;
  52. }
  53. }
  54. return $result;
  55. }
  56. function lmb_get_include_path_items()
  57. {
  58. return explode(PATH_SEPARATOR, get_include_path());
  59. }
  60. function lmb_is_path_absolute($path)
  61. {
  62. if(!$path)
  63. return false;
  64. //very trivial check, is more comprehensive one required?
  65. return (($path{0} == '/' || $path{0} == '\\') ||
  66. (strlen($path) > 2 && $path{1} == ':'));
  67. }
  68. function lmb_require($file_path, $class = '')
  69. {
  70. if(isset($_ENV['LIMB_LAZY_TRIED'][$file_path . $class]))
  71. return;
  72. else
  73. $_ENV['LIMB_LAZY_TRIED'][$file_path . $class] = true;
  74. if(strpos($file_path, '*') !== false)
  75. {
  76. $file_paths = lmb_glob($file_path);
  77. if(is_array($file_paths))
  78. foreach($file_paths as $path)
  79. lmb_require($path);
  80. return;
  81. }
  82. if(!$class)
  83. {
  84. //autoguessing class or interface name by file
  85. $file = basename($file_path);
  86. $items = explode('.', $file);
  87. if(isset($items[1]))
  88. {
  89. if($items[1] == 'class' || $items[1] == 'interface')
  90. $class = $items[0];
  91. }
  92. }
  93. if($class)
  94. {
  95. $_ENV['LIMB_LAZY_CLASS_PATHS'][$class] = $file_path;
  96. return;
  97. }
  98. try {
  99. $file_found = include_once $file_path;
  100. } catch (lmbException $e) {
  101. if (strpos($e->getMessage(), "include_once($file_path)") !== false)
  102. $file_found = false;
  103. else
  104. throw $e;
  105. }
  106. if(!$file_found)
  107. throw new lmbPHPFileNotFoundException("Could not include source file '$file_path'");
  108. }
  109. function lmb_require_class($file_path, $class = '')
  110. {
  111. if(!$class)
  112. {
  113. //autoguessing class or interface name by file
  114. $file = basename($file_path);
  115. $items = explode('.', $file);
  116. $class = $items[0];
  117. }
  118. $_ENV['LIMB_LAZY_CLASS_PATHS'][$class] = $file_path;
  119. }
  120. function lmb_require_glob($file_path)
  121. {
  122. if(strpos($file_path, '*') !== false)
  123. {
  124. foreach(lmb_glob($file_path) as $path)
  125. lmb_require($path);
  126. }
  127. else
  128. lmb_require($path);
  129. }
  130. function lmb_require_optional($file_path)
  131. {
  132. if(!file_exists($file_path))
  133. return;
  134. lmb_require($file_path);
  135. }
  136. function lmb_autoload($name)
  137. {
  138. if(isset($_ENV['LIMB_LAZY_CLASS_PATHS'][$name]))
  139. {
  140. $file_path = $_ENV['LIMB_LAZY_CLASS_PATHS'][$name];
  141. //is it safe to use include here instead of include_once?
  142. if(!include_once($file_path))
  143. {
  144. $message = "Could not include source file '$file_path'";
  145. if(class_exists('lmbException') && class_exists('lmbBacktrace'))
  146. {
  147. $trace = new lmbBacktrace(10, 1);
  148. throw new lmbException($message, array('trace' => $trace->toString()));
  149. }
  150. else
  151. {
  152. throw new Exception($message);
  153. }
  154. }
  155. }
  156. }
  157. function lmb_var_dump($arg, $echo = false, $level = 1)
  158. {
  159. if($echo)
  160. echo lmb_var_export($arg, $level);
  161. else
  162. return lmb_var_export($arg, $level);
  163. }
  164. function lmb_var_export($arg, $level = 1)
  165. {
  166. $prefix = str_repeat(' ', ($level > 0) ? ($level - 1) : 0);
  167. switch(gettype($arg))
  168. {
  169. case 'NULL':
  170. return 'NULL';
  171. case 'boolean':
  172. return $arg ? 'TRUE' : 'FALSE';
  173. case 'integer':
  174. return 'INT('.$arg.')';
  175. case 'double':
  176. return 'FLOAT('.$arg.')';
  177. case 'resource':
  178. if(is_resource($arg))
  179. {
  180. $resource_id = strstr((string) $arg, '#');
  181. $resource_type = get_resource_type($arg);
  182. return "RESOURCE($resource_id) of type (" . get_resource_type($arg) . ")";
  183. }
  184. else
  185. return lmb_var_export((string) $arg);
  186. case 'object':
  187. if (LIMB_DUMP_MAX_DEPTH == $level)
  188. return 'OBJECT(' . get_class($arg) . ')';
  189. $_ENV['LIMB_VAR_EXPORT_SHOWED_OBJECTS'][spl_object_hash($arg)] = true;
  190. if ($level == LIMB_DUMP_MAX_DEPTH)
  191. return 'OBJECT(' . get_class($arg) . ")";
  192. else
  193. {
  194. $dump = 'OBJECT(' . get_class($arg) . ") {";
  195. if (get_object_vars($arg))
  196. {
  197. $dump .= PHP_EOL;
  198. foreach(get_object_vars($arg) as $name => $value)
  199. {
  200. $dump .= $prefix . " [\"$name\"]=> "
  201. . lmb_var_export($value, $level + 1)
  202. . PHP_EOL;
  203. }
  204. $dump .= $prefix;
  205. }
  206. $dump .= "}";
  207. return $dump;
  208. }
  209. case 'array':
  210. if($level == LIMB_DUMP_MAX_DEPTH)
  211. return 'ARRAY(' . sizeof($arg) . ')';
  212. else
  213. {
  214. $dump = "ARRAY(".sizeof($arg).') [';
  215. if(sizeof($arg))
  216. {
  217. $dump .= PHP_EOL;
  218. foreach($arg as $arr_key => $arr_value)
  219. $dump .= $prefix . " [$arr_key] => ".lmb_var_export($arr_value, $level + 1).PHP_EOL;
  220. $dump .= $prefix;
  221. }
  222. $dump .= "]";
  223. return $dump;
  224. }
  225. case 'string':
  226. $dump = 'STRING('.strlen($arg).') "';
  227. $dump .= lmb_escape_string((string) $arg, 100);
  228. if(strlen($arg) > 100)
  229. $dump .= '...';
  230. $dump .= '"';
  231. return $dump;
  232. default:
  233. return var_export($arg, true);
  234. }
  235. }
  236. /**
  237. * Escaping string for log.
  238. * If we work in cli mode we don't need htmlspecialchars
  239. * @param $string
  240. */
  241. function lmb_escape_string($string, $length_limit = 100)
  242. {
  243. if ('cli' == php_sapi_name())
  244. return substr($string, 0, $length_limit);
  245. else
  246. return htmlspecialchars(substr($string, 0, $length_limit));
  247. }
  248. function lmb_var_dir($value = null)
  249. {
  250. if($value)
  251. lmb_env_set('LIMB_VAR_DIR', $value);
  252. else
  253. return lmb_env_get('LIMB_VAR_DIR');
  254. }
  255. spl_autoload_register('lmb_autoload');
  256. new lmbException('ugly hack');
  257. new lmbPHPFileNotFoundException('ugly hack');
  258. lmbErrorGuard::registerErrorHandler('lmbErrorGuard', 'convertErrorsToExceptions');