Ubiquity  2.3.10
php rapid development framework
CacheManager.php
Go to the documentation of this file.
1 <?php
2 
6 namespace Ubiquity\cache;
7 
16 
27 
32  public static $cache;
33  private static $cacheDirectory;
34 
41  public static function start(&$config) {
42  self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
43  $cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
44  Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
45  self::register ( Annotations::getManager () );
46  self::getCacheInstance ( $config, $cacheDirectory, '.cache' );
47  }
48 
54  public static function startProd(&$config) {
55  self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
56  $cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
57  self::getCacheInstance ( $config, $cacheDirectory, '.cache' );
58  }
59 
60  public static function startProdFromCtrl() {
61  $config = &Startup::$config;
62  $cacheD = \ROOT . \DS . ($config ['cache'] ['directory'] ??= 'cache' . \DS);
63  $cacheSystem = $config ['cache'] ['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache';
64  self::$cache = new $cacheSystem ( $cacheD, '.cache', $config ['cache'] ['params'] ?? [ ]);
65  }
66 
67  protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
68  if (! isset ( self::$cache )) {
69  $cacheSystem = $config ['cache'] ['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache';
70  $cacheParams = $config ['cache'] ['params'] ?? [ ];
71 
72  self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
73  }
74  return self::$cache;
75  }
76 
77  private static function initialGetCacheDirectory(&$config) {
78  return $config ['cache'] ['directory'] ??= 'cache' . \DS;
79  }
80 
86  public static function getCacheDirectory() {
87  return self::$cacheDirectory;
88  }
89 
95  public static function getAbsoluteCacheDirectory() {
96  return \ROOT . \DS . self::$cacheDirectory;
97  }
98 
105  public static function getCacheSubDirectory($subDirectory) {
106  return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory;
107  }
108 
116  public static function checkCache(&$config, $silent = false) {
117  $dirs = self::getCacheDirectories ( $config, $silent );
118  foreach ( $dirs as $dir ) {
119  self::safeMkdir ( $dir );
120  }
121  return $dirs;
122  }
123 
131  public static function getCacheDirectories(&$config, $silent = false) {
132  $cacheDirectory = self::initialGetCacheDirectory ( $config );
133  $rootDS = \ROOT . \DS;
134  if (! $silent) {
135  echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS . $cacheDirectory ) . "\n";
136  }
137  $cacheDirectory = $rootDS . $cacheDirectory . \DS;
138  $modelsDir = str_replace ( "\\", \DS, $config ['mvcNS'] ['models'] );
139  $controllersDir = str_replace ( "\\", \DS, $config ['mvcNS'] ['controllers'] );
140  $annotationCacheDir = $cacheDirectory . 'annotations';
141  $modelsCacheDir = $cacheDirectory . $modelsDir;
142  $queriesCacheDir = $cacheDirectory . 'queries';
143  $controllersCacheDir = $cacheDirectory . $controllersDir;
144  $viewsCacheDir = $cacheDirectory . 'views';
145  $seoCacheDir = $cacheDirectory . 'seo';
146  $gitCacheDir = $cacheDirectory . 'git';
147  $contentsCacheDir = $cacheDirectory . 'contents';
148  return [ 'annotations' => $annotationCacheDir,'models' => $modelsCacheDir,'controllers' => $controllersCacheDir,'queries' => $queriesCacheDir,'views' => $viewsCacheDir,'seo' => $seoCacheDir,'git' => $gitCacheDir,'contents' => $contentsCacheDir ];
149  }
150 
151  private static function safeMkdir($dir) {
152  if (! \is_dir ( $dir ))
153  return \mkdir ( $dir, 0777, true );
154  }
155 
162  public static function clearCache(&$config, $type = 'all') {
163  $cacheDirectories = self::checkCache ( $config );
164  $cacheDirs = [ 'annotations','controllers','models','queries','views','contents' ];
165  foreach ( $cacheDirs as $typeRef ) {
166  self::_clearCache ( $cacheDirectories, $type, $typeRef );
167  }
168  }
169 
170  private static function _clearCache($cacheDirectories, $type, $typeRef) {
171  if ($type === 'all' || $type === $typeRef)
172  UFileSystem::deleteAllFilesFromFolder ( $cacheDirectories [$typeRef] );
173  }
174 
181  public static function initCache(&$config, $type = 'all', $silent = false) {
182  self::checkCache ( $config, $silent );
183  self::start ( $config );
184  if ($type === 'all' || $type === 'models')
185  self::initModelsCache ( $config, false, $silent );
186  if ($type === 'all' || $type === 'controllers')
187  self::initRouterCache ( $config, $silent );
188  if ($type === 'all' || $type === 'rest')
189  self::initRestCache ( $config, $silent );
190  }
191 
197  public static function getAllRoutes() {
198  $routes = self::getControllerCache ();
199  return \array_merge ( $routes, self::getControllerCache ( true ) );
200  }
201 
210  protected static function _getFiles(&$config, $type, $silent = false) {
211  $typeNS = $config ['mvcNS'] [$type];
212  $typeDir = \ROOT . \DS . \str_replace ( "\\", \DS, $typeNS );
213  if (! $silent)
214  echo \ucfirst ( $type ) . ' directory is ' . \ROOT . $typeNS . "\n";
215  return UFileSystem::glob_recursive ( $typeDir . \DS . '*.php' );
216  }
217 
218  private static function register(AnnotationManager $annotationManager) {
219  $annotationManager->registry = \array_merge ( $annotationManager->registry, [
220  'id' => 'Ubiquity\annotations\IdAnnotation',
221  'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation',
222  'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
223  'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation',
224  'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
225  'table' => 'Ubiquity\annotations\TableAnnotation',
226  'database' => 'Ubiquity\annotations\DatabaseAnnotation',
227  'transient' => 'Ubiquity\annotations\TransientAnnotation',
228  'column' => 'Ubiquity\annotations\ColumnAnnotation',
229  'validator' => 'Ubiquity\annotations\ValidatorAnnotation',
230  'transformer' => 'Ubiquity\annotations\TransformerAnnotation',
231  'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation',
232  'requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation',
233  'route' => 'Ubiquity\annotations\router\RouteAnnotation',
234  'get' => 'Ubiquity\annotations\router\GetAnnotation',
235  'getMapping' => 'Ubiquity\annotations\router\GetAnnotation',
236  'post' => 'Ubiquity\annotations\router\PostAnnotation',
237  'postMapping' => 'Ubiquity\annotations\router\PostAnnotation',
238  'put' => 'Ubiquity\annotations\router\PutAnnotation',
239  'putMapping' => 'Ubiquity\annotations\router\PutAnnotation',
240  'patch' => 'Ubiquity\annotations\router\PatchAnnotation',
241  'patchMapping' => 'Ubiquity\annotations\router\PatchAnnotation',
242  'delete' => 'Ubiquity\annotations\router\DeleteAnnotation',
243  'deleteMapping' => 'Ubiquity\annotations\router\DeleteAnnotation',
244  'options' => 'Ubiquity\annotations\router\OptionsAnnotation',
245  'optionsMapping' => 'Ubiquity\annotations\router\OptionsAnnotation',
246  'var' => 'mindplay\annotations\standard\VarAnnotation',
247  'yuml' => 'Ubiquity\annotations\YumlAnnotation',
248  'rest' => 'Ubiquity\annotations\rest\RestAnnotation',
249  'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation',
250  'injected' => 'Ubiquity\annotations\di\InjectedAnnotation',
251  'autowired' => 'Ubiquity\annotations\di\AutowiredAnnotation' ] );
252  }
253 }
static initCache(&$config, $type='all', $silent=false)
static glob_recursive($pattern, $flags=0)
Find recursively pathnames matching a pattern.
Definition: UFileSystem.php:25
static clearCache(&$config, $type='all')
Deletes files from a cache type.
static _getFiles(&$config, $type, $silent=false)
Returns an array of files from type $type.
static getCacheInstance(&$config, $cacheDirectory, $postfix)
static _clearCache($cacheDirectories, $type, $typeRef)
static cleanPathname($path)
Cleans a directory path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR.
Definition: UFileSystem.php:79
static start(&$config)
Starts the cache in dev mode, for generating the other caches Do not use in production.
static deleteAllFilesFromFolder($folder, $mask=' *')
Deletes all files from a folder (not in subfolders)
Definition: UFileSystem.php:39
static startProd(&$config)
Starts the cache for production.
static getCacheSubDirectory($subDirectory)
Returns an absolute cache subdirectory.
static initialGetCacheDirectory(&$config)
static getAbsoluteCacheDirectory()
Returns the absolute cache directory.
static getCacheDirectory()
Returns the relative cache directory.
static checkCache(&$config, $silent=false)
Checks the existence of cache subdirectories and returns an array of cache folders.
static getAllRoutes()
Returns an array of all defined routes, included REST routes.
static getCacheDirectories(&$config, $silent=false)
Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents)
Cache managment.
Definition: CacheFile.php:3
Manager for caches (Router, Rest, models).