Passed
Push — master ( 9c6b4e...07b0df )
by judicael
03:29
created

bundles/lib/Cache/File.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Manage Cache by file
5
 *
6
 * @category  	lib
7
 * @package		lib\Cache
8
 * @author    	Judicaël Paquet <[email protected]>
9
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
10
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
11
 * @version   	Release: 1.0.0
12
 * @filesource	https://github.com/las93/venus2
13
 * @link      	https://github.com/las93
14
 * @since     	1.0
15
 */
16
namespace Venus\lib\Cache;
17
18
/**
19
 * This class manage the Cache by file
20
 *
21
 * @category  	lib
22
 * @package		lib\Cache
23
 * @author    	Judicaël Paquet <[email protected]>
24
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
25
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
26
 * @version   	Release: 1.0.0
27
 * @filesource	https://github.com/las93/venus2
28
 * @link      	https://github.com/las93
29
 * @since     	1.0
30
 */
31
class File implements CacheInterface
32
{
33
    /**
34
     * var containe this folder of cache
35
     *
36
     * @access private
37
     * @var    string
38
     */
39
    private $_sFolder = '';
40
41
    /**
42
     * constructor
43
     *
44
     * @access public
45
     * @return \Venus\lib\Cache\File
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
     */
47
    public function __construct()
48
    {
49
        $this->_sFolder = str_replace('bundles'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Cache', CACHE_DIR, __DIR__).DIRECTORY_SEPARATOR;
50
    }
51
52
    /**
53
     * set a value
54
     *
55
     * @access public
56
     * @param  string $sName name of the session
57
     * @param  mixed $mValue value of this sesion var
58
     * @param  int $iFlag flags
59
     * @param  int $iExpire expiration of cache
60
     * @return \Venus\lib\Cache\File
61
     */
62
    public function set(string $sName, $mValue, int $iFlag, int $iExpire)
63
    {
64
        file_put_contents($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac', serialize($mValue));
65
        return $this;
66
    }
67
68
    /**
69
     * get a value
70
     *
71
     * @access public
72
     * @param  string $sName name of the session
73
     * @param  int $iFlags flags
74
     * @param  int $iTimeout expiration of cache
75
     * @return mixed
76
     */
77
    public function get(string $sName, int &$iFlags = null, int $iTimeout = 0)
78
    {
79
        if ($iTimeout > 0 && file_exists($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac')
80
            && time() - filemtime($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac') > $iTimeout) {
81
82
            unlink($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac');
83
        }
84
85
        if (file_exists($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac')) {
86
87
            return unserialize(file_get_contents($this->_sFolder . $this->_getSubDirectory($sName) . md5($sName) . '.fil.cac'));
88
        } else {
89
90
            return false;
91
        }
92
    }
93
94
    /**
95
     * delete a value
96
     *
97
     * @access public
98
     * @param  string $sName name of the session
99
     * @return mixed
100
     */
101
    public function delete(string $sName)
102
    {
103
        return unlink($this->_sFolder.$this->_getSubDirectory($sName).md5($sName).'.fil.cac');
104
    }
105
106
    /**
107
     * flush the cache
108
     *
109
     * @access public
110
     * @param  string $sName name of the session
111
     * @return mixed
112
     */
113
    public function flush()
114
    {
115
        $this->_removeDirectory($this->_sFolder);
116
    }
117
118
    /**
119
     *
120
     *
121
     * @access public
122
     * @param  string $sName name of the session
123
     * @return mixed
124
     */
125
    private function _getSubDirectory($sName)
126
    {
127
        if (!file_exists($this->_sFolder.substr(md5($sName), 0, 2).DIRECTORY_SEPARATOR.substr(md5($sName), 2, 2))) {
128
129
            mkdir($this->_sFolder.substr(md5($sName), 0, 2).DIRECTORY_SEPARATOR.substr(md5($sName), 2, 2), 0777, true);
130
        }
131
132
        return substr(md5($sName), 0, 2).DIRECTORY_SEPARATOR.substr(md5($sName), 2, 2).DIRECTORY_SEPARATOR;
133
    }
134
135
    /**
136
     * remove a directory recursivly
137
     *
138
     * @access private
139
     * @param  string $sName nom du répertoire
140
     * @return void
141
     */
142
    private function _removeDirectory($sName)
143
    {
144
        if ($rDirectory = opendir($sName)) {
145
146
            while (($sFile = readdir($rDirectory)) !== false) {
147
148
                if ($sFile > '0' && filetype($sName.$sFile) == "file") { unlink($sName.$sFile); } elseif ($sFile > '0' && filetype($sName.$sFile) == "dir") { remove_dir($sName.$sFile."\\"); }
149
            }
150
151
            closedir($rDirectory);
152
            rmdir($sName);
153
        }
154
    }
155
}
156