| Total Complexity | 48 |
| Total Lines | 181 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RouterCacheTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RouterCacheTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | trait RouterCacheTrait{ |
||
| 19 | |||
| 20 | abstract protected static function _getFiles(&$config, $type, $silent=false); |
||
| 21 | private static $expiredRoutes=[ ]; |
||
| 22 | |||
| 23 | private static function addControllerCache($classname) { |
||
| 32 | } |
||
| 33 | |||
| 34 | private static function initRouterCache(&$config, $silent=false) { |
||
| 35 | $routes=[ "rest" => [ ],"default" => [ ] ]; |
||
| 36 | $files=self::getControllersFiles($config); |
||
| 37 | foreach ( $files as $file ) { |
||
| 38 | if (is_file($file)) { |
||
| 39 | $controller=ClassUtils::getClassFullNameFromFile($file); |
||
| 40 | $parser=new ControllerParser(); |
||
| 41 | try { |
||
| 42 | $parser->parse($controller); |
||
| 43 | $ret=$parser->asArray(); |
||
| 44 | $key=($parser->isRest()) ? "rest" : "default"; |
||
| 45 | $routes[$key]=\array_merge($routes[$key], $ret); |
||
| 46 | } catch ( \Exception $e ) { |
||
| 47 | // Nothing to do |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | self::$cache->store("controllers/routes.default", "return " . UArray::asPhpArray($routes["default"], "array") . ";", 'controllers'); |
||
| 52 | self::$cache->store("controllers/routes.rest", "return " . UArray::asPhpArray($routes["rest"], "array") . ";", 'controllers'); |
||
| 53 | if (!$silent) { |
||
| 54 | echo "Router cache reset\n"; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | private static function storeRouteResponse($key, $response) { |
||
| 59 | self::setKeyExpired($key, false); |
||
| 60 | self::$cache->store("controllers/" . $key, $response, 'controllers', false); |
||
| 61 | return $response; |
||
| 62 | } |
||
| 63 | |||
| 64 | private static function getRouteKey($routePath) { |
||
| 65 | return "path" . \md5(\implode("", $routePath)); |
||
| 66 | } |
||
| 67 | |||
| 68 | private static function setKeyExpired($key, $expired=true) { |
||
| 69 | if ($expired) { |
||
| 70 | self::$expiredRoutes[$key]=true; |
||
| 71 | } else { |
||
| 72 | unset(self::$expiredRoutes[$key]); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | public static function getControllerCache($isRest=false) { |
||
| 77 | $key=($isRest) ? "rest" : "default"; |
||
| 78 | if (self::$cache->exists("controllers/routes." . $key)) |
||
| 79 | return self::$cache->fetch("controllers/routes." . $key); |
||
| 80 | return [ ]; |
||
| 81 | } |
||
| 82 | |||
| 83 | public static function getRouteCache($routePath, $duration) { |
||
| 84 | $key=self::getRouteKey($routePath); |
||
| 85 | |||
| 86 | if (self::$cache->exists("controllers/" . $key) && !self::expired($key, $duration)) { |
||
| 87 | $response=self::$cache->file_get_contents("controllers/" . $key); |
||
| 88 | return $response; |
||
| 89 | } else { |
||
| 90 | $response=Startup::runAsString($routePath); |
||
| 91 | return self::storeRouteResponse($key, $response); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | public static function expired($key, $duration) { |
||
| 96 | return self::$cache->expired("controllers/" . $key, $duration) === true || \array_key_exists($key, self::$expiredRoutes); |
||
| 97 | } |
||
| 98 | |||
| 99 | public static function isExpired($path, $duration) { |
||
| 100 | $route=Router::getRoute($path, false); |
||
| 101 | if ($route !== false && \is_array($route)) { |
||
| 102 | return self::expired(self::getRouteKey($route), $duration); |
||
| 103 | } |
||
| 104 | return true; |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function setExpired($routePath, $expired=true) { |
||
| 108 | $key=self::getRouteKey($routePath); |
||
| 109 | self::setKeyExpired($key, $expired); |
||
| 110 | } |
||
| 111 | |||
| 112 | public static function setRouteCache($routePath) { |
||
| 113 | $key=self::getRouteKey($routePath); |
||
| 114 | $response=Startup::runAsString($routePath); |
||
| 115 | return self::storeRouteResponse($key, $response); |
||
| 116 | } |
||
| 117 | |||
| 118 | public static function addAdminRoutes() { |
||
| 120 | } |
||
| 121 | |||
| 122 | public static function getRoutes() { |
||
| 123 | $result=self::getControllerCache(); |
||
| 124 | return $result; |
||
| 125 | } |
||
| 126 | |||
| 127 | public static function getControllerRoutes($controllerClass, $isRest=false) { |
||
| 128 | $result=[ ]; |
||
| 129 | $ctrlCache=self::getControllerCache($isRest); |
||
| 130 | foreach ( $ctrlCache as $path => $routeAttributes ) { |
||
| 131 | if (isset($routeAttributes["controller"])) { |
||
| 132 | if ($routeAttributes["controller"] === $controllerClass) { |
||
| 133 | $result[$path]=$routeAttributes; |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | $firstValue=reset($routeAttributes); |
||
| 137 | if (isset($firstValue) && isset($firstValue["controller"])) { |
||
| 138 | if ($firstValue["controller"] === $controllerClass) { |
||
| 139 | $result[$path]=$routeAttributes; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | return $result; |
||
| 145 | } |
||
| 146 | |||
| 147 | public static function addRoute($path, $controller, $action="index", $methods=null, $name="") { |
||
| 148 | $controllerCache=self::getControllerCache(); |
||
| 149 | Router::addRouteToRoutes($controllerCache, $path, $controller, $action, $methods, $name); |
||
| 150 | self::$cache->store("controllers/routes.default", "return " . UArray::asPhpArray($controllerCache, "array") . ";", 'controllers'); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function addRoutes($pathArray, $controller, $action="index", $methods=null, $name="") { |
||
| 154 | self::addRoutes_($pathArray, $controller,$action,$methods,$name,false); |
||
| 155 | } |
||
| 156 | |||
| 157 | public static function addRestRoutes($pathArray, $controller, $action="index", $methods=null, $name="") { |
||
| 158 | self::addRoutes_($pathArray, $controller,$action,$methods,$name,true); |
||
| 159 | } |
||
| 160 | |||
| 161 | private static function addRoutes_($pathArray, $controller, $action="index", $methods=null, $name="",$isRest=false) { |
||
| 169 | } |
||
| 170 | |||
| 171 | public static function getControllersFiles(&$config, $silent=false) { |
||
| 173 | } |
||
| 174 | |||
| 175 | public static function getControllers($subClass="\\Ubiquity\\controllers\\Controller",$backslash=false,$includeSubclass=false) { |
||
| 199 | } |
||
| 200 | } |
||
| 201 |