Ubiquity  2.0.3
php rapid development framework
ControllerAction.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
12  private $controller;
13  private $action;
14  private $parameters;
15  private $dValues;
16  private $annots;
17  private static $excludeds=[ "__construct","isValid","initialize","finalize","onInvalidControl","loadView","forward","redirectToRoute","getView","message","loadDefaultView","getDefaultViewName","showSimpleMessage","showConfMessage" ];
18  public static $controllers=[];
19 
20  public function __construct($controller="", $action="", $parameters=[], $dValues=[], $annots=[]) {
21  $this->controller=$controller;
22  $this->action=$action;
23  $this->parameters=$parameters;
24  $this->dValues=$dValues;
25  $this->annots=$annots;
26  }
27 
28  public static function initWithPath($url) {
29  $result=[ ];
30  $config=Startup::getConfig();
31  $ns=$config["mvcNS"]["controllers"];
32  if ($ns !== "" && $ns !== null) {
33  $ns.="\\";
34  }
35  if (!$url) {
36  $url="_default";
37  }
38  if (UString::endswith($url, "/"))
39  $url=\substr($url, 0, strlen($url) - 1);
40  $u=\explode("/", $url);
41  $u[0]=$ns . $u[0];
42  if (\class_exists($u[0])) {
43  $controllerClass=$u[0];
44  if (\count($u) < 2)
45  $u[]="index";
46  if (\method_exists($controllerClass, $u[1])) {
47  $method=new \ReflectionMethod($u[0], $u[1]);
48  $r=self::scanMethod($controllerClass, $method);
49  if (isset($r))
50  $result[]=$r;
51  }
52  }
53  return $result;
54  }
55 
56  public static function init() {
57  $result=[ ];
58  $config=Startup::getConfig();
59 
60  $files=CacheManager::getControllersFiles($config, true);
61  try {
62  $restCtrls=CacheManager::getRestCache();
63  } catch ( \Exception $e ) {
64  $restCtrls=[ ];
65  }
66 
67  foreach ( $files as $file ) {
68  if (is_file($file)) {
69  $controllerClass=ClassUtils::getClassFullNameFromFile($file);
70  if (isset($restCtrls[$controllerClass]) === false) {
71  self::$controllers[]=$controllerClass;
72  $reflect=new \ReflectionClass($controllerClass);
73  if (!$reflect->isAbstract() && $reflect->isSubclassOf("Ubiquity\controllers\Controller") && ! $reflect->isSubclassOf("Ubiquity\controllers\seo\SeoController")) {
74  $methods=$reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
75  foreach ( $methods as $method ) {
76  $r=self::scanMethod($controllerClass, $method);
77  if (isset($r))
78  $result[]=$r;
79  }
80  }
81  }
82  }
83  }
84  return $result;
85  }
86 
87  private static function scanMethod($controllerClass, \ReflectionMethod $method) {
88  $result=null;
89  if (\array_search($method->name, self::$excludeds) === false && !UString::startswith($method->name, "_")) {
90  $annots=Router::getAnnotations($controllerClass, $method->name);
91  $parameters=$method->getParameters();
92  $defaults=[ ];
93  foreach ( $parameters as $param ) {
94  if ($param->isOptional() && !$param->isVariadic()) {
95  $defaults[$param->name]=$param->getDefaultValue();
96  }
97  }
98  $result=new ControllerAction($controllerClass, $method->name, $parameters, $defaults, $annots);
99  }
100  return $result;
101  }
102 
103  public function getController() {
104  return $this->controller;
105  }
106 
107  public function setController($controller) {
108  $this->controller=$controller;
109  return $this;
110  }
111 
112  public function getAction() {
113  return $this->action;
114  }
115 
116  public function setAction($action) {
117  $this->action=$action;
118  return $this;
119  }
120 
121  public function getParameters() {
122  return $this->parameters;
123  }
124 
125  public function setParameters($parameters) {
126  $this->parameters=$parameters;
127  return $this;
128  }
129 
130  public function getDValues() {
131  return $this->dValues;
132  }
133 
134  public function setDValues($dValues) {
135  $this->dValues=$dValues;
136  return $this;
137  }
138 
139  public function getAnnots() {
140  return $this->annots;
141  }
142 
143  public function setAnnots($annots) {
144  $this->annots=$annots;
145  return $this;
146  }
147 
148  public function getPath() {
149  $reflect=new \ReflectionClass($this->controller);
150  return $reflect->getShortName() . "/" . $this->action;
151  }
152 
153  public function getId() {
154  return $this->getPath();
155  }
156 }
static scanMethod($controllerClass, \ReflectionMethod $method)
static getAnnotations($controllerName, $actionName)
Definition: Router.php:87
__construct($controller="", $action="", $parameters=[], $dValues=[], $annots=[])
static getControllersFiles(&$config, $silent=false)
static endswith($hay, $needle)
Definition: UString.php:16
static startswith($hay, $needle)
Definition: UString.php:12
static getClassFullNameFromFile($filePathName, $backSlash=false)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Definition: ClassUtils.php:17