source-class-Com.Tecnick.File.Cache

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Cache.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     File
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filecache
 12:  *
 13:  * This file is part of tc-lib-pdf-filecache software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\File;
 17: 
 18: use \Com\Tecnick\File\File;
 19: 
 20: /**
 21:  * Com\Tecnick\Pdf\File\Cache
 22:  *
 23:  * @since       2011-05-23
 24:  * @category    Library
 25:  * @package     File
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filecache
 30:  */
 31: class Cache
 32: {
 33:     /**
 34:      * Cache path
 35:      *
 36:      * @var string
 37:      */
 38:     protected static $path;
 39: 
 40:     /**
 41:      * File prefix
 42:      *
 43:      * @var string
 44:      */
 45:     protected static $prefix;
 46: 
 47:     /**
 48:      * Set the file prefix (common name)
 49:      *
 50:      * @param string $prefix Common prefix to be used for all cache files
 51:      */
 52:     public function __construct($prefix = null)
 53:     {
 54:         $this->defineSystemCachePath();
 55:         $this->setCachePath();
 56:         if ($prefix === null) {
 57:             $prefix = rtrim(base64_encode(pack('H*', md5(uniqid(rand(), true)))), '=');
 58:         }
 59:         self::$prefix = '_'.preg_replace('/[^a-zA-Z0-9_\-]/', '', strtr($prefix, '+/', '-_')).'_';
 60:     }
 61: 
 62:     /**
 63:      * Get the cache directory path
 64:      *
 65:      * @return string
 66:      */
 67:     public function getCachePath()
 68:     {
 69:         return self::$path;
 70:     }
 71: 
 72:     /**
 73:      * Set the default cache directory path
 74:      *
 75:      * @param string|null $path Cache directory path; if null use the K_PATH_CACHE value
 76:      */
 77:     public function setCachePath($path = null)
 78:     {
 79:         if (($path === null) || !is_writable($path)) {
 80:             self::$path = K_PATH_CACHE;
 81:         } else {
 82:             self::$path = $this->normalizePath($path);
 83:         }
 84:     }
 85: 
 86:     /**
 87:      * Get the file prefix
 88:      *
 89:      * @return string
 90:      */
 91:     public function getFilePrefix()
 92:     {
 93:         return self::$prefix;
 94:     }
 95: 
 96:     /**
 97:      * Returns a temporary filename for caching files
 98:      *
 99:      * @param string $type Type of file
100:      * @param string $key  File key (used to retrieve file from cache)
101:      *
102:      * @return string filename
103:      */
104:     public function getNewFileName($type = 'tmp', $key = '0')
105:     {
106:         return tempnam(self::$path, self::$prefix.$type.'_'.$key.'_');
107:     }
108: 
109:     /**
110:      * Delete cached files
111:      *
112:      * @param string $type Type of files to delete
113:      * @param string $key  Specific file key to delete
114:      *
115:      */
116:     public function delete($type = null, $key = null)
117:     {
118:         $path = self::$path.self::$prefix;
119:         if ($type !== null) {
120:             $path .= $type.'_';
121:             if ($key !== null) {
122:                 $path .= $key.'_';
123:             }
124:         }
125:         $path .= '*';
126:         $files = glob($path);
127:         if (!empty($files)) {
128:             array_map('unlink', $files);
129:         }
130:     }
131: 
132:     /**
133:      * Set the K_PATH_CACHE constant (if not set) to the default system directory for temporary files
134:      */
135:     protected function defineSystemCachePath()
136:     {
137:         if (defined('K_PATH_CACHE')) {
138:             return;
139:         }
140:         $K_PATH_CACHE = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
141:         define('K_PATH_CACHE', $this->normalizePath($K_PATH_CACHE));
142:     }
143: 
144:     /**
145:      * Normalize cache path
146:      *
147:      * @param string $path Path to normalize
148:      *
149:      * @return string
150:      */
151:     protected function normalizePath($path)
152:     {
153:         $path = realpath($path);
154:         if (substr($path, -1) != '/') {
155:             $path .= '/';
156:         }
157:         return $path;
158:     }
159: }
160: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy