Skip to content

Commit

Permalink
Added a QueryBuilder Class
Browse files Browse the repository at this point in the history
Added a debug class
Added a new env variable in env file and template
Added redirect function in Url class
A + in url parameter is now treated as space
  • Loading branch information
ShiniDev committed Jun 26, 2021
1 parent ab645d7 commit ad36542
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 26 deletions.
1 change: 1 addition & 0 deletions .envtemplate
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[DATABASE]
DATABASE_TYPE = "mysql"
DATABASE_NAME = "dbname"
HOST_NAME = "localhost"
USER_NAME = ""
Expand Down
1 change: 1 addition & 0 deletions config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Database
{
public const DB = ENV["DATABASE"];
public const DB_TYPE = self::DB['DATABASE_TYPE'];
public const DB_NAME = self::DB['DATABASE_NAME'];
public const DB_HOST_NAME = self::DB['HOST_NAME'];
public const DB_USER_NAME = self::DB['USER_NAME'];
Expand Down
11 changes: 11 additions & 0 deletions config/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sapling\Config;

class Debug
{
// Display errors if true
public const DEVELOPMENT = TRUE;
// Don't load page if there are errors
public const STRICT = TRUE;
}
2 changes: 1 addition & 1 deletion public/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ RewriteRule ^\.ht(.*)$ [L,F]
RewriteRule ^$ index.php [L]
# RewriteRule ^([a-zA-Z]+)$ index.php?controller=$1 [L]
# RewriteRule ^([a-zA-Z]+)/([a-zA-Z_]*)$ index.php?controller=$1&function=$2 [L]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z_]*)/?([/a-zA-Z0-9_\-]*)$ index.php?controller=$1&function=$2&parameters=$3 [L]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z_]*)/?([/a-zA-Z0-9_\-+%]*)$ index.php?controller=$1&function=$2&parameters=$3 [L]
18 changes: 9 additions & 9 deletions src/App/Controller/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller;

use Sapling\Controller\Controller as SaplingController;
use Sapling\Functions\Url;

class Test extends SaplingController
{
Expand All @@ -20,20 +21,19 @@ public function table($data)
$data['table'] = $this->TestModel->getAllTest();
$this->loadView("table.php", $data);
}
public function insert()
public function insert($data)
{
$this->TestModel->insertTest(['Mark', '09', 'PPC', 'PH']);
$this->TestModel->insertTest(['name', 'phone', 'address', 'language'], [$data[0], $data[1], $data[2], $data[3]]);
Url::redirect(Url::baseUrl() . "test/table");
}
public function delete()
public function delete($data)
{
$this->TestModel->deleteTest('test_id', 3);
}
public function deleteMany()
{
$this->TestModel->deleteManyTest(['test_id', 'test_id'], [2, 4], false);
$this->TestModel->deleteTest([$data[0]], [$data[1]]);
Url::redirect(Url::baseUrl() . "test/table");
}
public function update()
{
$this->TestModel->updateTest(['language'], ['C++, PHP'], "test_id", 1);
$this->TestModel->updateTest();
$this->TestModel->getLastQuery();
}
}
31 changes: 16 additions & 15 deletions src/App/Model/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Model;

use Sapling\Model\Model as SaplingModel;
use Sapling\Model\QueryBuilder as SaplingModel;

class TestModel extends SaplingModel
{
Expand All @@ -12,24 +12,25 @@ public function __construct()
}
public function getAllTest()
{
// $statement = "SELECT * FROM test";
// return $this->query($statement)->fetchAll();
return $this->get("test")->fetchAll();
$this->table("test");
$this->order('test_id', false);
return $this->get()->fetchAll();
}
public function insertTest(array $data)
public function insertTest(array $columns, array $data)
{
$this->insert("test", $data);
$this->table("test");
$this->insert($columns, $data);
}
public function deleteTest(string $column, $value)
public function deleteTest(array $columns, array $values)
{
$this->delete("test", $column, $value);
$this->table("test");
$this->whereMany($columns, $values);
$this->delete();
}
public function deleteManyTest(array $columns, array $values, bool $use_and = true)
public function updateTest()
{
$this->deleteMany("test", $columns, $values, $use_and);
$this->table("test");
$this->whereMany(['test_id'], [22]);
$this->update(['name'], ['Angela']);
}
public function updateTest(array $update_column, array $update_value, string $where_column, $where_value)
{
$this->update("test", $update_column, $update_value, $where_column, $where_value);
}
}
}
2 changes: 1 addition & 1 deletion src/Sapling/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
*
* Loads php files in the view directory.
*/
public function loadView(string $view_dir, array $data)
public function loadView(string $view_dir, ?array $data = null)
{
if (!preg_match("#^([a-zA-Z_\-/]+).php$#", $view_dir)) // String has no .php extension
{
Expand Down
31 changes: 31 additions & 0 deletions src/Sapling/Functions/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Sapling\Functions;

use Sapling\Config\Debug as ConfigDebug;

class Debug
{
private static $errors = ConfigDebug::DEVELOPMENT;
private static $strict = ConfigDebug::STRICT;
public static function debugClass(string $message)
{
if (self::$errors) {
$array = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($array);
array_pop($array);
array_pop($array);
$array = array_reverse($array);
echo '<pre style="padding: 1em;border: 2px solid red;font-size: 1.2em;background-color:#EE9C96">';
echo '<h1 style="margin: 0.3em; font-size: 1.5em">' . $message . '</h1>';
echo "<span><strong>Stack Trace</strong></span>";
for ($i = 0, $len = count($array); $i < $len; ++$i) {
echo "\n " . $array[$i]['file'] . ". Line ({$array[$i]['line']}): {$array[$i]['class']}{$array[$i]['type']}{$array[$i]['function']}()";
}
echo "</pre>";
if (self::$strict) {
die();
}
}
}
}
13 changes: 13 additions & 0 deletions src/Sapling/Functions/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public static function resourceUrl(): string
return self::baseUrl() . "resources/";
}

public static function redirect(string $url): void
{
header('Location: ' . $url);
}

public static function baseUrl(): string
{
self::createRequest();
Expand Down Expand Up @@ -67,6 +72,14 @@ public static function getUrlParameters(): array
$parameters = array_filter($parameters, 'strlen');
// Re-adjust index values, clearing empty parameters does not fix index values
$parameters = array_values($parameters);
// Replace + to space
for ($i = 0, $len = count($parameters); $i < $len; ++$i) {
for ($j = 0, $lenk = strlen($parameters[$i]); $j < $lenk; ++$j) {
if ($parameters[$i][$j] === '+') {
$parameters[$i][$j] = ' ';
}
}
}
}
return $parameters;
}
Expand Down
Loading

0 comments on commit ad36542

Please sign in to comment.