Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • ArrayTools
  • Callback
  • Component
  • ComponentContainer
  • Configurator
  • DateTime
  • Debug
  • Environment
  • Framework
  • FreezableObject
  • GenericRecursiveIterator
  • Image
  • ImageMagick
  • InstanceFilterIterator
  • Object
  • ObjectMixin
  • Paginator
  • RecursiveComponentIterator
  • ServiceLocator
  • SmartCachingIterator
  • String
  • Tools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • AmbiguousServiceException
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Tools library.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: final class Tools
 24: {
 25:     /** minute in seconds */
 26:     const MINUTE = 60;
 27: 
 28:     /** hour in seconds */
 29:     const HOUR = 3600;
 30: 
 31:     /** day in seconds */
 32:     const DAY = 86400;
 33: 
 34:     /** week in seconds */
 35:     const WEEK = 604800;
 36: 
 37:     /** average month in seconds */
 38:     const MONTH = 2629800;
 39: 
 40:     /** average year in seconds */
 41:     const YEAR = 31557600;
 42: 
 43: 
 44: 
 45:     /**
 46:      * Static class - cannot be instantiated.
 47:      */
 48:     final public function __construct()
 49:     {
 50:         throw new \LogicException("Cannot instantiate static class " . get_class($this));
 51:     }
 52: 
 53: 
 54: 
 55:     /**
 56:      * DateTime object factory.
 57:      * @param  string|int|DateTime
 58:      * @return DateTime
 59:      */
 60:     public static function createDateTime($time)
 61:     {
 62:         if ($time instanceof \DateTime) {
 63:             return clone $time;
 64: 
 65:         } elseif (is_numeric($time)) {
 66:             if ($time <= self::YEAR) {
 67:                 $time += time();
 68:             }
 69:             return new \DateTime(date('Y-m-d H:i:s', $time));
 70: 
 71:         } else { // textual or NULL
 72:             return new \DateTime($time);
 73:         }
 74:     }
 75: 
 76: 
 77: 
 78:     /**
 79:      * Gets the boolean value of a configuration option.
 80:      * @param  string  configuration option name
 81:      * @return bool
 82:      */
 83:     public static function iniFlag($var)
 84:     {
 85:         $status = strtolower(ini_get($var));
 86:         return $status === 'on' || $status === 'true' || $status === 'yes' || $status % 256;
 87:     }
 88: 
 89: 
 90: 
 91:     /**
 92:      * Initializes variable with $default value.
 93:      * @param  mixed  variable
 94:      * @param  mixed  default value
 95:      * @return void
 96:      */
 97:     public static function defaultize(&$var, $default)
 98:     {
 99:         if ($var === NULL) $var = $default;
100:     }
101: 
102: 
103: 
104:     /**
105:      * Recursive glob(). Finds pathnames matching a pattern.
106:      * @param  string
107:      * @param  int
108:      * @return array
109:      */
110:     public static function glob($pattern, $flags = 0)
111:     {
112:         // TODO: replace by RecursiveDirectoryIterator
113:         $files = glob($pattern, $flags);
114:         if (!is_array($files)) {
115:             $files = array();
116:         }
117: 
118:         $dirs = glob(dirname($pattern) . '/*', $flags | GLOB_ONLYDIR);
119:         if (is_array($dirs)) {
120:             $mask = basename($pattern);
121:             foreach ($dirs as $dir) {
122:                 $files = array_merge($files, self::glob($dir . '/' . $mask, $flags));
123:             }
124:         }
125: 
126:         return $files;
127:     }
128: 
129: 
130: 
131:     /**
132:      * Returns the MIME content type of file.
133:      * @param  string
134:      * @return string
135:      */
136:     public static function detectMimeType($file)
137:     {
138:         if (!is_file($file)) {
139:             throw new \FileNotFoundException("File '$file' not found.");
140:         }
141: 
142:         $info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
143:         if (isset($info['mime'])) {
144:             return $info['mime'];
145: 
146:         } elseif (extension_loaded('fileinfo')) {
147:             $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
148: 
149:         } elseif (function_exists('mime_content_type')) {
150:             $type = mime_content_type($file);
151:         }
152: 
153:         return isset($type) && preg_match('#^\S+/\S+$#', $type) ? $type : 'application/octet-stream';
154:     }
155: 
156: 
157: 
158:     /********************* errors and warnings catching ****************d*g**/
159: 
160: 
161: 
162:     /** @var string */
163:     private static $errorMsg;
164: 
165: 
166: 
167:     /**
168:      * Starts catching potential errors/warnings.
169:      * @return void
170:      */
171:     public static function tryError($level = E_ALL)
172:     {
173:         set_error_handler(array(__CLASS__, '_errorHandler'), $level);
174:         self::$errorMsg = NULL;
175:     }
176: 
177: 
178: 
179:     /**
180:      * Returns catched error/warning message.
181:      * @param  string  catched message
182:      * @return bool
183:      */
184:     public static function catchError(& $message)
185:     {
186:         restore_error_handler();
187:         $message = self::$errorMsg;
188:         self::$errorMsg = NULL;
189:         return $message !== NULL;
190:     }
191: 
192: 
193: 
194:     /**
195:      * Internal error handler. Do not call directly.
196:      * @internal
197:      */
198:     public static function _errorHandler($severity, $message)
199:     {
200:         if (($severity & error_reporting()) !== $severity) {
201:             return NULL;
202:         }
203: 
204:         if (ini_get('html_errors')) {
205:             $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
206:         }
207: 
208:         if (($a = strpos($message, ': ')) !== FALSE) {
209:             $message = substr($message, $a + 2);
210:         }
211: 
212:         self::$errorMsg = $message;
213:     }
214: 
215: }
216: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0