Skip to content

Commit

Permalink
Fix Dotenv loading
Browse files Browse the repository at this point in the history
  • Loading branch information
susanBuck committed Nov 1, 2022
1 parent a6367da commit ebbb233
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
30 changes: 21 additions & 9 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace E2;

use Jenssegers\Blade\Blade;
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidPathException;
use Dflydev\DotAccessData\Data;

class App
{
/**
Expand All @@ -27,10 +32,18 @@ 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 \Dflydev\DotAccessData\Data($config);
$this->dotAccessConfig = new Data($config);

# Set up error reporting
# (TODO: Make this environment specific, or leave always on for learning purposes?)
Expand All @@ -56,7 +69,7 @@ public function __construct($context = 'web')
$this->routes = include DOC_ROOT.'routes.php';

# Initialize Blade
$this->blade = new \Jenssegers\Blade\Blade(DOC_ROOT . '/views', DOC_ROOT . '/cache');
$this->blade = new Blade(DOC_ROOT . '/views', DOC_ROOT . '/cache');
}

/**
Expand All @@ -77,7 +90,7 @@ public function console($args)
$method = $args[2];

# Set up command
$commandName = "App\Commands\\".$commandName."Command";
$commandName = "App\Commands\\" . $commandName . "Command";
$command = new $commandName($this);

dump("Executing $commandName@$method");
Expand All @@ -103,7 +116,7 @@ public function db()
$password = $this->env('DB_PASSWORD');
$charset = $this->env('DB_CHARSET', 'utf8mb4');

$this->db = new Database($host, $database, $username, $password, 'utf8mb4');
$this->db = new Database($host, $database, $username, $password, $charset);
}
return $this->db;
}
Expand All @@ -113,8 +126,7 @@ public function db()
*/
public function env(string $name, $default = null)
{
# Note: getenv fill return `false`, not null, if a value does not exist
return getenv($name) != false ? getenv($name) : $default;
return $_ENV[$name] ?? $default;
}

/**
Expand Down Expand Up @@ -200,15 +212,15 @@ public function redirect($path, $data = null)
$this->sessionSet($this->sessionRedirect, $data);
}

header('Location: '.$path);
header('Location: ' . $path);
}

/**
* Parses current url, returning a matching route if it exists
*/
public function route()
{
$fullUrl = '/'.substr($_SERVER['REQUEST_URI'], 1);
$fullUrl = '/' . substr($_SERVER['REQUEST_URI'], 1);
$parsedUrl = parse_url($fullUrl);
$path = $parsedUrl['path'];

Expand Down
6 changes: 3 additions & 3 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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 @@ -64,7 +64,7 @@ public function insert($table, $data = [])
/**
* Return all rows where the given $column matches the given $value
*/
public function findByColumn($table, $column, $operator = '=', $value)
public function findByColumn($table, $column, $operator, $value)
{
$sql = "SELECT * FROM `" . $table . "` WHERE `" . $column . "` " . $operator . " :" . $column;

Expand Down Expand Up @@ -115,4 +115,4 @@ public function createTable($table, $columns)

return $sql;
}
}
}

0 comments on commit ebbb233

Please sign in to comment.