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:      * Recursive glob(). Finds pathnames matching a pattern.
  90. 90:      * @param  string 
  91. 91:      * @param  int 
  92. 92:      * @return array 
  93. 93:      */
  94. 94:     public static function glob($pattern$flags 0)
  95. 95:     {
  96. 96:         // TODO: replace by RecursiveDirectoryIterator
  97. 97:         $files glob($pattern$flags);
  98. 98:         if (!is_array($files)) {
  99. 99:             $files array();
  100. 100:         }
  101. 101:  
  102. 102:         $dirs glob(dirname($pattern'/*'$flags GLOB_ONLYDIR);
  103. 103:         if (is_array($dirs)) {
  104. 104:             $mask basename($pattern);
  105. 105:             foreach ($dirs as $dir{
  106. 106:                 $files array_merge($filesself::glob($dir '/' $mask$flags));
  107. 107:             }
  108. 108:         }
  109. 109:  
  110. 110:         return $files;
  111. 111:     }
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /********************* errors and warnings catching ****************d*g**/
  116. 116:  
  117. 117:  
  118. 118:  
  119. 119:     /** @var string */
  120. 120:     private static $errorMsg;
  121. 121:  
  122. 122:  
  123. 123:  
  124. 124:     /**
  125. 125:      * Starts catching potential errors/warnings.
  126. 126:      *
  127. 127:      * @return void 
  128. 128:      */
  129. 129:     public static function tryError($level E_ALL)
  130. 130:     {
  131. 131:         set_error_handler(array(__CLASS__'_errorHandler')$level);
  132. 132:         self::$errorMsg NULL;
  133. 133:     }
  134. 134:  
  135. 135:  
  136. 136:  
  137. 137:     /**
  138. 138:      * Returns catched error/warning message.
  139. 139:      *
  140. 140:      * @param  string  catched message
  141. 141:      * @return bool 
  142. 142:      */
  143. 143:     public static function catchError($message)
  144. 144:     {
  145. 145:         restore_error_handler();
  146. 146:         $message self::$errorMsg;
  147. 147:         self::$errorMsg NULL;
  148. 148:         return $message !== NULL;
  149. 149:     }
  150. 150:  
  151. 151:  
  152. 152:  
  153. 153:     /**
  154. 154:      * Internal error handler. Do not call directly.
  155. 155:      * @ignore internal
  156. 156:      */
  157. 157:     public static function _errorHandler($code$message)
  158. 158:     {
  159. 159:         if (ini_get('html_errors')) {
  160. 160:             $message strip_tags($message);
  161. 161:             $message html_entity_decode($message);
  162. 162:         }
  163. 163:  
  164. 164:         if (($a strpos($message': ')) !== FALSE{
  165. 165:             $message substr($message$a 2);
  166. 166:         }
  167. 167:  
  168. 168:         self::$errorMsg $message;
  169. 169:     }
  170. 170: