Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
RestCacheTrait.php
Go to the documentation of this file.
1<?php
2
4
7use Ubiquity\utils\base\UArray;
9
20
21 protected static function initRestCache(array &$config, bool $silent = false): void {
22 $restCache = [ ];
23 $files = self::getControllersFiles ( $config );
24 foreach ( $files as $file ) {
25 if (is_file ( $file )) {
26 $controller = ClassUtils::getClassFullNameFromFile ( $file );
27 $parser = new RestControllerParser ();
28 $parser->parse ( $controller, $config );
29 if ($parser->isRest ()) {
30 $restCache = \array_merge($restCache, $parser->asArray());
31 }
32 }
33 }
34 self::$cache->store ( 'controllers/rest', $restCache, 'controllers' );
35 if (! $silent) {
36 echo "Rest cache reset\n";
37 }
38 }
39
40 public static function getRestRoutes(): array {
41 $result = [ ];
42 $restCache = self::getRestCache ();
43 foreach ( $restCache as $controllerClass => $restAttributes ) {
44 if (isset ( $restCache [$controllerClass] )) {
45 $result [$controllerClass] = [ 'restAttributes' => $restAttributes,'routes' => self::getControllerRoutes ( $controllerClass, true ) ];
46 }
47 }
48 return $result;
49 }
50
51 public static function getRestCache() {
52 if (self::$cache->exists ( 'controllers/rest' )) {
53 return self::$cache->fetch('controllers/rest');
54 }
55 throw new RestException ( 'Rest cache entry `' . self::$cache->getEntryKey ( 'controllers/rest' ) . "` is missing.\nTry to Re-init Rest cache." );
56 }
57
58 public static function getRestResource(string $controllerClass) {
59 $cacheControllerClass = self::getRestCacheController ( $controllerClass );
60 if (isset ( $cacheControllerClass )) {
61 return $cacheControllerClass ['resource'];
62 }
63 return null;
64 }
65
66 public static function getRestCacheController(string $controllerClass) {
67 $cache = self::getRestCache ();
68 if (isset ( $cache [$controllerClass] )) {
69 return $cache [$controllerClass];
70 }
71 return null;
72 }
73}
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
static getClassFullNameFromFile($filePathName, $backSlash=false)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Ubiquity\cache\parser$RestControllerParser This class is part of Ubiquity.
static initRestCache(array &$config, bool $silent=false)
static getRestCacheController(string $controllerClass)
static getRestResource(string $controllerClass)
Exceptions for Rest service.