-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperStaticCache.php
More file actions
142 lines (117 loc) · 3.93 KB
/
SuperStaticCache.php
File metadata and controls
142 lines (117 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/***
* Class SuperStaticCache
* This class is a component that caches the whole php output into files based on the URL string.
* Please use it only if you don't have any dynamic content like user login.
*/
class SuperStaticCache extends CApplicationComponent
{
const SSC_DEFAULT_CACHE = '/runtime/superstaticcache/';
public $enabled;
public $sURL;
public $duration;
public $htmlContent;
public $newFile;
public $cacheHomepage;
public $controllers = array(); //Should be in the following format -> ('controllerName')
private $_fileName;
public function init()
{
$this->_composeFileName();
}
public static function startCache()
{
if(!Yii::app()->superStaticCache->enabled)
return false;
//Check if actions are configured
if(!self::checkActions())
return false;
$dirname = dirname(__FILE__);
//Check if folder exists and create it
if(!file_exists($dirname . "/.." . self::SSC_DEFAULT_CACHE))
{
mkdir($dirname . "/.." . self::SSC_DEFAULT_CACHE, 0777);
}
if(file_exists(Yii::app()->superStaticCache->_getFileName()) && !is_dir(Yii::app()->superStaticCache->_getFileName()))
{
//Open the file
$f = Yii::app()->superStaticCache->_openFileToRead();
//Check if the time has expired
$timeLine = fgets($f);
//Get the time without the duration
$time = strtotime(str_replace(array("<!-- ", " -->"), "", $timeLine)) + Yii::app()->superStaticCache->duration;
$timeNow = strtotime('now');
//Compare the time to now and see if its expired
if($time < $timeNow)
{
//Then we need to create a new file
Yii::app()->superStaticCache->newFile = true;
ob_start();
}
else
{
require Yii::app()->superStaticCache->_getFileName();
Yii::app()->end();
}
}
else
{
//Create the new cache file
Yii::app()->superStaticCache->newFile = true;
ob_start();
}
}
public static function endCache()
{
if(!Yii::app()->superStaticCache->enabled)
return false;
if(Yii::app()->superStaticCache->newFile)
{
$htmlContent = "<!-- " . date('Y-m-d H:i:s') . " -->" . PHP_EOL . ob_get_contents();
ob_end_clean();
$f = Yii::app()->superStaticCache->_openFileToWrite();
fwrite($f, $htmlContent);
fclose($f);
echo $htmlContent;
}
}
private function _getFileName()
{
return $this->_fileName;
}
private function _composeFileName()
{
$this->_fileName = dirname(__FILE__) . "/.." . self::SSC_DEFAULT_CACHE . $_SERVER['HTTP_HOST'] . preg_replace('/[^A-Za-z0-9 _ .-]/', '', $_SERVER['REQUEST_URI']) . ".html";
}
private static function _getTimeAsNumber()
{
return intval(date('YmdHis'));
}
private static function _openFileToRead()
{
return fopen(Yii::app()->superStaticCache->_getFileName(), 'r');
}
private static function _openFileToWrite()
{
//This cleans up the file and we can refill it
return fopen(Yii::app()->superStaticCache->_getFileName(), 'w+');
}
public static function checkActions()
{
if(empty(Yii::app()->superStaticCache->controllers))
return true;
$arr = Yii::app()->superStaticCache->controllers;
//Cache the homepage
if(Yii::app()->superStaticCache->cacheHomepage === true && $_SERVER['REQUEST_URI'] == '/')
return true;
else
{
foreach($arr as $v)
{
if(strpos($_SERVER['REQUEST_URI'], $v))
return true;
}
}
return false;
}
}