Skip to content

Commit

Permalink
🎨 switched http and auth methods to static calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Feb 26, 2021
1 parent 9184651 commit 02e85c6
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 248 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
<!-- markdownlint-disable no-duplicate-header -->
# Changelog

## v2.4.3 - 🎋 Giant Cane Grass - 26th February 2021

### Added

- Updated `Leaf\Db` and `Leaf\Auth` to throw dev errors to Leaf's error handler for better error reporting

### Fixed

- Organized methods in `Leaf\FS`

### Changed

- Made `Leaf\Http\Response` static
- Made `Leaf\Http\Request` static

### Removed

- No removals

## v2.4.2 - 🥬 Desert Wishbone-bush - 3rd February 2021

This version of Leaf continues the goal of making Leaf features more flexible and increasing usability.
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ $auth = new Leaf\Auth;

$auth->connect("host", "user", "pass", "db name");

// Base example
$app->get('/', function() use($app) {
$app->response()->respond("My first Leaf app");
});

// Full login example
$app->post('/auth/login', function() use($app, $auth) {
$credentials = $app->request()->get(["username", "password"]);

$user = $auth->login("users", $credentials, [
"username" => ["username", "min:3"],
"password" => ["text", "NoSpaces"]
"username" => ["username", "max:15"],
"password" => ["text", "NoSpaces", "min:8"],
]);

if (!$user) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"name": "Michael Darko",
"email": "[email protected]",
"homepage": "https://mychi.netlify.com",
"homepage": "https://mychi.netlify.app",
"role": "Developer"
}
],
Expand Down
16 changes: 8 additions & 8 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,22 @@ public static function guard($type)
{
static::experimental("guard");

if (is_array($type)) {
if (isset($type["hasAuth"])) {
$type = $type["hasAuth"] ? 'auth' : 'guest';
}
}

if (!static::config("USE_SESSION")) {
if ($type === 'guest' && static::user()) {
(new Http\Response)->throwErr("You can't view this page while you're logged in!");
(new Http\Response)->throwErr(["auth" => "You can't view this page while you're logged in!"]);
}

if ($type === 'auth' && !static::user()) {
return;
}
}

if (is_array($type)) {
if (isset($type["hasAuth"])) {
$type = $type["hasAuth"] ? 'auth' : 'guest';
}
}

if ($type === 'guest' && static::session()) {
exit(header("location: " . static::config("GUARD_HOME"), true, 302));
}
Expand Down Expand Up @@ -525,7 +525,7 @@ public static function update(string $table, array $credentials, array $where, a
if (count($uniques) > 0) {
foreach ($uniques as $unique) {
if (!isset($credentials[$unique])) {
(new Http\Response)->throwErr(["error" => "$unique not found in credentials."]);
trigger_error("$unique not found in credentials: " . json_encode($credentials));
}

$data = static::$db->select($table)->where($unique, $credentials[$unique])->fetchAssoc();
Expand Down
10 changes: 2 additions & 8 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ class Db
*/
protected $form;

/**
* Leaf Response Module
*/
protected $response;

/**
* List of methods called
*/
Expand All @@ -70,7 +65,6 @@ class Db
public function __construct($host = null, $user = null, $password = null, $dbname = null)
{
$this->form = new Form;
$this->response = new Http\Response;

if ($host != null || $user != null || $password != null || $dbname != null) {
$this->connect($host, $user, $password, $dbname);
Expand Down Expand Up @@ -535,7 +529,7 @@ public function bind(...$bindings): self
public function execute($paramTypes = null)
{
if ($this->connection === null) {
$this->response->throwErr("Couldn't establish database connection. Call the connect() method, or check your database");
trigger_error("Couldn't establish database connection. Call the connect() method, or check your database");
}

if (count($this->errorsArray) > 0) return null;
Expand All @@ -561,7 +555,7 @@ public function execute($paramTypes = null)
if (count($uniques) > 0 && ($this->queryData["type"] != "select" || $this->queryData["type"] != "delete")) {
foreach ($uniques as $unique) {
if (!isset($paramValues[$unique])) {
$this->response->throwErr(["error" => "$unique not found, Add $unique to your \$db->add items or check your spelling."]);
trigger_error("$unique not found, Add $unique to your \$db->add items or check your spelling.");
}

if (mysqli_fetch_object($this->connection->query("SELECT * FROM {$this->queryData["table"]} WHERE $unique = '$paramValues[$unique]'"))) {
Expand Down
Loading

0 comments on commit 02e85c6

Please sign in to comment.