Ubiquity  2.0.3
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  $gitCacheDir = ROOT . DS . $cacheDirectory . DS . "git";
92  return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir ];
93  }
94 
95  private static function safeMkdir($dir) {
96  if (! is_dir ( $dir ))
97  return mkdir ( $dir, 0777, true );
98  }
99 
100  public static function clearCache(&$config, $type = "all") {
101  $cacheDirectories = self::checkCache ( $config );
102  $cacheDirs = [ "annotations","controllers","models","queries","views" ];
103  foreach ( $cacheDirs as $typeRef ) {
104  self::_clearCache ( $cacheDirectories, $type, $typeRef );
105  }
106  }
107 
108  private static function _clearCache($cacheDirectories, $type, $typeRef) {
109  if ($type === "all" || $type === $typeRef)
110  UFileSystem::deleteAllFilesFromFolder ( $cacheDirectories [$typeRef] );
111  }
112 
113  public static function initCache(&$config, $type = "all", $silent = false) {
114  self::checkCache ( $config, $silent );
115  self::start ( $config );
116  if ($type === "all" || $type === "models")
117  self::initModelsCache ( $config, false, $silent );
118  if ($type === "all" || $type === "controllers")
119  self::initRouterCache ( $config, $silent );
120  if ($type === "all" || $type === "rest")
121  self::initRestCache ( $config, $silent );
122  }
123 
124  protected static function _getFiles(&$config, $type, $silent = false) {
125  $typeNS = $config ["mvcNS"] [$type];
126  $typeDir = ROOT . DS . str_replace ( "\\", DS, $typeNS );
127  if (! $silent)
128  echo \ucfirst ( $type ) . " directory is " . ROOT . $typeNS . "\n";
129  return UFileSystem::glob_recursive ( $typeDir . DS . '*' );
130  }
131 
132  private static function register(AnnotationManager $annotationManager) {
133  $annotationManager->registry = array_merge ( $annotationManager->registry, [ 'id' => 'Ubiquity\annotations\IdAnnotation','manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation','oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
134  'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation','joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation','table' => 'Ubiquity\annotations\TableAnnotation','transient' => 'Ubiquity\annotations\TransientAnnotation','column' => 'Ubiquity\annotations\ColumnAnnotation',
135  'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation','route' => 'Ubiquity\annotations\router\RouteAnnotation','var' => 'mindplay\annotations\standard\VarAnnotation','yuml' => 'Ubiquity\annotations\YumlAnnotation','rest' => 'Ubiquity\annotations\rest\RestAnnotation',
136  'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation' ] );
137  }
138 }
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")