Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
156 lines (140 sloc) 4.82 KB
<?php
/**
* Created by PhpStorm.
* User: vincent.foulon
* Date: 07/09/2017
* Time: 09:58
*/
namespace VFou\CmsBundle\Service;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Cache\Simple\FilesystemCache;
use VFou\CmsBundle\Entity\Page;
use VFou\CmsBundle\Entity\PageType;
class CacheService
{
/** No HTML caching at all */
const NO_CACHE = 0;
/** Volatile HTML Caching : every edition on the admin panel will clear it */
const TEMPORARY = 1;
/** Permanent HTML Caching : it'll be cleared only if the current page is edited */
const PERMANENT = 2;
/** Children Dependence is like temporary caching but it'll be cleared only if himself or a direct children is edited. */
const CHILDREN_DEPENDENCE = 3;
/** "Custom" means that it'll clear normally the cache but it'll additionnaly call the function "clearCache" of the page type.
* Useful when you manage routing on your page type. You'll have to override the function "clearCache" on your type.
*/
const CUSTOM_TEMPORARY = -1;
const CUSTOM_PERMANENT = -2;
const CUSTOM_CHILDREN_DEPENDENCE = -3;
/** @var ConfigurationService */
private $config;
/** @var FilesystemCache */
private $cache;
/** @var EntityManagerInterface */
private $em;
/**
* CacheService constructor.
* @param ConfigurationService $config
* @param EntityManagerInterface $em
*/
public function __construct(ConfigurationService $config, EntityManagerInterface $em)
{
$this->em = $em;
$this->cache = new FilesystemCache("midnightcms",0,$GLOBALS['kernel']->getCacheDir());
$this->config = $config;
}
/**
* Clears cache for one or multiple page
* @param array|Page $pages
*/
public function clearCache($pages){
if(!is_array($pages)){
$pages = [$pages];
}
foreach($pages as $page){
foreach($this->config->getDomain("type") as $type => $cacheLvl){
$cacheLvl = abs($cacheLvl);
if($cacheLvl == self::NO_CACHE) continue;
$type = $this->em->getRepository(PageType::class)->findOneByTitle(str_replace("type.","",$type));
$pagesToClear = $this->em->getRepository(Page::class)->findByType($type);
switch($cacheLvl){
case self::TEMPORARY:
foreach($pagesToClear as $pageToClear){
$this->clearPage($pageToClear);
}
break;
case self::CHILDREN_DEPENDENCE:
foreach($pagesToClear as $p){
if(in_array($page, $p->getSubPages()->toArray())){
$this->clearPage($p);
}
}
break;
}
$uri = $this->config->get("URIs","uri.".$page->getRootPage()->getId());
$this->removeUrl($uri, $page->getLink());
}
}
}
/**
* Clears all subpages in any depth of the given page
* @param Page $page
* @param int $depth
*/
public function clearCacheCascade(Page $page, $depth = -1){
if ($depth == 0) return;
$depth--;
foreach($page->getSubpages() as $p){
$this->clearPage($p);
$this->clearCacheCascade($p, $depth);
}
}
/**
* @param $page
*/
private function clearPage(Page $page)
{
if ($page->getType()->getCacheLevel() < 0) {
$page->getType()->clearCache($this, $page);
}
$this->removeUrl("", $page->getLink());
}
public function clearAll(){
return $this->cache->clear();
}
/**
* @param $uri
* @param $path
*/
public function removeUrl($uri, $path){
$path = str_replace("/","|",$path);
if(substr($path,-1) == "|") $path = substr($path, 0, -1);
if($path == "") $path = "|";
if(empty($uri)){
$cachedUris = $this->cache->get("URIs") ?? "N;";
$cachedUris = unserialize($cachedUris);
if($cachedUris === null){
return;
}
foreach($cachedUris as $uri){
$this->cache->delete("path.".$uri."|".$path);
$this->cache->delete("display.".$uri."|".$path);
}
} else {
$this->cache->delete("path.".$uri."|".$path);
$this->cache->delete("display.".$uri."|".$path);
}
}
public function get($key){
return $this->cache->get($key);
}
public function set($key, $value){
return $this->cache->set($key, $value);
}
public function has($key){
return $this->cache->has($key);
}
public function delete($key){
return $this->cache->delete($key);
}
}
You can’t perform that action at this time.