Skip to content

Instantly share code, notes, and snippets.

@Ronmi
Last active October 20, 2015 01:58
Show Gist options
  • Save Ronmi/274bdf115d8b4b5ea1a0 to your computer and use it in GitHub Desktop.
Save Ronmi/274bdf115d8b4b5ea1a0 to your computer and use it in GitHub Desktop.
PoC of router - compiled php
<?php
class FruitRouteKitGeneratedMux implements Fruit\RouteKit\Router
{
public function __construct()
{
}
private function dispatchGET($uri)
{
$arr = explode('/', $uri);
array_push($arr, array_shift($arr));
$state = 0;
while (count($arr) > 0) {
$part = array_shift($arr);
switch ($state) {
case 0: // root
if ($part == '') {$state = 1; break;}
if ($part == 'obj') {$state = 2; break;}
if ($part == 'initobj') {$state = 5; break;}
if ($part == 'cls') {$state = 7; break;}
if ($part == 'some') {$state = 9; break;}
if ($part == 'user') {$state = 11; break;}
throw new \Exception("no matching rule for url " . $part);
case 1: // /
if (count($arr) == 0) return (new \A\B\C())->methodA();
throw new \Exception("no matching rule for url " . $part);
case 2: // /obj
if ($part == 'methodA') {$state = 3; break;}
if ($part == 'methodB') {$state = 4; break;}
throw new \Exception("no matching rule for url " . $part);
case 3: // /obj/methodA
if (count($arr) == 0) return (new Obj())->methodA();
throw new \Exception("no matching rule for url " . $part);
case 4: // /obj/methodB
if (count($arr) == 0) return (new Obj())->methodB();
throw new \Exception("no matching rule for url " . $part);
case 5: // /initobj
if ($part == 'methodC') {$state = 6; break;}
throw new \Exception("no matching rule for url " . $part);
case 6: // /initobj/methodC
if (count($arr) == 0) return (new InitObj(1, 'abc'))->methodC();
throw new \Exception("no matching rule for url " . $part);
case 7: // /cls
if ($part == 'methodD') {$state = 8; break;}
throw new \Exception("no matching rule for url " . $part);
case 8: // /cls/methodD
if (count($arr) == 0) return \Cls::methodD();
throw new \Exception("no matching rule for url " . $part);
case 9: // /some
if ($part == 'funct') {$state = 10; break;}
throw new \Exception("no matching rule for url " . $part);
case 10: // /some/funct
if (count($arr) == 0) return some_funct();
throw new \Exception("no matching rule for url " . $part);
case 11: // /user
$state = 12;
$params[] = $part;
break;
case 12: // /user/[variable]
if ($part == 'email') {$state = 13; break;}
if (count($arr) == 0) return (new User())->Info($params[0]);
throw new \Exception("no matching rule for url " . $part);
case 13: // /user/[variable]/email
if (count($arr) == 0) return (new User())->Email($params[0]);
throw new \Exception("no matching rule for url " . $part);
}
}
throw new Exception('No matching rule for ' . $url);
}
public function dispatch($method, $uri)
{
$method = strtolower($method);
if ($method == 'get') {
$this->dispatchGET($uri);
}
}
}
return new FruitRouteKitGeneratedMux;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment