DevRouterCacheTrait::getControllers()   B
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.1626

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 24
ccs 15
cts 17
cp 0.8824
rs 7.6666
cc 10
nc 20
nop 4
crap 10.1626

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ubiquity\cache\traits;
4
5
use Ubiquity\cache\ClassUtils;
6
use Ubiquity\cache\parser\ControllerParser;
7
use Ubiquity\controllers\Startup;
8
use Ubiquity\controllers\di\DiManager;
9
use Ubiquity\utils\base\UArray;
10
use Ubiquity\exceptions\ParserException;
11
12
/**
13
 * Ubiquity\cache\traits$DevRouterCacheTrait
14
 * This class is part of Ubiquity
15
 *
16
 * @author jc
17
 * @version 1.0.15
18
 *
19
 */
20
trait DevRouterCacheTrait {
21
	
22
	abstract public static function getAnnotationsEngineInstance();
23
	
24
	private static function addControllerCache(string $classname): array {
25
		$parser = new ControllerParser ( self::getAnnotationsEngineInstance () );
26
		try {
27
			$parser->parse ( $classname );
28
			return $parser->asArray ();
29
		} catch ( \Exception $e ) {
30
			// Nothing to do
31
		}
32
		return [ ];
33
	}
34
	
35 17
	private static function parseControllerFiles(array &$config, bool $silent = false,bool $activeDomain=false): array {
36 17
		$routes = [ 'rest' => [ ],'default' => [ ] ];
37 17
		if($activeDomain){
38
			$files=self::getControllersFiles($config,$silent);
39
		}else {
40 17
			$files = self::getAllControllersFiles($config, $silent);
41
		}
42 17
		$annotsEngine = self::getAnnotationsEngineInstance ();
43 17
		foreach ( $files as $file ) {
44 17
			if (is_file ( $file )) {
45 17
				$controller = ClassUtils::getClassFullNameFromFile ( $file );
46 17
				$parser = new ControllerParser ( $annotsEngine );
47 17
				$parser->setSilent($silent);
48
				try {
49 17
					$parser->parse ( $controller);
50 17
					$ret = $parser->asArray ();
51 17
					$key = ($parser->isRest ()) ? 'rest' : 'default';
52 17
					$routes [$key] = \array_merge ( $routes [$key], $ret );
53
				} catch ( \Exception $e ) {
54
					if (!$silent && $e instanceof ParserException) {
55
						throw $e;
56
					}
57
					// Nothing to do
58
				}
59
			}
60
		}
61 17
		self::sortByPriority ( $routes ['default'] );
62 17
		self::sortByPriority ( $routes ['rest'] );
63 17
		$routes ['rest-index'] = self::createIndex ( $routes ['rest'] );
64 17
		$routes ['default-index'] = self::createIndex ( $routes ['default'] );
65 17
		return $routes;
66
	}
67
	
68 17
	private static function hasCapturingGroup(string $expression): bool {
69 17
		return \preg_match ( "~\\\\.(*SKIP)(?!)|\((?(?=\?)\?(P?['<]\w+['>]))~", $expression )===1;
70
	}
71
	
72 17
	public static function getFirstPartIndex(string $element): string {
73 17
		return \strtok ( \trim ( $element, '/' ), '/' );
74
	}
75
	
76 17
	protected static function createIndex(array $routes): array {
77 17
		$res = [ ];
78 17
		foreach ( $routes as $path => $route ) {
79 17
			if (self::hasCapturingGroup ( $path )) {
80 17
				$part = self::getFirstPartIndex ( $path );
81 17
				if ($part != null) {
82 17
					if($part===\trim($path,'/')){
83
						$part='*';
84
					}
85 17
					$res [$part] [] = $path;
86
				}
87
			}
88
		}
89 17
		return $res;
90
	}
91
	
92 17
	protected static function sortByPriority(array &$array): void {
93 17
		\uasort ( $array, function ($item1, $item2) {
94 17
			return UArray::getRecursive ( $item2, 'priority', 0 ) <=> UArray::getRecursive ( $item1, 'priority', 0 );
95 17
		} );
96 17
			UArray::removeRecursive ( $array, 'priority' );
97
	}
98
	
99 8
	protected static function initRouterCache(array &$config, bool $silent = false): void {
100 8
		$routes = self::parseControllerFiles ( $config, $silent );
101 8
		self::$cache->store ( 'controllers/routes.default', $routes ['default'], 'controllers' );
102 8
		self::$cache->store ( 'controllers/routes.rest', $routes ['rest'], 'controllers' );
103 8
		self::$cache->store ( 'controllers/routes.default-index', $routes ['default-index'], 'controllers' );
104 8
		self::$cache->store ( 'controllers/routes.rest-index', $routes ['rest-index'], 'controllers' );
105 8
		DiManager::init ( $config );
106 8
		if (! $silent) {
107 8
			echo "Router cache reset\n";
108
		}
109
	}
110
	
111 10
	public static function getControllersFiles(array &$config, bool $silent = false): array {
112 10
		return self::_getFiles ( $config, 'controllers', $silent );
113
	}
114
115 17
	public static function getAllControllersFiles(array &$config, bool $silent = false): array {
116 17
		return self::_getAllFiles ( $config, 'controllers', $silent );
117
	}
118
	
119 10
	public static function getControllers(string $subClass = "\\Ubiquity\\controllers\\Controller", bool $backslash = false, bool $includeSubclass = false, bool $includeAbstract = false): array {
120 10
		$result = [ ];
121 10
		if ($includeSubclass) {
122 1
			$result [] = $subClass;
123
		}
124 10
		$config = Startup::getConfig ();
125 10
		$files = self::getControllersFiles ( $config, true );
126
		try {
127 10
			$restCtrls = self::getRestCache ();
128
		} catch ( \Exception $e ) {
129
			$restCtrls = [ ];
130
		}
131 10
		foreach ( $files as $file ) {
132 10
			if (\is_file ( $file )) {
133 10
				$controllerClass = ClassUtils::getClassFullNameFromFile ( $file, $backslash );
134 10
				if (\class_exists ( $controllerClass ) && isset ( $restCtrls [$controllerClass] ) === false) {
135 10
					$r = new \ReflectionClass ( $controllerClass );
136 10
					if ($r->isSubclassOf ( $subClass ) && ($includeAbstract || ! $r->isAbstract ())) {
137 10
						$result [] = $controllerClass;
138
					}
139
				}
140
			}
141
		}
142 10
		return $result;
143
	}
144
}
145
146