Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: Ione
- * Date: 09/07/2018
- * Time: 22:40
- */
- class Request
- {
- private $attrs = [];
- public function __construct(array $attr = [])
- {
- $this->attrs = $attr;
- }
- public function __get($name)
- {
- return $this->attrs[":{$name}"];
- }
- }
- class Testes
- {
- public function home(Request $request)
- {
- echo "{$request->show} estou em casa com {$request->marombada}";
- }
- }
- class Router
- {
- private $methods = ['GET', 'POST'];
- private $routes = [];
- public function __call($name, $args)
- {
- if (in_array(strtoupper($name), $this->methods)) {
- list($uri, $callable) = $args;
- $this->routes[strtoupper($name)][$uri] = $callable;
- }
- }
- public function run(Di $di)
- {
- $found = false;
- $uriServer = $_SERVER['REQUEST_URI'];
- $method = strtoupper($_SERVER['REQUEST_METHOD']);
- foreach ($this->routes[$method] as $uri => $callable) {
- $ex1 = explode('/', $uriServer);
- $ex2 = explode('/', $uri);
- if (count($this->clear($ex1)) == count($this->clear($ex2))) {
- $params = array_combine($ex2, $ex1);
- $req = new Request($params != false ? $this->clear($params) : []);
- $di->add(Request::class, $req);
- $pattern = '/(:\w*)/';
- $replace = array_filter(explode('/', $uriServer), function ($item) {
- return !empty($item);
- });
- $variables = preg_grep($pattern, explode('/', $uri));
- $result = array_map(function ($item) {
- return str_replace('/', '', $item);
- }, array_filter(preg_split($pattern, $uri), function ($item) {
- return !empty($item);
- }));
- $arrayKeys = array_keys($variables);
- foreach ($arrayKeys as $key => $indice) {
- unset($replace[$indice]);
- }
- $found = !array_diff($replace, $result);
- if ($found) {
- if (gettype($callable) == 'string') {
- if (strpos($callable, '@') === false) {
- $callable($req);
- } else {
- list($class, $action) = explode('@', $callable);
- $ex = new Executor();
- $ex->setDi($di);
- $ex->setRc(new ReflectionClass($class));
- $name = $ex->execute($action);
- //(new $class)->{$action}($req);
- }
- } else {
- $callable($req);
- echo '<br>';
- }
- return;
- }
- }
- }
- if(!$found){
- echo 'not found';
- }
- }
- public function clear(array $array)
- {
- return array_filter($array, function ($el) {
- return !empty($el);
- });
- }
- }
- $start = microtime(true);
- class Di
- {
- private $store = [];
- /**
- * @return array
- */
- public function getStore()
- {
- return $this->store;
- }
- public function add($name, $value)
- {
- $this->store[$name] = $value;
- }
- public function has($name)
- {
- return isset($this->store[$name]);
- }
- public function get($name)
- {
- return $this->store[$name];
- }
- }
- Class Foo
- {
- public function helloworld($nome)
- {
- return 'Hole mundou ' . $nome;
- }
- }
- $di = new Di();
- $di->add(Foo::class, new Foo());
- class Teste
- {
- private $nome;
- public function getNome()
- {
- return $this->nome;
- }
- public function setNome(Foo $foo, $nome)
- {
- return $foo->helloworld($nome);
- }
- public function qualquercoisa()
- {
- return 'eu to aki';
- }
- }
- $rc = new \ReflectionClass('Teste');
- class ExecutorInterator implements Iterator
- {
- protected $elements = [];
- public function __construct(array $elements = [])
- {
- $this->elements = $elements;
- }
- public function current()
- {
- return current($this->elements);
- }
- public function next()
- {
- return next($this->elements);
- }
- public function key()
- {
- return key($this->elements);
- }
- public function valid()
- {
- return isset($this->elements[current($this->elements)]);
- }
- public function rewind()
- {
- return reset($this->elements);
- }
- public function count()
- {
- return count($this->elements);
- }
- public function add($element, $name = null)
- {
- $this->elements[] = $element;
- }
- public function remove($element)
- {
- foreach ($this->elements as $index => $e) {
- if ($e == $element) {
- unset($this->elements[$index]);
- }
- }
- }
- }
- class ExecutorCompostInterator extends ExecutorInterator
- {
- public function add($element, $name = null)
- {
- $this->elements[$name] = $element;
- }
- }
- class Executor
- {
- private $rc = null;
- private $di = null;
- private $params = [];
- private $mparams = [];
- /**
- * @return null
- */
- public function getRc()
- {
- return $this->rc;
- }
- /**
- * @param null $rc
- * @return Executor
- */
- public function setRc(ReflectionClass $rc)
- {
- $this->rc = $rc;
- return $this;
- }
- /**
- * @return null
- */
- public function getDi()
- {
- return $this->di;
- }
- /**
- * @param null $di
- * @return Executor
- */
- public function setDi(Di $di)
- {
- $this->di = $di;
- return $this;
- }
- function extractParamsOfMethods()
- {
- $methods = $this->getMethods();
- $params = new ExecutorCompostInterator();
- foreach ($methods as $method) {
- $params->add($method->name, $method->getParameters());
- }
- return $params;
- }
- function getMethods()
- {
- return $this->rc->getMethods(\ReflectionMethod::IS_PUBLIC);
- }
- function notEmpty($item)
- {
- return !empty($item);
- }
- public function prepareExecution($param, $index, $method)
- {
- $this->fillParameter(
- $this->filterUnnecessaryElements(
- $this->removeUnncessaryCharacters(
- $this->getValueFromInfo(
- $this->extractInfoParameters($method, $index)
- )
- )
- ), $this->mparams
- );
- }
- public function fillParameter($end, $param)
- {
- if ($this->hasType($end)) {
- $this->params[] =
- $this->di->get(reset($end));
- }
- if (!$this->hasType($end)) {
- $this->params[] = $param;
- }
- }
- public function hasType($param)
- {
- return count($param) == 2;
- }
- function filterUnnecessaryElements(array $end)
- {
- $end = array_filter($end, [$this, 'notEmpty']);
- return $end;
- }
- public function removeUnncessaryCharacters($output)
- {
- $end = explode(' ', str_replace(['[', ']', '<required>'], '', trim($output[0])));
- return $end;
- }
- public function getValueFromInfo($export)
- {
- preg_match('/\[.*\]/', $export, $output);
- return $output;
- }
- function extractInfoParameters(\ReflectionMethod $method, $index)
- {
- $refParam = new \ReflectionParameter([$method->class, $method->name], $index);
- $export = \ReflectionParameter::export(
- array(
- $refParam->getDeclaringClass()->name,
- $refParam->getDeclaringFunction()->name
- ),
- $refParam->name,
- true
- );
- return $export;
- }
- public function execute($methodName, $parameter = null)
- {
- $this->mparams = $parameter;
- $method = $this->getMethod($methodName);
- $instance = $this->rc->newInstance();
- $params = $this->extractParamsOfMethod($methodName);
- array_walk($params, [$this, 'prepareExecution'], $method);
- return $method->invokeArgs($instance, $this->params);
- }
- function getMethod($name)
- {
- return $this->rc->getMethod($name);
- }
- function extractParamsOfMethod($name)
- {
- return $this->getMethod($name)->getParameters();
- }
- }
- echo '<pre>';
- $router = new Router;
- $router->get('/', function () {
- echo 'start';
- });
- $router->get('/data', function () {
- echo 'get data';
- });
- $router->post('/data', function (Request $request) {
- echo 'post data';
- });
- $router->get('/datas/:var', function (Request $request) {
- echo 'datas ' . $request->var;
- });
- $router->get('/date/:show', function (Request $request) {
- echo 'date ' . $request->show;
- });
- $router->get('/data/:show', function (Request $request) {
- echo 'datA ' . $request->show;
- });
- $router->get('/data/:show/denovo', function (Request $request) {
- echo 'show: ' . $request->show;
- });
- $router->get('/data/:show/denovo/:marombada', 'Testes@home');
- $router->run($di);
Add Comment
Please, Sign In to add comment