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