Packages

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

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

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