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