Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DevCacheTrait.php
Go to the documentation of this file.
1<?php
2
4
7use Ubiquity\config\Configuration;
9
21
23
24 abstract protected static function getCacheInstance(array &$config, string $cacheDirectory, string $postfix): AbstractDataCache;
25
26 abstract protected static function initRestCache(array &$config, bool $silent = false): void;
27
28 abstract protected static function initRouterCache(array &$config, bool $silent = false): void;
29
30 abstract public static function initModelsCache(array &$config, bool $forChecking = false, bool $silent = false): void;
31
33 if (\class_exists('Ubiquity\\attributes\\AttributesEngine', true)) {
34 return new \Ubiquity\attributes\AttributesEngine();
35 } elseif (\class_exists('Ubiquity\\annotations\\AnnotationsEngine', true)) {
36 return new \Ubiquity\annotations\AnnotationsEngine();
37 }
38 }
39
41 return self::$annotationsEngine ??= self::_getAnnotationsEngineInstance();
42 }
43
44 private static function initialGetCacheDirectory(array &$config): string {
45 return $config['cache']['directory'] ??= 'cache' . \DS;
46 }
47
54 public static function start(array &$config) {
55 self::$cacheDirectory = self::initialGetCacheDirectory($config);
56 $cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
57 self::getAnnotationsEngineInstance()->start($cacheDirectory);
58 self::getCacheInstance($config, $cacheDirectory, '.cache')->init();
59 }
60
66 public static function registerAnnotations(array $nameClasses): void {
67 self::getAnnotationsEngineInstance()->registerAnnotations($nameClasses);
68 }
69
70
78 public static function checkCache(array &$config, bool $silent = false): array {
79 $dirs = self::getCacheDirectories($config, $silent);
80 foreach ($dirs as $dir) {
81 self::safeMkdir($dir);
82 }
83 return $dirs;
84 }
85
93 public static function getCacheDirectories(array &$config, bool $silent = false): array {
94 $cacheDirectory = self::initialGetCacheDirectory($config);
95 $rootDS = \ROOT . \DS;
96 if (!$silent) {
97 echo "cache directory is " . UFileSystem::cleanPathname($rootDS . $cacheDirectory) . "\n";
98 }
99 $cacheDirectory = $rootDS . $cacheDirectory . \DS;
100 $modelsDir = \str_replace("\\", \DS, $config['mvcNS']['models']);
101 $controllersDir = \str_replace("\\", \DS, $config['mvcNS']['controllers']);
102 $annotationCacheDir = $cacheDirectory . 'annotations';
103 $modelsCacheDir = $cacheDirectory . $modelsDir;
104 $queriesCacheDir = $cacheDirectory . 'queries';
105 $controllersCacheDir = $cacheDirectory . $controllersDir;
106 $viewsCacheDir = $cacheDirectory . 'views';
107 $seoCacheDir = $cacheDirectory . 'seo';
108 $gitCacheDir = $cacheDirectory . 'git';
109 $contentsCacheDir = $cacheDirectory . 'contents';
110 $configCacheDir = $cacheDirectory . 'config';
111 return [
112 'annotations' => $annotationCacheDir,
113 'models' => $modelsCacheDir,
114 'controllers' => $controllersCacheDir,
115 'queries' => $queriesCacheDir,
116 'views' => $viewsCacheDir,
117 'seo' => $seoCacheDir,
118 'git' => $gitCacheDir,
119 'contents' => $contentsCacheDir,
120 'config' => $configCacheDir
121 ];
122 }
123
124 private static function safeMkdir(string $dir): bool {
125 if (!\is_dir($dir)) {
126 return \mkdir($dir, 0777, true);
127 }
128 return true;
129 }
130
137 public static function clearCache(array &$config, string $type = 'all') {
138 $cacheDirectories = self::checkCache($config);
139 $cacheDirs = [
140 'annotations',
141 'controllers',
142 'models',
143 'queries',
144 'views',
145 'contents',
146 'config'
147 ];
148 foreach ($cacheDirs as $typeRef) {
149 self::_clearCache($cacheDirectories, $type, $typeRef);
150 }
151 }
152
153 private static function _clearCache($cacheDirectories, $type, $typeRef) {
154 if ($type === 'all' || $type === $typeRef) {
155 UFileSystem::deleteAllFilesFromFolder($cacheDirectories[$typeRef]);
156 }
157 }
158
165 public static function initCache(array &$config, string $type = 'all', bool $silent = false): void {
166 self::checkCache($config, $silent);
167 self::start($config);
168 if ($type === 'all' || $type === 'models') {
169 self::initModelsCache($config, false, $silent);
170 }
171 if ($type === 'all' || $type === 'controllers') {
172 if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) {
173 self::getAnnotationsEngineInstance()->registerAcls();
174 }
175 self::initRouterCache($config, $silent);
176 }
177 if ($type === 'all' || $type === 'acls') {
178 if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) {
179 \Ubiquity\security\acl\AclManager::initCache($config);
180 }
181 }
182 if ($type === 'all' || $type === 'rest') {
183 self::initRestCache($config, $silent);
184 }
185 if ($type === 'all' || $type === 'config') {
186 Configuration::generateCache($silent);
187 }
188 }
189}
190
This class is responsible for storing Arrays in PHP files.
static getCacheInstance(array &$config, string $cacheDirectory, string $postfix)
static AnnotationsEngineInterface $annotationsEngine
static initRouterCache(array &$config, bool $silent=false)
static registerAnnotations(array $nameClasses)
static initRestCache(array &$config, bool $silent=false)
static getCacheDirectories(array &$config, bool $silent=false)
Returns an associative array of cache folders (annotations, models, controllers, queries,...
static initCache(array &$config, string $type='all', bool $silent=false)
static checkCache(array &$config, bool $silent=false)
Checks the existence of cache subdirectories and returns an array of cache folders.
static start(array &$config)
Starts the cache in dev mode, for generating the other caches Do not use in production.
static initialGetCacheDirectory(array &$config)
static clearCache(array &$config, string $type='all')
Deletes files from a cache type.
static initModelsCache(array &$config, bool $forChecking=false, bool $silent=false)
static _clearCache($cacheDirectories, $type, $typeRef)
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.