Skip to content

Commit 9bf8684

Browse files
committed
-- Introduce namespace
1 parent 6575300 commit 9bf8684

28 files changed

+1251
-4
lines changed

.vscode/launch.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
"program": "${file}",
1818
"cwd": "${fileDirname}",
1919
"port": 0,
20-
"runtimeArgs": [
21-
"-dxdebug.start_with_request=yes"
22-
],
20+
"runtimeArgs": ["-dxdebug.start_with_request=yes"],
2321
"env": {
2422
"XDEBUG_MODE": "debug,develop",
2523
"XDEBUG_CONFIG": "client_port=${port}"
@@ -45,4 +43,4 @@
4543
}
4644
}
4745
]
48-
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
use PDO;
6+
7+
class Database
8+
{
9+
public $connection;
10+
public $statement;
11+
12+
public function __construct($config, $username = 'root', $password = 'db_password')
13+
{
14+
$dsn = 'mysql:' . http_build_query($config, '', ';');
15+
16+
$this->connection = new PDO($dsn, $username, $password, [
17+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
18+
]);
19+
}
20+
21+
public function query($query, $params = [])
22+
{
23+
$this->statement = $this->connection->prepare($query);
24+
25+
$this->statement->execute($params);
26+
27+
return $this;
28+
}
29+
30+
public function get()
31+
{
32+
return $this->statement->fetchAll();
33+
}
34+
35+
public function find()
36+
{
37+
return $this->statement->fetch();
38+
}
39+
40+
public function findOrFail()
41+
{
42+
$result = $this->find();
43+
44+
if (! $result) {
45+
abort();
46+
}
47+
48+
return $result;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
class Response {
6+
const NOT_FOUND = 404;
7+
const FORBIDDEN = 403;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
class Validator
6+
{
7+
public static function string($value, $min = 1, $max = INF)
8+
{
9+
$value = trim($value);
10+
11+
return strlen($value) >= $min && strlen($value) <= $max;
12+
}
13+
14+
public static function email($value)
15+
{
16+
return filter_var($value, FILTER_VALIDATE_EMAIL);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
function dd($value)
4+
{
5+
echo "<pre>";
6+
var_dump($value);
7+
echo "</pre>";
8+
9+
die();
10+
}
11+
12+
function urlIs($value)
13+
{
14+
return $_SERVER['REQUEST_URI'] === $value;
15+
}
16+
17+
function authorize($condition, $status = Response::FORBIDDEN)
18+
{
19+
if (!$condition) {
20+
abort($status);
21+
}
22+
}
23+
24+
function base_path($path)
25+
{
26+
return BASE_PATH . $path;
27+
}
28+
29+
function view($path, $attributes = [])
30+
{
31+
extract($attributes);
32+
33+
require base_path('views/' . $path);
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
function routeToController($uri, $routes) {
4+
if (array_key_exists($uri, $routes)) {
5+
require base_path($routes[$uri]);
6+
} else {
7+
abort();
8+
}
9+
}
10+
11+
function abort($code = 404) {
12+
http_response_code($code);
13+
14+
require base_path("views/{$code}.php");
15+
16+
die();
17+
}
18+
19+
$routes = require base_path('routes.php');
20+
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
21+
22+
routeToController($uri, $routes);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'database' => [
5+
// 'host' => 'localhost',
6+
'host' => '127.0.0.1',
7+
'port' => 3306,
8+
'dbname' => 'myapp',
9+
'charset' => 'utf8mb4'
10+
],
11+
12+
//
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
view("about.view.php", [
4+
'heading' => 'About Us',
5+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
view("contact.view.php", [
4+
'heading' => 'Contact Us',
5+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
view("index.view.php", [
4+
'heading' => 'Home',
5+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Core\Database;
4+
use Core\Validator;
5+
6+
$config = require base_path('config.php');
7+
$db = new Database($config['database']);
8+
9+
$errors = [];
10+
11+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
12+
if (! Validator::string($_POST['body'], 1, 1000)) {
13+
$errors['body'] = 'A body of no more than 1,000 characters is required.';
14+
}
15+
16+
if (empty($errors)) {
17+
$db->query('INSERT INTO notes(body, user_id) VALUES(:body, :user_id)', [
18+
'body' => $_POST['body'],
19+
'user_id' => 1
20+
]);
21+
}
22+
}
23+
24+
view("notes/create.view.php", [
25+
'heading' => 'Create Note',
26+
'errors' => $errors
27+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Core\Database;
4+
5+
$config = require base_path('config.php');
6+
$db = new Database($config['database']);
7+
8+
$notes = $db->query('select * from notes where user_id = 1')->get();
9+
10+
view("notes/index.view.php", [
11+
'heading' => 'My Notes',
12+
'notes' => $notes
13+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Core\Database;
4+
5+
$config = require base_path('config.php');
6+
$db = new Database($config['database']);
7+
8+
$currentUserId = 1;
9+
10+
$note = $db->query('select * from notes where id = :id', [
11+
'id' => $_GET['id']
12+
])->findOrFail();
13+
14+
authorize($note['user_id'] === $currentUserId);
15+
16+
view("notes/show.view.php", [
17+
'heading' => 'Note',
18+
'note' => $note
19+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
const BASE_PATH = __DIR__.'/../';
4+
5+
require BASE_PATH.'Core/functions.php';
6+
7+
spl_autoload_register(function ($class) {
8+
// Core\Database
9+
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
10+
11+
require base_path("{$class}.php");
12+
});
13+
14+
require base_path('Core/router.php');
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'/' => 'controllers/index.php',
5+
'/about' => 'controllers/about.php',
6+
'/notes' => 'controllers/notes/index.php',
7+
'/note' => 'controllers/notes/show.php',
8+
'/notes/create' => 'controllers/notes/create.php',
9+
'/contact' => 'controllers/contact.php',
10+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php require('partials/head.php') ?>
2+
<?php require('partials/nav.php') ?>
3+
4+
<main>
5+
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
6+
<h1 class="text-2xl font-bold">You are not authorized to view this page.</h1>
7+
8+
<p class="mt-4">
9+
<a href="/" class="text-blue-500 underline">Go back home.</a>
10+
</p>
11+
</div>
12+
</main>
13+
14+
<?php require('partials/footer.php') ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php require('partials/head.php') ?>
2+
<?php require('partials/nav.php') ?>
3+
4+
<main>
5+
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
6+
<h1 class="text-2xl font-bold">Sorry. Page Not Found.</h1>
7+
8+
<p class="mt-4">
9+
<a href="/" class="text-blue-500 underline">Go back home.</a>
10+
</p>
11+
</div>
12+
</main>
13+
14+
<?php require('partials/footer.php') ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php require('partials/head.php') ?>
2+
<?php require('partials/nav.php') ?>
3+
<?php require('partials/banner.php') ?>
4+
5+
<main>
6+
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
7+
<p>Hello. Welcome to the about page.</p>
8+
</div>
9+
</main>
10+
11+
<?php require('partials/footer.php') ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php require('partials/head.php') ?>
2+
<?php require('partials/nav.php') ?>
3+
<?php require('partials/banner.php') ?>
4+
5+
<main>
6+
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
7+
<p>Hello. Welcome to the contact page.</p>
8+
</div>
9+
</main>
10+
11+
<?php require('partials/footer.php') ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php require('partials/head.php') ?>
2+
<?php require('partials/nav.php') ?>
3+
<?php require('partials/banner.php') ?>
4+
5+
<main>
6+
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
7+
<p>Hello. Welcome to the home page.</p>
8+
</div>
9+
</main>
10+
11+
<?php require('partials/footer.php') ?>

0 commit comments

Comments
 (0)