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  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Tools library.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette
  20. 20:  */
  21. 21: final class Tools
  22. 22: {
  23. 23:     /** minute in seconds */
  24. 24:     const MINUTE 60;
  25. 25:  
  26. 26:     /** hour in seconds */
  27. 27:     const HOUR 3600;
  28. 28:  
  29. 29:     /** day in seconds */
  30. 30:     const DAY 86400;
  31. 31:  
  32. 32:     /** week in seconds */
  33. 33:     const WEEK 604800;
  34. 34:  
  35. 35:     /** average month in seconds */
  36. 36:     const MONTH 2629800;
  37. 37:  
  38. 38:     /** average 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:      * DateTime object factory.
  55. 55:      * @param  string|int|DateTime
  56. 56:      * @return DateTime 
  57. 57:      */
  58. 58:     public static function createDateTime($time)
  59. 59:     {
  60. 60:         if ($time instanceof DateTime{
  61. 61:             return clone $time;
  62. 62:  
  63. 63:         elseif (is_numeric($time)) {
  64. 64:             if ($time <= self::YEAR{
  65. 65:                 $time += time();
  66. 66:             }
  67. 67:             return new DateTime53(date('Y-m-d H:i:s'$time));
  68. 68:  
  69. 69:         else // textual or NULL
  70. 70:             return new DateTime53($time);
  71. 71:         }
  72. 72:     }
  73. 73:  
  74. 74:  
  75. 75:  
  76. 76:     /**
  77. 77:      * Gets the boolean value of a configuration option.
  78. 78:      * @param  string  configuration option name
  79. 79:      * @return bool 
  80. 80:      */
  81. 81:     public static function iniFlag($var)
  82. 82:     {
  83. 83:         $status strtolower(ini_get($var));
  84. 84:         return $status === 'on' || $status === 'true' || $status === 'yes' || $status 256;
  85. 85:     }
  86. 86:  
  87. 87:  
  88. 88:  
  89. 89:     /**
  90. 90:      * Initializes variable with $default value.
  91. 91:      *
  92. 92:      * @param  mixed  variable
  93. 93:      * @param  mixed  default value
  94. 94:      * @return void 
  95. 95:      */
  96. 96:     public static function defaultize(&$var$default)
  97. 97:     {
  98. 98:         if ($var === NULL$var $default;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Recursive glob(). Finds pathnames matching a pattern.
  105. 105:      * @param  string 
  106. 106:      * @param  int 
  107. 107:      * @return array 
  108. 108:      */
  109. 109:     public static function glob($pattern$flags 0)
  110. 110:     {
  111. 111:         // TODO: replace by RecursiveDirectoryIterator
  112. 112:         $files glob($pattern$flags);
  113. 113:         if (!is_array($files)) {
  114. 114:             $files array();
  115. 115:         }
  116. 116:  
  117. 117:         $dirs glob(dirname($pattern'/*'$flags GLOB_ONLYDIR);
  118. 118:         if (is_array($dirs)) {
  119. 119:             $mask basename($pattern);
  120. 120:             foreach ($dirs as $dir{
  121. 121:                 $files array_merge($filesself::glob($dir '/' $mask$flags));
  122. 122:             }
  123. 123:         }
  124. 124:  
  125. 125:         return $files;
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /********************* errors and warnings catching ****************d*g**/
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     /** @var string */
  135. 135:     private static $errorMsg;
  136. 136:  
  137. 137:  
  138. 138:  
  139. 139:     /**
  140. 140:      * Starts catching potential errors/warnings.
  141. 141:      *
  142. 142:      * @return void 
  143. 143:      */
  144. 144:     public static function tryError($level E_ALL)
  145. 145:     {
  146. 146:         set_error_handler(array(__CLASS__'_errorHandler')$level);
  147. 147:         self::$errorMsg NULL;
  148. 148:     }
  149. 149:  
  150. 150:  
  151. 151:  
  152. 152:     /**
  153. 153:      * Returns catched error/warning message.
  154. 154:      *
  155. 155:      * @param  string  catched message
  156. 156:      * @return bool 
  157. 157:      */
  158. 158:     public static function catchError($message)
  159. 159:     {
  160. 160:         restore_error_handler();
  161. 161:         $message self::$errorMsg;
  162. 162:         self::$errorMsg NULL;
  163. 163:         return $message !== NULL;
  164. 164:     }
  165. 165:  
  166. 166:  
  167. 167:  
  168. 168:     /**
  169. 169:      * Internal error handler. Do not call directly.
  170. 170:      * @ignore internal
  171. 171:      */
  172. 172:     public static function _errorHandler($code$message)
  173. 173:     {
  174. 174:         if (ini_get('html_errors')) {
  175. 175:             $message html_entity_decode(strip_tags($message)ENT_QUOTES'UTF-8');
  176. 176:         }
  177. 177:  
  178. 178:         if (($a strpos($message': ')) !== FALSE{
  179. 179:             $message substr($message$a 2);
  180. 180:         }
  181. 181:  
  182. 182:         self::$errorMsg $message;
  183. 183:     }
  184. 184: