Skip to content

Commit ad36542

Browse files
committed
Added a QueryBuilder Class
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
1 parent ab645d7 commit ad36542

File tree

10 files changed

+454
-26
lines changed

10 files changed

+454
-26
lines changed

.envtemplate

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[DATABASE]
2+
DATABASE_TYPE = "mysql"
23
DATABASE_NAME = "dbname"
34
HOST_NAME = "localhost"
45
USER_NAME = ""

config/Database.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Database
88
{
99
public const DB = ENV["DATABASE"];
10+
public const DB_TYPE = self::DB['DATABASE_TYPE'];
1011
public const DB_NAME = self::DB['DATABASE_NAME'];
1112
public const DB_HOST_NAME = self::DB['HOST_NAME'];
1213
public const DB_USER_NAME = self::DB['USER_NAME'];

config/Debug.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Sapling\Config;
4+
5+
class Debug
6+
{
7+
// Display errors if true
8+
public const DEVELOPMENT = TRUE;
9+
// Don't load page if there are errors
10+
public const STRICT = TRUE;
11+
}

public/.htaccess

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ RewriteRule ^\.ht(.*)$ [L,F]
33
RewriteRule ^$ index.php [L]
44
# RewriteRule ^([a-zA-Z]+)$ index.php?controller=$1 [L]
55
# RewriteRule ^([a-zA-Z]+)/([a-zA-Z_]*)$ index.php?controller=$1&function=$2 [L]
6-
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z_]*)/?([/a-zA-Z0-9_\-]*)$ index.php?controller=$1&function=$2&parameters=$3 [L]
6+
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z_]*)/?([/a-zA-Z0-9_\-+%]*)$ index.php?controller=$1&function=$2&parameters=$3 [L]

src/App/Controller/Test.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Controller;
44

55
use Sapling\Controller\Controller as SaplingController;
6+
use Sapling\Functions\Url;
67

78
class Test extends SaplingController
89
{
@@ -20,20 +21,19 @@ public function table($data)
2021
$data['table'] = $this->TestModel->getAllTest();
2122
$this->loadView("table.php", $data);
2223
}
23-
public function insert()
24+
public function insert($data)
2425
{
25-
$this->TestModel->insertTest(['Mark', '09', 'PPC', 'PH']);
26+
$this->TestModel->insertTest(['name', 'phone', 'address', 'language'], [$data[0], $data[1], $data[2], $data[3]]);
27+
Url::redirect(Url::baseUrl() . "test/table");
2628
}
27-
public function delete()
29+
public function delete($data)
2830
{
29-
$this->TestModel->deleteTest('test_id', 3);
30-
}
31-
public function deleteMany()
32-
{
33-
$this->TestModel->deleteManyTest(['test_id', 'test_id'], [2, 4], false);
31+
$this->TestModel->deleteTest([$data[0]], [$data[1]]);
32+
Url::redirect(Url::baseUrl() . "test/table");
3433
}
3534
public function update()
3635
{
37-
$this->TestModel->updateTest(['language'], ['C++, PHP'], "test_id", 1);
36+
$this->TestModel->updateTest();
37+
$this->TestModel->getLastQuery();
3838
}
3939
}

src/App/Model/TestModel.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Model;
44

5-
use Sapling\Model\Model as SaplingModel;
5+
use Sapling\Model\QueryBuilder as SaplingModel;
66

77
class TestModel extends SaplingModel
88
{
@@ -12,24 +12,25 @@ public function __construct()
1212
}
1313
public function getAllTest()
1414
{
15-
// $statement = "SELECT * FROM test";
16-
// return $this->query($statement)->fetchAll();
17-
return $this->get("test")->fetchAll();
15+
$this->table("test");
16+
$this->order('test_id', false);
17+
return $this->get()->fetchAll();
1818
}
19-
public function insertTest(array $data)
19+
public function insertTest(array $columns, array $data)
2020
{
21-
$this->insert("test", $data);
21+
$this->table("test");
22+
$this->insert($columns, $data);
2223
}
23-
public function deleteTest(string $column, $value)
24+
public function deleteTest(array $columns, array $values)
2425
{
25-
$this->delete("test", $column, $value);
26+
$this->table("test");
27+
$this->whereMany($columns, $values);
28+
$this->delete();
2629
}
27-
public function deleteManyTest(array $columns, array $values, bool $use_and = true)
30+
public function updateTest()
2831
{
29-
$this->deleteMany("test", $columns, $values, $use_and);
32+
$this->table("test");
33+
$this->whereMany(['test_id'], [22]);
34+
$this->update(['name'], ['Angela']);
3035
}
31-
public function updateTest(array $update_column, array $update_value, string $where_column, $where_value)
32-
{
33-
$this->update("test", $update_column, $update_value, $where_column, $where_value);
34-
}
35-
}
36+
}

src/Sapling/Controller/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct()
2121
*
2222
* Loads php files in the view directory.
2323
*/
24-
public function loadView(string $view_dir, array $data)
24+
public function loadView(string $view_dir, ?array $data = null)
2525
{
2626
if (!preg_match("#^([a-zA-Z_\-/]+).php$#", $view_dir)) // String has no .php extension
2727
{

src/Sapling/Functions/Debug.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Sapling\Functions;
4+
5+
use Sapling\Config\Debug as ConfigDebug;
6+
7+
class Debug
8+
{
9+
private static $errors = ConfigDebug::DEVELOPMENT;
10+
private static $strict = ConfigDebug::STRICT;
11+
public static function debugClass(string $message)
12+
{
13+
if (self::$errors) {
14+
$array = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
15+
array_shift($array);
16+
array_pop($array);
17+
array_pop($array);
18+
$array = array_reverse($array);
19+
echo '<pre style="padding: 1em;border: 2px solid red;font-size: 1.2em;background-color:#EE9C96">';
20+
echo '<h1 style="margin: 0.3em; font-size: 1.5em">' . $message . '</h1>';
21+
echo "<span><strong>Stack Trace</strong></span>";
22+
for ($i = 0, $len = count($array); $i < $len; ++$i) {
23+
echo "\n " . $array[$i]['file'] . ". Line ({$array[$i]['line']}): {$array[$i]['class']}{$array[$i]['type']}{$array[$i]['function']}()";
24+
}
25+
echo "</pre>";
26+
if (self::$strict) {
27+
die();
28+
}
29+
}
30+
}
31+
}

src/Sapling/Functions/Url.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public static function resourceUrl(): string
2222
return self::baseUrl() . "resources/";
2323
}
2424

25+
public static function redirect(string $url): void
26+
{
27+
header('Location: ' . $url);
28+
}
29+
2530
public static function baseUrl(): string
2631
{
2732
self::createRequest();
@@ -67,6 +72,14 @@ public static function getUrlParameters(): array
6772
$parameters = array_filter($parameters, 'strlen');
6873
// Re-adjust index values, clearing empty parameters does not fix index values
6974
$parameters = array_values($parameters);
75+
// Replace + to space
76+
for ($i = 0, $len = count($parameters); $i < $len; ++$i) {
77+
for ($j = 0, $lenk = strlen($parameters[$i]); $j < $lenk; ++$j) {
78+
if ($parameters[$i][$j] === '+') {
79+
$parameters[$i][$j] = ' ';
80+
}
81+
}
82+
}
7083
}
7184
return $parameters;
7285
}

0 commit comments

Comments
 (0)