Completed
Push — master ( 980c15...74d9cb )
by Jean-Christophe
01:25
created

ControllerAction::setAnnots()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace micro\controllers\admin\popo;
4
5
use micro\cache\CacheManager;
6
use micro\controllers\Startup;
7
use micro\cache\ClassUtils;
8
use micro\controllers\Router;
9
use micro\utils\StrUtils;
10
11
class ControllerAction {
12
	private $controller;
13
	private $action;
14
	private $parameters;
15
	private $dValues;
16
	private $annots;
17
18
	private static $excludeds=["__construct","isValid","initialize","finalize","onInvalidControl","loadView","forward"];
19
20
21
	public function __construct($controller="",$action="",$parameters=[],$dValues=[],$annots=[]){
22
		$this->controller=$controller;
23
		$this->action=$action;
24
		$this->parameters=$parameters;
25
		$this->dValues=$dValues;
26
		$this->annots=$annots;
27
	}
28
29
	public static function initWithPath($url){
30
		$result=[];
31
		$config=Startup::getConfig();
32
		$ns=$config["mvcNS"]["controllers"];
33
		if ($ns !== "" && $ns !== null) {
34
			$ns.="\\";
35
		}
36
		if (!$url) {
37
			$url="_default";
38
		}
39 View Code Duplication
		if (StrUtils::endswith($url, "/"))
40
			$url=\substr($url, 0, strlen($url) - 1);
41
		$u=\explode("/", $url);
42
		$u[0]=$ns . $u[0];
43
		if(\class_exists($u[0])){
44
			$controllerClass=$u[0];
45
			if(\count($u)<2)
46
				$u[]="index";
47
			if(\method_exists($controllerClass, $u[1])){
48
				$method=new \ReflectionMethod($u[0],$u[1]);
49
				$r=self::scanMethod($controllerClass, $method);
50
				if(isset($r))
51
					$result[]=$r;
52
			}
53
		}
54
		return $result;
55
	}
56
	public static function init(){
57
		$result=[];
58
		$config=Startup::getConfig();
59
60
		$files=CacheManager::getControllerFiles($config,true);
0 ignored issues
show
Bug introduced by
The method getControllerFiles() does not seem to exist on object<micro\cache\CacheManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
		foreach ( $files as $file ) {
62
			if (is_file($file)) {
63
				$controllerClass=ClassUtils::getClassFullNameFromFile($file);
64
				$reflect=new \ReflectionClass($controllerClass);
65
				if (!$reflect->isAbstract() && $reflect->isSubclassOf("micro\controllers\Controller")) {
66
					$methods=$reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
67
					foreach ( $methods as $method ) {
68
						$r=self::scanMethod($controllerClass, $method);
69
						if(isset($r))
70
							$result[]=$r;
71
					}
72
				}
73
			}
74
		}
75
		return $result;
76
	}
77
78
	private static function scanMethod($controllerClass,\ReflectionMethod $method){
79
		$result=null;
80
		if(\array_search($method->name, self::$excludeds)===false && !StrUtils::startswith($method->name, "_")){
81
			$annots=Router::getAnnotations($controllerClass, $method->name);
0 ignored issues
show
Bug introduced by
The method getAnnotations() does not seem to exist on object<micro\controllers\Router>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
			$parameters=$method->getParameters();
83
			$defaults=[];
84
			foreach ($parameters as $param){
85
				if($param->isOptional()){
86
					$defaults[$param->name]=$param->getDefaultValue();
87
				}
88
			}
89
			$result=new ControllerAction($controllerClass,$method->name,$parameters,$defaults,$annots);
90
		}
91
		return $result;
92
	}
93
94
	public function getController() {
95
		return $this->controller;
96
	}
97
98
	public function setController($controller) {
99
		$this->controller=$controller;
100
		return $this;
101
	}
102
103
	public function getAction() {
104
		return $this->action;
105
	}
106
107
	public function setAction($action) {
108
		$this->action=$action;
109
		return $this;
110
	}
111
112
	public function getParameters() {
113
		return $this->parameters;
114
	}
115
116
	public function setParameters($parameters) {
117
		$this->parameters=$parameters;
118
		return $this;
119
	}
120
121
	public function getDValues() {
122
		return $this->dValues;
123
	}
124
125
	public function setDValues($dValues) {
126
		$this->dValues=$dValues;
127
		return $this;
128
	}
129
130
	public function getAnnots() {
131
		return $this->annots;
132
	}
133
134
	public function setAnnots($annots) {
135
		$this->annots=$annots;
136
		return $this;
137
	}
138
}
139