Ubiquity  2.0.2
php rapid development framework
CacheManager.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\cache;
4 
13 
15 
20  public static $cache;
21  private static $cacheDirectory;
22 
23  public static function start(&$config) {
24  self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
25  $cacheDirectory = ROOT . DS . self::$cacheDirectory;
26  Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
27  self::register ( Annotations::getManager () );
28  self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
29  }
30 
36  public static function startProd(&$config) {
37  self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
38  $cacheDirectory = ROOT . DS . self::$cacheDirectory;
39  self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
40  }
41 
42  protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
43  $cacheSystem = 'Ubiquity\cache\system\ArrayCache';
44  $cacheParams = [ ];
45  if (! isset ( self::$cache )) {
46  if (isset ( $config ["cache"] ["system"] )) {
47  $cacheSystem = $config ["cache"] ["system"];
48  }
49  if (isset ( $config ["cache"] ["params"] )) {
50  $cacheParams = $config ["cache"] ["params"];
51  }
52  self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
53  }
54  return self::$cache;
55  }
56 
57  private static function initialGetCacheDirectory(&$config) {
58  $cacheDirectory = @$config ["cache"] ["directory"];
59  if (! isset ( $cacheDirectory )) {
60  $config ["cache"] ["directory"] = "cache/";
61  $cacheDirectory = $config ["cache"] ["directory"];
62  }
63  return $cacheDirectory;
64  }
65 
66  public static function getCacheDirectory() {
67  return self::$cacheDirectory;
68  }
69 
70  public static function checkCache(&$config, $silent = false) {
71  $dirs = self::getCacheDirectories ( $config, $silent );
72  foreach ( $dirs as $dir ) {
73  self::safeMkdir ( $dir );
74  }
75  return $dirs;
76  }
77 
78  public static function getCacheDirectories(&$config, $silent = false) {
79  $cacheDirectory = self::initialGetCacheDirectory ( $config );
80  if (! $silent) {
81  echo "cache directory is " . UFileSystem::cleanPathname ( ROOT . DS . $cacheDirectory ) . "\n";
82  }
83  $modelsDir = str_replace ( "\\", DS, $config ["mvcNS"] ["models"] );
84  $controllersDir = str_replace ( "\\", DS, $config ["mvcNS"] ["controllers"] );
85  $annotationCacheDir = ROOT . DS . $cacheDirectory . DS . "annotations";
86  $modelsCacheDir = ROOT . DS . $cacheDirectory . DS . $modelsDir;
87  $queriesCacheDir = ROOT . DS . $cacheDirectory . DS . "queries";
88  $controllersCacheDir = ROOT . DS . $cacheDirectory . DS . $controllersDir;
89  $viewsCacheDir = ROOT . DS . $cacheDirectory . DS . "views";
90  $seoCacheDir = ROOT . DS . $cacheDirectory . DS . "seo";
91  return [
92  "annotations" => $annotationCacheDir,
93  "models" => $modelsCacheDir,
94  "controllers" => $controllersCacheDir,
95  "queries" => $queriesCacheDir,
96  "views" => $viewsCacheDir,
97  "seo" => $seoCacheDir
98  ];
99  }
100 
101  private static function safeMkdir($dir) {
102  if (! is_dir ( $dir ))
103  return mkdir ( $dir, 0777, true );
104  }
105 
106  public static function clearCache(&$config, $type = "all") {
107  $cacheDirectories = self::checkCache ( $config );
108  $cacheDirs = [
109  "annotations",
110  "controllers",
111  "models",
112  "queries",
113  "views"
114  ];
115  foreach ( $cacheDirs as $typeRef ) {
116  self::_clearCache ( $cacheDirectories, $type, $typeRef );
117  }
118  }
119 
120  private static function _clearCache($cacheDirectories, $type, $typeRef) {
121  if ($type === "all" || $type === $typeRef)
122  UFileSystem::deleteAllFilesFromFolder ( $cacheDirectories [$typeRef] );
123  }
124 
125  public static function initCache(&$config, $type = "all", $silent = false) {
126  self::checkCache ( $config, $silent );
127  self::start ( $config );
128  if ($type === "all" || $type === "models")
129  self::initModelsCache ( $config, false, $silent );
130  if ($type === "all" || $type === "controllers")
131  self::initRouterCache ( $config, $silent );
132  if ($type === "all" || $type === "rest")
133  self::initRestCache ( $config, $silent );
134  }
135 
136  protected static function _getFiles(&$config, $type, $silent = false) {
137  $typeNS = $config ["mvcNS"] [$type];
138  $typeDir = ROOT . DS . str_replace ( "\\", DS, $typeNS );
139  if (! $silent)
140  echo \ucfirst ( $type ) . " directory is " . ROOT . $typeNS . "\n";
141  return UFileSystem::glob_recursive ( $typeDir . DS . '*' );
142  }
143 
144  private static function register(AnnotationManager $annotationManager) {
145  $annotationManager->registry = array_merge ( $annotationManager->registry, [
146  'id' => 'Ubiquity\annotations\IdAnnotation',
147  'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation',
148  'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
149  'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation',
150  'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
151  'table' => 'Ubiquity\annotations\TableAnnotation',
152  'transient' => 'Ubiquity\annotations\TransientAnnotation',
153  'column' => 'Ubiquity\annotations\ColumnAnnotation',
154  'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation',
155  'route' => 'Ubiquity\annotations\router\RouteAnnotation',
156  'var' => 'mindplay\annotations\standard\VarAnnotation',
157  'yuml' => 'Ubiquity\annotations\YumlAnnotation',
158  'rest' => 'Ubiquity\annotations\rest\RestAnnotation',
159  'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation'
160  ] );
161  }
162 }
static glob_recursive($pattern, $flags=0)
Definition: UFileSystem.php:12
static _getFiles(&$config, $type, $silent=false)
static getCacheInstance(&$config, $cacheDirectory, $postfix)
static _clearCache($cacheDirectories, $type, $typeRef)
static startProd(&$config)
Starts the cache for production.
static initialGetCacheDirectory(&$config)
static checkCache(&$config, $silent=false)
static getCacheDirectories(&$config, $silent=false)
static initCache(&$config, $type="all", $silent=false)
static deleteAllFilesFromFolder($folder)
Definition: UFileSystem.php:20
static clearCache(&$config, $type="all")