Guest User

Untitled

a guest
Jul 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Ione
  5. * Date: 09/07/2018
  6. * Time: 22:40
  7. */
  8.  
  9. class Request
  10. {
  11. private $attrs = [];
  12.  
  13. public function __construct(array $attr = [])
  14. {
  15. $this->attrs = $attr;
  16. }
  17.  
  18. public function __get($name)
  19. {
  20. return $this->attrs[":{$name}"];
  21. }
  22.  
  23. }
  24.  
  25.  
  26. class Testes
  27. {
  28. public function home(Request $request)
  29. {
  30. echo "{$request->show} estou em casa com {$request->marombada}";
  31. }
  32. }
  33.  
  34. class Router
  35. {
  36.  
  37. private $methods = ['GET', 'POST'];
  38. private $routes = [];
  39.  
  40. public function __call($name, $args)
  41. {
  42. if (in_array(strtoupper($name), $this->methods)) {
  43. list($uri, $callable) = $args;
  44. $this->routes[strtoupper($name)][$uri] = $callable;
  45. }
  46. }
  47. public function run(Di $di)
  48. {
  49. $found = false;
  50. $uriServer = $_SERVER['REQUEST_URI'];
  51. $method = strtoupper($_SERVER['REQUEST_METHOD']);
  52. foreach ($this->routes[$method] as $uri => $callable) {
  53. $ex1 = explode('/', $uriServer);
  54. $ex2 = explode('/', $uri);
  55. if (count($this->clear($ex1)) == count($this->clear($ex2))) {
  56. $params = array_combine($ex2, $ex1);
  57.  
  58. $req = new Request($params != false ? $this->clear($params) : []);
  59. $di->add(Request::class, $req);
  60. $pattern = '/(:\w*)/';
  61. $replace = array_filter(explode('/', $uriServer), function ($item) {
  62. return !empty($item);
  63. });
  64.  
  65. $variables = preg_grep($pattern, explode('/', $uri));
  66.  
  67. $result = array_map(function ($item) {
  68. return str_replace('/', '', $item);
  69. }, array_filter(preg_split($pattern, $uri), function ($item) {
  70. return !empty($item);
  71. }));
  72.  
  73. $arrayKeys = array_keys($variables);
  74.  
  75. foreach ($arrayKeys as $key => $indice) {
  76. unset($replace[$indice]);
  77. }
  78. $found = !array_diff($replace, $result);
  79. if ($found) {
  80. if (gettype($callable) == 'string') {
  81. if (strpos($callable, '@') === false) {
  82. $callable($req);
  83. } else {
  84. list($class, $action) = explode('@', $callable);
  85. $ex = new Executor();
  86. $ex->setDi($di);
  87. $ex->setRc(new ReflectionClass($class));
  88. $name = $ex->execute($action);
  89. //(new $class)->{$action}($req);
  90. }
  91. } else {
  92. $callable($req);
  93. echo '<br>';
  94. }
  95. return;
  96. }
  97. }
  98. }
  99. if(!$found){
  100. echo 'not found';
  101. }
  102. }
  103.  
  104. public function clear(array $array)
  105. {
  106. return array_filter($array, function ($el) {
  107. return !empty($el);
  108. });
  109. }
  110.  
  111. }
  112. $start = microtime(true);
  113.  
  114. class Di
  115. {
  116. private $store = [];
  117.  
  118. /**
  119. * @return array
  120. */
  121. public function getStore()
  122. {
  123. return $this->store;
  124. }
  125.  
  126. public function add($name, $value)
  127. {
  128. $this->store[$name] = $value;
  129. }
  130.  
  131. public function has($name)
  132. {
  133. return isset($this->store[$name]);
  134. }
  135.  
  136. public function get($name)
  137. {
  138. return $this->store[$name];
  139. }
  140. }
  141.  
  142. Class Foo
  143. {
  144. public function helloworld($nome)
  145. {
  146. return 'Hole mundou ' . $nome;
  147. }
  148. }
  149.  
  150. $di = new Di();
  151. $di->add(Foo::class, new Foo());
  152.  
  153. class Teste
  154. {
  155. private $nome;
  156.  
  157. public function getNome()
  158. {
  159. return $this->nome;
  160. }
  161.  
  162. public function setNome(Foo $foo, $nome)
  163. {
  164. return $foo->helloworld($nome);
  165. }
  166.  
  167. public function qualquercoisa()
  168. {
  169. return 'eu to aki';
  170. }
  171. }
  172.  
  173. $rc = new \ReflectionClass('Teste');
  174.  
  175. class ExecutorInterator implements Iterator
  176. {
  177. protected $elements = [];
  178.  
  179. public function __construct(array $elements = [])
  180. {
  181. $this->elements = $elements;
  182. }
  183.  
  184.  
  185. public function current()
  186. {
  187. return current($this->elements);
  188. }
  189.  
  190.  
  191. public function next()
  192. {
  193. return next($this->elements);
  194. }
  195.  
  196.  
  197. public function key()
  198. {
  199. return key($this->elements);
  200. }
  201.  
  202.  
  203. public function valid()
  204. {
  205. return isset($this->elements[current($this->elements)]);
  206. }
  207.  
  208. public function rewind()
  209. {
  210. return reset($this->elements);
  211. }
  212.  
  213. public function count()
  214. {
  215. return count($this->elements);
  216. }
  217.  
  218. public function add($element, $name = null)
  219. {
  220. $this->elements[] = $element;
  221. }
  222.  
  223. public function remove($element)
  224. {
  225. foreach ($this->elements as $index => $e) {
  226. if ($e == $element) {
  227. unset($this->elements[$index]);
  228. }
  229. }
  230. }
  231. }
  232.  
  233. class ExecutorCompostInterator extends ExecutorInterator
  234. {
  235. public function add($element, $name = null)
  236. {
  237. $this->elements[$name] = $element;
  238. }
  239. }
  240.  
  241. class Executor
  242. {
  243. private $rc = null;
  244. private $di = null;
  245. private $params = [];
  246. private $mparams = [];
  247.  
  248. /**
  249. * @return null
  250. */
  251. public function getRc()
  252. {
  253. return $this->rc;
  254. }
  255.  
  256. /**
  257. * @param null $rc
  258. * @return Executor
  259. */
  260. public function setRc(ReflectionClass $rc)
  261. {
  262. $this->rc = $rc;
  263. return $this;
  264. }
  265.  
  266. /**
  267. * @return null
  268. */
  269. public function getDi()
  270. {
  271. return $this->di;
  272. }
  273.  
  274. /**
  275. * @param null $di
  276. * @return Executor
  277. */
  278. public function setDi(Di $di)
  279. {
  280. $this->di = $di;
  281. return $this;
  282. }
  283.  
  284. function extractParamsOfMethods()
  285. {
  286. $methods = $this->getMethods();
  287. $params = new ExecutorCompostInterator();
  288. foreach ($methods as $method) {
  289. $params->add($method->name, $method->getParameters());
  290. }
  291. return $params;
  292. }
  293.  
  294. function getMethods()
  295. {
  296. return $this->rc->getMethods(\ReflectionMethod::IS_PUBLIC);
  297. }
  298.  
  299. function notEmpty($item)
  300. {
  301. return !empty($item);
  302. }
  303.  
  304. public function prepareExecution($param, $index, $method)
  305. {
  306. $this->fillParameter(
  307. $this->filterUnnecessaryElements(
  308. $this->removeUnncessaryCharacters(
  309. $this->getValueFromInfo(
  310. $this->extractInfoParameters($method, $index)
  311. )
  312. )
  313. ), $this->mparams
  314. );
  315. }
  316.  
  317. public function fillParameter($end, $param)
  318. {
  319. if ($this->hasType($end)) {
  320. $this->params[] =
  321. $this->di->get(reset($end));
  322. }
  323. if (!$this->hasType($end)) {
  324. $this->params[] = $param;
  325. }
  326. }
  327.  
  328. public function hasType($param)
  329. {
  330. return count($param) == 2;
  331. }
  332.  
  333. function filterUnnecessaryElements(array $end)
  334. {
  335. $end = array_filter($end, [$this, 'notEmpty']);
  336. return $end;
  337. }
  338.  
  339. public function removeUnncessaryCharacters($output)
  340. {
  341. $end = explode(' ', str_replace(['[', ']', '<required>'], '', trim($output[0])));
  342. return $end;
  343. }
  344.  
  345. public function getValueFromInfo($export)
  346. {
  347. preg_match('/\[.*\]/', $export, $output);
  348. return $output;
  349. }
  350.  
  351. function extractInfoParameters(\ReflectionMethod $method, $index)
  352. {
  353. $refParam = new \ReflectionParameter([$method->class, $method->name], $index);
  354. $export = \ReflectionParameter::export(
  355. array(
  356. $refParam->getDeclaringClass()->name,
  357. $refParam->getDeclaringFunction()->name
  358. ),
  359. $refParam->name,
  360. true
  361. );
  362. return $export;
  363. }
  364.  
  365. public function execute($methodName, $parameter = null)
  366. {
  367. $this->mparams = $parameter;
  368. $method = $this->getMethod($methodName);
  369. $instance = $this->rc->newInstance();
  370. $params = $this->extractParamsOfMethod($methodName);
  371. array_walk($params, [$this, 'prepareExecution'], $method);
  372. return $method->invokeArgs($instance, $this->params);
  373. }
  374.  
  375. function getMethod($name)
  376. {
  377. return $this->rc->getMethod($name);
  378. }
  379.  
  380. function extractParamsOfMethod($name)
  381. {
  382. return $this->getMethod($name)->getParameters();
  383. }
  384. }
  385.  
  386.  
  387. echo '<pre>';
  388. $router = new Router;
  389. $router->get('/', function () {
  390. echo 'start';
  391. });
  392. $router->get('/data', function () {
  393. echo 'get data';
  394. });
  395. $router->post('/data', function (Request $request) {
  396. echo 'post data';
  397. });
  398. $router->get('/datas/:var', function (Request $request) {
  399. echo 'datas ' . $request->var;
  400. });
  401. $router->get('/date/:show', function (Request $request) {
  402. echo 'date ' . $request->show;
  403. });
  404. $router->get('/data/:show', function (Request $request) {
  405. echo 'datA ' . $request->show;
  406. });
  407.  
  408. $router->get('/data/:show/denovo', function (Request $request) {
  409. echo 'show: ' . $request->show;
  410. });
  411.  
  412. $router->get('/data/:show/denovo/:marombada', 'Testes@home');
  413.  
  414. $router->run($di);
Add Comment
Please, Sign In to add comment