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