Skip to content

Commit

Permalink
Fix deprecated dynamic props; allow DB port
Browse files Browse the repository at this point in the history
  • Loading branch information
susanBuck committed Dec 4, 2023
1 parent 4687d41 commit 1edd859
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
19 changes: 10 additions & 9 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ public function __construct($context = 'web')
{
# Define $app as $this because it’s used in config.php
$app = $this;

# Initialize Dotenv
try {
$dotenv = Dotenv::createImmutable(DOC_ROOT);
$dotenv->load();
} catch(InvalidPathException $e) {
dump($e->getMessage());
}

# Load config
$config = include(DOC_ROOT . 'config.php');
$this->dotAccessConfig = new Data($config);

# Set up error reporting
# (TODO: Make this environment specific, or leave always on for learning purposes?)
ini_set('display_errors', 1);
Expand All @@ -67,7 +67,7 @@ public function __construct($context = 'web')

# Load routes
$this->routes = include DOC_ROOT . 'routes.php';

# Initialize Blade
$this->blade = new Blade(DOC_ROOT . '/views', DOC_ROOT . '/cache');
}
Expand Down Expand Up @@ -115,8 +115,9 @@ public function db()
$username = $this->env('DB_USERNAME');
$password = $this->env('DB_PASSWORD');
$charset = $this->env('DB_CHARSET', 'utf8mb4');
$port = $this->env('DB_PORT', '3306');

$this->db = new Database($host, $database, $username, $password, $charset);
$this->db = new Database($host, $database, $username, $password, $charset, $port);
}
return $this->db;
}
Expand Down Expand Up @@ -200,7 +201,7 @@ public function path(string $path)
{
return DOC_ROOT . $path;
}

/**
* Redirect to a given path
* Will optionally persist a set of data to the session
Expand Down Expand Up @@ -235,7 +236,7 @@ public function route()
$this->sessionSet($this->sessionPrevious, $fullUrl);

# Initialize Controller and invoke method
$controllerName = "App\Controllers\\".$this->routes[$path][0];
$controllerName = "App\Controllers\\" . $this->routes[$path][0];
$controller = new $controllerName($this);
$method = $this->routes[$path][1];
return $controller->$method();
Expand Down Expand Up @@ -282,7 +283,7 @@ public function validate(array $rules)
$validator = new Validate($rules, $this->inputAll());

$errors = $validator->validate();

# If there are errors...
if (count($errors) > 0) {
# Store the errors
Expand All @@ -291,7 +292,7 @@ public function validate(array $rules)
# Redirect to previous URL, persisting the input into the session
# so it can be retrieved via `old` method
$this->redirect($this->previousUrl, $this->inputAll());

die();
}
}
Expand Down
23 changes: 12 additions & 11 deletions src/Database.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace E2;

class Database
Expand All @@ -8,9 +9,9 @@ class Database
/**
* Establish a PDO connection
*/
public function __construct($host, $database, $username, $password, $charset)
public function __construct($host, $database, $username, $password, $charset, $port)
{
$dsn = "mysql:host=$host;dbname=$database;charset=$charset";
$dsn = "mysql:host=$host;dbname=$database;charset=$charset;port=$port";
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
Expand Down Expand Up @@ -43,7 +44,7 @@ public function run($statement, $data = null)
*/
public function all($table)
{
return $this->run("SELECT * FROM ".$table." ORDER BY id DESC")->fetchAll();
return $this->run("SELECT * FROM " . $table . " ORDER BY id DESC")->fetchAll();
}

/**
Expand All @@ -54,7 +55,7 @@ public function insert($table, $data = [])
{
$fields = array_keys($data);

$sql = "INSERT INTO " . $table . "(" . implode(', ', $fields).") values (:" . implode(', :', $fields) . ")";
$sql = "INSERT INTO " . $table . "(" . implode(', ', $fields) . ") values (:" . implode(', :', $fields) . ")";

$this->run($sql, $data);

Expand All @@ -67,11 +68,11 @@ public function insert($table, $data = [])
public function findByColumn($table, $column, $operator, $value)
{
$sql = "SELECT * FROM `" . $table . "` WHERE `" . $column . "` " . $operator . " :" . $column;

$statement = $this->run($sql, [
$column => $value
]);

return ($statement) ? $statement->fetchAll() : null;
}

Expand All @@ -81,7 +82,7 @@ public function findByColumn($table, $column, $operator, $value)
public function findById($table, $id)
{
$sql = "SELECT * FROM " . $table . " WHERE id = :id";

$statement = $this->run($sql, ['id' => $id]);

$results = $statement->fetch();
Expand All @@ -104,15 +105,15 @@ public function createTable($table, $columns)
# Set up table with auto-incremending primary key `id`
$sql .= 'id int NOT NULL AUTO_INCREMENT,';
$sql .= 'PRIMARY KEY (id), ';

foreach ($columns as $name => $type) {
$sql .= $name . ' ' . $type . ',';
}

$sql = rtrim($sql, ',').') ENGINE=InnoDB DEFAULT CHARSET=utf8;';
$sql = rtrim($sql, ',') . ') ENGINE=InnoDB DEFAULT CHARSET=utf8;';

$this->run($sql, []);

return $sql;
}
}
}
8 changes: 6 additions & 2 deletions src/Validate.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

namespace E2;

class Validate
{
private $fieldsToValidate;
private $data;

public function __construct($fieldsToValidate, $data)
{
$this->fieldsToValidate = $fieldsToValidate;
Expand All @@ -29,7 +33,7 @@ public function validate()
foreach ($rules as $rule) {
# Get the value for this field from the request
$value = $this->data[$fieldName];

# Handle any parameters with the rule, e.g. max:99
$parameter = null;
if (strstr($rule, ':')) {
Expand Down Expand Up @@ -224,4 +228,4 @@ protected function maxMessage($parameter)
{
return 'must be less than or equal to ' . $parameter;
}
}
}

0 comments on commit 1edd859

Please sign in to comment.