Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add args to filename #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 120 additions & 10 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,25 @@ public function cache(Request $request, Response $response)
{
list($path, $file) = $this->getDirectoryAndFileNames($request, $response);

$this->files->makeDirectory($path, 0775, true, true);
$fileJoined = $this->join([$path, $file]);

$this->files->put(
$this->join([$path, $file]),
$response->getContent(),
true
);
@mkdir(dirname($path.'/'.$file), 0777, true);

// Create the file handle. We use the "c" mode which will avoid writing an
// empty file if we abort when checking the lock status in the next step.
$handle = fopen($path.'/'.$file, 'c');


fwrite($handle, $response->getContent());
chmod($path.'/'.$file, 0777);

// Hold the file lock for a moment to prevent other processes from trying to write the same file.
sleep(0);

fclose($handle);

\App\Models\CacheIndex::create([
'path' => $this->join([$path, $file]),
'path' => $fileJoined,
'page_type' => $this->pageType,
'expire_at' => \Carbon\Carbon::now()->addMinutes($this->expireAt),
]);
Expand Down Expand Up @@ -226,13 +235,63 @@ public function clear($path = null)
protected function getDirectoryAndFileNames($request, $response)
{
$segments = explode('/', ltrim($request->getPathInfo(), '/'));

$fullUrl = config('app/url').$_SERVER['REQUEST_URI'];
$filename = $this->aliasFilename(array_pop($segments));
$extension = $this->guessFileExtension($response);
$urlParts = parse_url($fullUrl);
$query = $this->arrGet($urlParts, 'query', '');

$file = "{$filename}.{$extension}";
if (isset($query[0]['query']) && $this->filterQuery($query[0]['query']) !== null) {
$basename = '_' . $this->filterQuery($query[0]['query']) . '.html';
return [$this->getCachePath(implode('/',$segments )), $basename];
}

return [$this->getCachePath(implode('/', $segments)), $file];
$basename = "{$filename}.{$extension}";
return [$this->getCachePath(implode('/',$segments )), $basename];
}

/**
* Check if the filter is allowed and accept only the first 2 query's
* @param $query
* @return string|null
*/
private function filterQuery($query)
{
if (config('cache-whitelist.tags') === null) {
return null;
}

if (strpos($query, '&') === false) {
$query = explode('=', $query);
if (!in_array($query[0], config('cache-whitelist.tags'))) {
return null;
}
return $query[0].'='.$query[1];
}

$query = explode('&', $query);
$queryPartOne = explode('=', $query[0]);
$queryPartTow = explode('=', $query[1]);
$combineQuery = '';

if (!in_array($queryPartOne[0], config('cache-whitelist.tags')) &&
!in_array($queryPartTow[0], config('cache-whitelist.tags'))) {
return null;
}

if (in_array($queryPartOne[0], config('cache-whitelist.tags'))) {
$combineQuery .= $queryPartOne[0].'='.$queryPartOne[1];
}
if (in_array($queryPartTow[0], config('cache-whitelist.tags'))) {
if ($combineQuery === '') {
$combineQuery .= $queryPartTow[0] . '=' . $queryPartTow[1];
}
else {
$combineQuery .= '&' . $queryPartTow[0] . '=' . $queryPartTow[1];
}
}

return $combineQuery;
}

/**
Expand Down Expand Up @@ -348,4 +407,55 @@ public function getPageType()
return $this->pageType;
}

/**
* @param mixed $content
* @return string
*/
protected function normalizeContent($content)
{
if ($content instanceof Response) {
$content = $content->content();
}

return $content;
}

public function cacheUrl($key, $url)
{
$this->cacheDomain();

$urls = $this->getUrls();

$url = Str::removeLeft($url, $this->getBaseUrl());

$urls->put($key, $url);

$this->cache->forever($this->getUrlsCacheKey(), $urls->all());
}

/**
* Get a hashed string representation of a URL.
*
* @param string $url
* @return string
*/
protected function makeHash($url)
{
return md5($url);
}

private function isBasenameTooLong($basename)
{
return strlen($basename) > 255;
}

private function arrGet($array, $key, $default = null)
{
if ($key) {
$key = str_replace(':', '.', $key);
}

return [$array, $key, $default];
}

}