Ubiquity  2.0.0
php rapid development framework
CacheTrait.php
Go to the documentation of this file.
1 <?php
3 
4 use Ajax\JsUtils;
10 
15 class CacheTrait{
16  abstract public function _getAdminData();
17  abstract public function _getAdminViewer();
18  abstract public function _getAdminFiles();
19 
20  public function setCacheTypes(){
21  if(isset($_POST["cacheTypes"]))
22  $caches=$_POST["cacheTypes"];
23  else
24  $caches=[];
25  $cacheFiles=[];
26  foreach ($caches as $cache){
27  if($cache=='models' || $cache=='controllers'){
28  $cacheFiles=\array_merge($cacheFiles,CacheManager::$cache->getCacheFiles($cache));
29  }else{
30  $cacheFiles=\array_merge($cacheFiles,CacheFile::initFromFiles(ROOT . DS .CacheManager::getCacheDirectory().$cache, \ucfirst($cache)));
31  }
32  }
33  $dt=$this->_getAdminViewer()->getCacheDataTable($cacheFiles);
34  echo $dt->refresh();
35  echo $this->jquery->compile($this->view);
36  }
37 
38  public function deleteCacheFile(){
39  if(isset($_POST["toDelete"])){
40  $toDelete=$_POST["toDelete"];
41  $type=\strtolower($_POST["type"]);
42  if($type=='models' || $type=='controllers'){
43  CacheManager::$cache->remove($toDelete);
44  }else{
45  if(\file_exists($toDelete))
46  \unlink($toDelete);
47  }
48  }
49  $this->setCacheTypes();
50  }
51 
52  public function deleteAllCacheFiles(){
53  if(isset($_POST["type"])){
54  \session_destroy();
55  $toDelete=\strtolower($_POST["type"]);
56  if($toDelete=='models' || $toDelete=='controllers'){
57  CacheManager::$cache->clearCache($toDelete);
58  }else{
59  CacheFile::delete(ROOT . DS .CacheManager::getCacheDirectory().\strtolower($toDelete));
60  }
61  }
62  $this->setCacheTypes();
63  }
64 
65  public function _showFileContent(){
66  if(URequest::isPost()){
67  $type=\strtolower($_POST["type"]);$filename=$_POST["filename"];$key=$_POST["key"];
68  if($type=='models' || $type=='controllers'){
69  $content=CacheManager::$cache->file_get_contents($key);
70  }else{
71  if(\file_exists($filename)){
72  $content=\file_get_contents($filename);
73  }
74  }
75  $modal=$this->jquery->semantic()->htmlModal("file",$type." : ".\basename($filename));
76  $frm=new HtmlForm("frmShowFileContent");
77  $frm->addTextarea("file-content", null,$content,"",10);
78  $modal->setContent($frm);
79  $modal->addAction("Close");
80  $this->jquery->exec("$('#file').modal('show');",true);
81  echo $modal;
82  echo $this->jquery->compile($this->view);
83  }
84  }
85 
86  public function initCacheType(){
87  if(isset($_POST["type"])){
88  $type=$_POST["type"];
89  $config=Startup::getConfig();
90  switch ($type){
91  case "Models":
92  CacheManager::initCache($config,"models");
93  break;
94  case "Controllers":
95  CacheManager::initCache($config,"controllers");
96  break;
97  }
98 
99  }
100  $this->setCacheTypes();
101  }
102 
103  public function _initModelsCache(){
104  $config=Startup::getConfig();
105  \ob_start();
106  CacheManager::initCache($config,"models");
107  \ob_end_clean();
108  $this->models();
109  }
110 }
static isPost()
Returns true if the request is sent by the POST method.
Definition: URequest.php:91
static initFromFiles($folder, $type, $keyFunction=null)
Definition: CacheFile.php:22
static initCache(&$config, $type="all", $silent=false)