Skip to content

Instantly share code, notes, and snippets.

@ikariiin
Created March 4, 2016 06:43
Show Gist options
  • Save ikariiin/1ea8e88d818a2dfec676 to your computer and use it in GitHub Desktop.
Save ikariiin/1ea8e88d818a2dfec676 to your computer and use it in GitHub Desktop.
<?php
class Syntax_Exception extends Exception {
private $data = array();
public function setData(int $line_number, string $token_number) {
$this->data["line_number"] = $line_number;
$this->data["token_number"] = $token_number;
}
public $meta = array(
"type" => "syntax_error"
);
public function getData():array {
return $this->data;
}
}
class Router_Parser {
private $file_name;
private $file_content;
private $tokens;
private $_router_config = array();
public function __construct($router = "rules.router") {
$this->file_name = $router;
if(($content = file_get_contents($router)) === false) {
throw new Exception("{$router} file does not exist.");
} else {
$this->file_content = $content;
}
}
public function process() {
return $this->_Parser();
}
private function _Parser() {
$tokens = array();
$local_content = trim($this->file_content);
$lines = explode(";", $local_content);
$lines_count = count($lines);
for($i = 0; $i < $lines_count; $i++) {
// Get Current Line
$current_line = trim($lines[$i]);
$tokens_pre = explode(" ", $current_line);
$tokens[$i] = $tokens_pre;
}
$this->tokens = $tokens;
// All Done.
// Now Got To Process The tokens.
for($j = 0; $j < count($this->tokens); $j++) {
if($this->tokens[$j][0] == "files") {
// This is a files rule.
/*
* Currently Only Supporting line-break parameter.
*/
if($this->tokens[$j][1] == "line-break") {
// Okay So It Is a line break config
$break = $this->tokens[$j][2];
$this->_router_config["file_line_break"] = $break;
if(isset($this->tokens[$j][3])) {
$syntax_exp = new Syntax_Exception("Syntax Error. Expected ';' but got {$this->tokens[$j][3]}");
$syntax_exp->setData($j, $this->tokens[$j][3]);
return $syntax_exp;
}
} else {
// Throw a syntax error at the user
$syntax_exp = new Syntax_Exception("Syntax Error. Expected 'line-break' but got {$this->tokens[$j][1]}");
$syntax_exp->setData($j, $this->tokens[$j][1]);
return $syntax_exp;
}
} elseif($this->tokens[$j][0] == "http-header") {
// HTTP-HEADER MANIPULATION
if($this->tokens[$j][1] == "set") {
$key = $this->tokens[$j][2];
$val = $this->tokens[$j][3];
$this->_router_config["http-header"][$key] = $val;
} else {
$syntax_exp = new Syntax_Exception("Syntax Error. Expected 'set' but got {$this->tokens[$j][1]}");
$syntax_exp->setData($j, $this->tokens[$j][1]);
return $syntax_exp;
}
} elseif($this->tokens[$j][0] == "#") {
// Routing Instruction
if($this->tokens[$j][1] != "route") {
$syntax_exp = new Syntax_Exception("Syntax Error. Expected 'route' but got {$this->tokens[$j][1]}");
$syntax_exp->setData($j, $this->tokens[$j][1]);
return $syntax_exp;
}
$param = $this->tokens[$j][2];
if($this->tokens[$j][3] != "to") {
$syntax_exp = new Syntax_Exception("Syntax Error. Expected 'to' but got {$this->tokens[$j][1]}");
$syntax_exp->setData($j, $this->tokens[$j][1]);
return $syntax_exp;
}
if($this->tokens[$j][4] == "file") {
// To route the request to a file
$point_towards = $this->tokens[$j][5];
$type = "file";
$content = "";
} elseif($this->tokens[$j][5] == "static-string") {
$point_towards = "SELF";
$type = "static-string";
$content = $this->tokens[$j][6];
} else {
$syntax_exp = new Syntax_Exception("Syntax Error. Expected 'to' but got {$this->tokens[$j][1]}");
$syntax_exp->setData($j, $this->tokens[$j][1]);
return $syntax_exp;
}
$this->_router_config["routes"][][$param] = array(
"point-towards" => $point_towards,
"type" => $type,
"content" => $content
);
}
}
return true;
}
public function getConf() {
$object = new stdClass();
$object->conf = $this->_router_config;
$object->farewell = array(
"router" => "Custom Router Built By RagedWizard",
"router file" => $this->file_name,
"gift" => "USEFULLY USE THIS ROUTES :'-| I MADE IT WITH A LOT OF PATIENCE AND DO BE SUCCESSFUL!"
);
return $object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment