Source for file Tools.php

Documentation is available at Tools.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: /**
  23. 23:  * Tools library.
  24. 24:  *
  25. 25:  * @author     David Grudl
  26. 26:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  27. 27:  * @package    Nette
  28. 28:  */
  29. 29: final class Tools
  30. 30: {
  31. 31:     /** minute in seconds */
  32. 32:     const MINUTE 60;
  33. 33:  
  34. 34:     /** hour in seconds */
  35. 35:     const HOUR 3600;
  36. 36:  
  37. 37:     /** day in seconds */
  38. 38:     const DAY 86400;
  39. 39:  
  40. 40:     /** week in seconds */
  41. 41:     const WEEK 604800;
  42. 42:  
  43. 43:     /** average month in seconds */
  44. 44:     const MONTH 2629800;
  45. 45:  
  46. 46:     /** average year in seconds */
  47. 47:     const YEAR 31557600;
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * Static class - cannot be instantiated.
  53. 53:      */
  54. 54:     final public function __construct()
  55. 55:     {
  56. 56:         throw new LogicException("Cannot instantiate static class " get_class($this));
  57. 57:     }
  58. 58:  
  59. 59:  
  60. 60:  
  61. 61:     /**
  62. 62:      * Gets the boolean value of a configuration option.
  63. 63:      * @param  string  configuration option name
  64. 64:      * @return bool 
  65. 65:      */
  66. 66:     public static function iniFlag($var)
  67. 67:     {
  68. 68:         $status strtolower(ini_get($var));
  69. 69:         return $status === 'on' || $status === 'true' || $status === 'yes' || $status 256;
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Initializes variable with $default value.
  76. 76:      *
  77. 77:      * @param  mixed  variable
  78. 78:      * @param  mixed  default value
  79. 79:      * @return void 
  80. 80:      */
  81. 81:     public static function defaultize(&$var$default)
  82. 82:     {
  83. 83:         if ($var === NULL$var $default;
  84. 84:     }
  85. 85:  
  86. 86:  
  87. 87:  
  88. 88:     /**
  89. 89:      * Returns array item or $default if item is not set.
  90. 90:      * Example: $val = arrayGet($arr, 'i', 123);
  91. 91:      *
  92. 92:      * @param  mixed  array
  93. 93:      * @param  scalar key
  94. 94:      * @param  mixed  default value
  95. 95:      * @return mixed 
  96. 96:      */
  97. 97:     public static function arrayGet(array $arr$key$default NULL)
  98. 98:     {
  99. 99:         if (isset($arr[$key])) return $arr[$key];
  100. 100:         return $default;
  101. 101:     }
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Recursively appends elements of remaining keys from the second array to the first.
  107. 107:      * @param  array 
  108. 108:      * @param  array 
  109. 109:      * @return array 
  110. 110:      */
  111. 111:     public static function arrayMergeTree($arr1$arr2)
  112. 112:     {
  113. 113:         $res $arr1 $arr2;
  114. 114:         foreach (array_intersect_key($arr1$arr2as $k => $v{
  115. 115:             if (is_array($v&& is_array($arr2[$k])) {
  116. 116:                 $res[$kself::arrayMergeTree($v$arr2[$k]);
  117. 117:             }
  118. 118:         }
  119. 119:         return $res;
  120. 120:     }
  121. 121:  
  122. 122:  
  123. 123:  
  124. 124:     /**
  125. 125:      * Recursive glob(). Finds pathnames matching a pattern.
  126. 126:      * @param  string 
  127. 127:      * @param  int 
  128. 128:      * @return array 
  129. 129:      */
  130. 130:     public static function glob($pattern$flags 0)
  131. 131:     {
  132. 132:         // TODO: replace by RecursiveDirectoryIterator
  133. 133:         $files glob($pattern$flags);
  134. 134:         if (!is_array($files)) {
  135. 135:             $files array();
  136. 136:         }
  137. 137:  
  138. 138:         $dirs glob(dirname($pattern'/*'$flags GLOB_ONLYDIR);
  139. 139:         if (is_array($dirs)) {
  140. 140:             $mask basename($pattern);
  141. 141:             foreach ($dirs as $dir{
  142. 142:                 $files array_merge($filesself::glob($dir '/' $mask$flags));
  143. 143:             }
  144. 144:         }
  145. 145:  
  146. 146:         return $files;
  147. 147:     }
  148. 148:  
  149. 149:  
  150. 150:  
  151. 151:     /********************* errors and warnings catching ****************d*g**/
  152. 152:  
  153. 153:  
  154. 154:  
  155. 155:     /** @var string */
  156. 156:     private static $errorMsg;
  157. 157:  
  158. 158:  
  159. 159:  
  160. 160:     /**
  161. 161:      * Starts catching potential errors/warnings.
  162. 162:      *
  163. 163:      * @return void 
  164. 164:      */
  165. 165:     public static function tryError($level E_ALL)
  166. 166:     {
  167. 167:         set_error_handler(array(__CLASS__'_errorHandler')$level);
  168. 168:         self::$errorMsg NULL;
  169. 169:     }
  170. 170:  
  171. 171:  
  172. 172:  
  173. 173:     /**
  174. 174:      * Returns catched error/warning message.
  175. 175:      *
  176. 176:      * @param  string  catched message
  177. 177:      * @return bool 
  178. 178:      */
  179. 179:     public static function catchError($message)
  180. 180:     {
  181. 181:         restore_error_handler();
  182. 182:         $message self::$errorMsg;
  183. 183:         self::$errorMsg NULL;
  184. 184:         return $message !== NULL;
  185. 185:     }
  186. 186:  
  187. 187:  
  188. 188:  
  189. 189:     /**
  190. 190:      * Internal error handler. Do not call directly.
  191. 191:      * @ignore internal
  192. 192:      */
  193. 193:     public static function _errorHandler($code$message)
  194. 194:     {
  195. 195:         if (ini_get('html_errors')) {
  196. 196:             $message strip_tags($message);
  197. 197:             $message html_entity_decode($message);
  198. 198:         }
  199. 199:  
  200. 200:         if (($a strpos($message': ')) !== FALSE{
  201. 201:             $message substr($message$a 2);
  202. 202:         }
  203. 203:  
  204. 204:         self::$errorMsg $message;
  205. 205:     }
  206. 206: