diff --git a/src/Database.php b/src/Database.php new file mode 100644 index 0000000..42f7fd4 --- /dev/null +++ b/src/Database.php @@ -0,0 +1,75 @@ +host = $dbinfo['host']; + $this->user = $dbinfo['user']; + $this->pass = $dbinfo['pass']; + $this->dbname = $dbinfo['name']; + + $this->connect(); + } + + private function connect() { + $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname); + + if ($this->conn->connect_error) { + die("Connection failed: " . $this->conn->connect_error); + } + } + + public function query($query) { + $stmt = $this->conn->prepare($query); + $stmt->execute(); + + return $stmt->get_result(); + } + + public function getRow($query) { + $result = $this->query($query); + return $result->fetch_assoc(); + } + + public function debugGetRow($query) { + echo "

Debug GetRow Query

"; + echo "
Query: $query
"; + $result = $this->query($query); + $row = $result->fetch_assoc(); + + echo "
";
+        print_r($row);
+        echo "
"; + + die(); + } + + public function getRows($query) { + $result = $this->query($query); + return $result->fetch_all(MYSQLI_ASSOC); + } + + public function debugGetRows($query) { + echo "

Debug GetRows Query

"; + echo "
Query: $query
"; + + $result = $this->query($query); + $rows = $result->fetch_all(MYSQLI_ASSOC); + + echo "
";
+        print_r($rows);
+        echo "
"; + + die(); + } + + public function close() { + $this->conn->close(); + } +} \ No newline at end of file diff --git a/src/Router.php b/src/Router.php index 98ae2fb..e439b27 100644 --- a/src/Router.php +++ b/src/Router.php @@ -4,22 +4,30 @@ class Router { public $routes = []; public $query = []; public $path; - public $controllerPath = BASEPATH . '/App/controllers/404.php'; + public $controller; + public $controllerPath; + public $parameters = []; public function __construct() { - $this->loadRoutes(); - $this->preparePath(); - $this->prepareQuery(); - $this->setRouteFile(); + $this->routes = $this->loadRoutes(); + $this->path = $this->preparePath(); + $this->query = $this->prepareQuery(); + $this->controller = $this->findController(); + $this->controllerPath = $this->setRouteFile(); } private function loadRoutes() { require_once( BASEPATH . '/App/routes.php'); - $this->routes = $routes; + return $routes; } private function preparePath() { - $this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); + $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); + $path = rtrim($path, '/'); // remove trailing slash + //remove anything after and including ampersand + $path = preg_replace('/&.+$/', '', $path); + + return $path; } private function prepareQuery() { @@ -28,21 +36,74 @@ class Router { if (isset($parsedUri['query'])) { parse_str($parsedUri['query'], $queryArray); } - $this->query = $queryArray; + return $queryArray; } - private function setRouteFile() { - foreach ($this->routes as $key => $value) { - if ( $this->path == $key) { - $theFile = BASEPATH . '/App/controllers/' . $value['file'] . '.php'; + private function findController() { - //check if the file exists - if (file_exists($theFile)) { - $this->controllerPath = $theFile; + // one to one match + if (array_key_exists($this->path, $this->routes)) { + return $this->routes[$this->path]['file']; + } + + foreach ($this->routes as $key => $value) { + // Check if key contains a curly bracket, if not continue. We already checked above. + if (!strpos($key, '{')) continue; + + // Remove everything after the curly bracket, from key + $keyPath = substr($key, 0, strpos($key, '{')); + + //see if keyPath matches the first characters of $this->path, only the first characters have to match + if (strpos($this->path, $keyPath) === 0) { + + // We have a potential match. Now check if the parameter count is equal + $keyParams = explode('/', $key); + $pathParams = explode('/', $this->path); + $keyParamCount = count($keyParams); + $pathParamCount = count($pathParams); + if ($keyParamCount === $pathParamCount) { + for ($i=0; $i < $pathParamCount; $i++) { + if (strpos($keyParams[$i], '{') !== false) { + $keyParams[$i] = substr($keyParams[$i], 1, -1); + $this->parameters[$keyParams[$i]] = $pathParams[$i]; + return $this->routes[$key]['file']; + } + } } - break; } } + + return '404'; + + } + + // checks if the file exists, sets file path + private function setRouteFile() { + + $cp = BASEPATH . '/App/controllers/' . $this->controller . '.php'; + + if (file_exists($cp)) { + return $cp; + } else { + return BASEPATH . '/App/controllers/404.php'; + } + + } + + public function debug() { + echo '

Debugging Router

'; + echo '

Url Path

'; + echo $this->path . '
'; + echo '

ControllerPath

'; + echo $this->controllerPath; + echo '

Parameters

'; + echo '
';
+        print_r($this->parameters);
+        echo '
'; + echo '

Routes Variable

';
+        print_r($this->routes);
+        echo '
'; + die(); } } \ No newline at end of file diff --git a/src/novaconium.php b/src/novaconium.php index d79524d..85f87dd 100644 --- a/src/novaconium.php +++ b/src/novaconium.php @@ -1,5 +1,6 @@ debug(); require_once($router->controllerPath);