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