forked from Anahkiasen/flatten
/
Flatten.php
executable file
·210 lines (180 loc) · 5.39 KB
/
Flatten.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace Flatten;
use Illuminate\Container\Container;
use Symfony\Component\HttpFoundation\Response;
/**
* Handles the rendering of responses and starting of events.
*/
class Flatten
{
/**
* The IoC Container.
*
* @var Container
*/
protected $app;
/**
* Setup Flatten and hook it to the application.
*
* @param Container $app
*/
public function __construct(Container $app)
{
$this->app = $app;
}
/**
* Delegate flushing actions to CacheHandler.
*
* @param string $method
* @param array $arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
$class = $this;
// Go through the classes Flatten decorates
$decorators = ['cache', 'templating'];
foreach ($decorators as $decorator) {
$decorator = $this->app['flatten.'.$decorator];
if (method_exists($decorator, $method)) {
$class = $decorator;
break;
}
}
return call_user_func_array([$class, $method], $arguments);
}
////////////////////////////////////////////////////////////////////
///////////////////////// CACHING PROCESS //////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Starts the caching system.
*
* @return bool
* @codeCoverageIgnore
*/
public function start()
{
return $this->app['flatten.events']->onApplicationBoot();
}
/**
* Stops the caching system.
*
* @param \Illuminate\Http\Response|null $response A response to render on end
*
* @return bool
* @codeCoverageIgnore
*/
public function end($response = null)
{
if ($this->app['flatten.context']->shouldRun()) {
return $this->app['flatten.events']->onApplicationDone($response);
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////// KICKSTART ///////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Kickstart a raw version of Flatten.
*
* @return string|void
*/
public static function kickstart()
{
$filename = static::getKickstartPath(func_get_args());
// If we have a cache for it, unserialize it and output it
if ($filename && file_exists($filename)) {
$contents = file_get_contents($filename);
exit(unserialize(substr($contents, 10)));
}
}
/**
* Get the path to the current page for kickstart.
*
* @param array $salts
*
* @return string|null
*/
public static function getKickstartPath($salts = [])
{
// Get the salts
$salts = $salts ? implode('-', $salts) : null;
// Get storage path
$possible = [
__DIR__.'/../../../../storage/framework/cache',
__DIR__.'/../../storage/cache',
__DIR__.'/../cache',
];
$storage = null;
foreach ($possible as $path) {
if (is_dir($path)) {
$storage = realpath($path);
break;
}
}
if ($storage && isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_METHOD'] === 'GET') {
// Compute cache path
$query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$key = $salts.'GET-'.$_SERVER['REQUEST_URI'];
$key = $query ? $key.'?'.$query : $key;
// Hash and get path
$parts = array_slice(str_split($hash = md5($key), 2), 0, 2);
$filename = $storage.'/'.implode('/', $parts).'/'.$hash;
return $filename;
}
return;
}
////////////////////////////////////////////////////////////////////
////////////////////////////// RENDERING ///////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Create a response to send from content.
*
* @param string|null $content
*
* @return Response
*/
public function getResponse($content = null)
{
// If no content, get from cache
if (!$content) {
$content = $this->app['flatten.cache']->getCache();
}
return new Response($content);
}
/**
* Render a content.
*
* @param string|null $content A content to render
*
* @codeCoverageIgnore
*/
public function render($content = null)
{
$this->getResponse($content)->send();
exit;
}
////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPERS ////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Get the current page's hash.
*
* @param string|null $page
*
* @return string A page hash
*/
public function computeHash($page = null)
{
// Get current page URI
if (!$page) {
$page = $this->app['flatten.context']->getCurrentUrl();
}
// Add additional salts
$salts = $this->app['config']->get('flatten.saltshaker');
// Add method and page
$salts[] = $this->app['request']->getMethod();
$salts[] = $page;
return implode('-', $salts);
}
}