Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LucianoCharlesdeSouza/a0f708fe9cfe415a90b371100edaa7a8 to your computer and use it in GitHub Desktop.
Save LucianoCharlesdeSouza/a0f708fe9cfe415a90b371100edaa7a8 to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . '/../HomeController.php';
require __DIR__ . '/../ContatoController.php';
//http://localhost/projetox/public/teste/77
echo $uri = 'teste/77';
echo '</br>';
echo $rota = '/teste/{id}';
echo '</br>';
$callback = function($id){
echo 'eu sou uma rota com Clousure como callback com parametro no valor: '. $id;
};
echo '</br>';
$routes_get = [];
function pattern($rota)
{
$rota = implode('/', array_filter(explode('/', $rota)));
$rota = '/^' . str_replace('/', '\/', $rota) . '$/';
if (preg_match("/\{[A-Za-z0-9\_\-]{1,}\}/", $rota)) {
$rota = preg_replace("/\{[A-Za-z0-9\_\-]{1,}\}/", "[A-Za-z0-9\_\-]{1,}", $rota);
}
return $rota;
}
// var_dump(pattern($rota));
function strposArray(string $haystack, array $needles, int $offset = 0)
{
$result = false;
if (strlen($haystack) > 0 && count($needles) > 0) {
foreach ($needles as $element) {
$result = strpos($haystack, $element, $offset);
if ($result !== false) {
break;
}
}
}
return $result;
}
function getMetaTags($rota)
{
$result = [];
$needles = ['{', '[', '(', "\\"];
$rota = array_filter(explode('/', $rota));
foreach ($rota as $key => $element) {
$found = strposArray($element, $needles);
if ($found !== false) {
if (substr($element, 0, 1) === '{') {
$result[preg_filter('/([\{\}])/', '', $element)] = $key - 1;
} else {
$index = 'value_' . !empty($result) ? count($result) + 1 : 1;
array_merge($result, [$index => $key - 1]);
}
}
}
return count($result) > 0 ? $result : false;
}
// var_dump(getMetaTags($rota));
function getParamns($uri, $paramns)
{
$result = [];
$uri = array_filter(explode('/', $uri));
$countUri = count($uri);
if($countUri > 1){
foreach ($uri as $key => $value) {
if (in_array($key, $paramns)) {
$result[array_search($key, $paramns)] = $value;
}
}
}
return $result;
}
// var_dump(getParamns($uri, getMetaTags($rota)));
function get($rota, $callback, $uri)
{
return [
'pattern' => pattern($rota),
'callback' => $callback,
'paramns' => getParamns($uri, getMetaTags($rota))
];
}
// var_dump(get($rota, $callback, $uri));
function matchGet($uri, $routes_get)
{
foreach ($routes_get as $callback) {
if (preg_match($routes_get['pattern'], $uri, $matched_uri)) {
return [
'callback' => $routes_get['callback'],
'paramns' => $routes_get['paramns'],
'uri' => $matched_uri
];
}
}
return false;
}
// var_dump(matchGet($uri, get($rota, $callback, $uri)));
function dispach($callback, $params = [])
{
if(is_callable($callback['callback']))
{
return call_user_func_array($callback['callback'], array_values($params));
} elseif (is_string($callback['callback'])) {
if(!!strpos($callback['callback'], '@') !== false) {
$callback['callback'] = explode('@', $callback['callback']);
$controller = $callback['callback'][0];
$method = $callback['callback'][1];
$reflectionClass = new ReflectionClass($controller);
if($reflectionClass->isInstantiable() && $reflectionClass->hasMethod($method))
{
return call_user_func_array(array(new $controller, $method), array_values($params) );
} else {
throw new Exception("Erro ao despachar: controller não pode ser instanciado, ou método não existe");
}
}
}
throw new Exception("Erro ao despachar: método não implementado");
}
$callbacks = matchGet($uri, get($rota, $callback, $uri));
$paramns = $callbacks['paramns'];
dispach($callbacks, $paramns);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment