Skip to content

Commit 2142270

Browse files
committed
backup added
1 parent 966a914 commit 2142270

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+5318
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
class App
6+
{
7+
protected static $container;
8+
9+
public static function setContainer($container)
10+
{
11+
static::$container = $container;
12+
}
13+
14+
public static function container()
15+
{
16+
return static::$container;
17+
}
18+
19+
public static function bind($key, $resolver)
20+
{
21+
static::container()->bind($key, $resolver);
22+
}
23+
24+
public static function resolve($key)
25+
{
26+
return static::container()->resolve($key);
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
class Authenticator
6+
{
7+
public function attempt($email, $password)
8+
{
9+
$user = App::resolve(Database::class)
10+
->query('select * from users where email = :email', [
11+
'email' => $email
12+
])->find();
13+
14+
if ($user) {
15+
if (password_verify($password, $user['password'])) {
16+
$this->login([
17+
'email' => $email
18+
]);
19+
20+
return true;
21+
}
22+
}
23+
24+
return false;
25+
}
26+
27+
public function login($user)
28+
{
29+
$_SESSION['user'] = [
30+
'email' => $user['email']
31+
];
32+
33+
session_regenerate_id(true);
34+
}
35+
36+
public function logout()
37+
{
38+
Session::destroy();
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
use Exception;
6+
7+
class Container
8+
{
9+
protected $bindings = [];
10+
11+
public function bind($key, $resolver)
12+
{
13+
$this->bindings[$key] = $resolver;
14+
}
15+
16+
/**
17+
* @throws Exception
18+
*/
19+
public function resolve($key)
20+
{
21+
if (!array_key_exists($key, $this->bindings)) {
22+
throw new Exception("No matching binding found for {$key}");
23+
}
24+
25+
$resolver = $this->bindings[$key];
26+
27+
return call_user_func($resolver);
28+
}
29+
}
Lines changed: 50 additions & 0 deletions
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 = '')
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Core\Middleware;
4+
5+
class Authenticated
6+
{
7+
public function handle()
8+
{
9+
if (! $_SESSION['user'] ?? false) {
10+
header('location: /');
11+
exit();
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Core\Middleware;
4+
5+
class Guest
6+
{
7+
public function handle()
8+
{
9+
if ($_SESSION['user'] ?? false) {
10+
header('location: /');
11+
exit();
12+
}
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Core\Middleware;
4+
5+
class Middleware
6+
{
7+
public const MAP = [
8+
'guest' => Guest::class,
9+
'auth' => Authenticated::class
10+
];
11+
12+
public static function resolve($key)
13+
{
14+
if (!$key) {
15+
return;
16+
}
17+
18+
$middleware = static::MAP[$key] ?? false;
19+
20+
if (!$middleware) {
21+
throw new \Exception("No matching middleware found for key '{$key}'.");
22+
}
23+
24+
(new $middleware)->handle();
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
use Core\Middleware\Authenticated;
6+
use Core\Middleware\Guest;
7+
use Core\Middleware\Middleware;
8+
9+
class Router
10+
{
11+
protected $routes = [];
12+
13+
public function add($method, $uri, $controller)
14+
{
15+
$this->routes[] = [
16+
'uri' => $uri,
17+
'controller' => $controller,
18+
'method' => $method,
19+
'middleware' => null
20+
];
21+
22+
return $this;
23+
}
24+
25+
public function get($uri, $controller)
26+
{
27+
return $this->add('GET', $uri, $controller);
28+
}
29+
30+
public function post($uri, $controller)
31+
{
32+
return $this->add('POST', $uri, $controller);
33+
}
34+
35+
public function delete($uri, $controller)
36+
{
37+
return $this->add('DELETE', $uri, $controller);
38+
}
39+
40+
public function patch($uri, $controller)
41+
{
42+
return $this->add('PATCH', $uri, $controller);
43+
}
44+
45+
public function put($uri, $controller)
46+
{
47+
return $this->add('PUT', $uri, $controller);
48+
}
49+
50+
public function only($key)
51+
{
52+
$this->routes[array_key_last($this->routes)]['middleware'] = $key;
53+
54+
return $this;
55+
}
56+
57+
public function route($uri, $method)
58+
{
59+
foreach ($this->routes as $route) {
60+
if ($route['uri'] === $uri && $route['method'] === strtoupper($method)) {
61+
Middleware::resolve($route['middleware']);
62+
63+
return require base_path('Http/controllers/' . $route['controller']);
64+
}
65+
}
66+
67+
$this->abort();
68+
}
69+
70+
public function previousUrl()
71+
{
72+
return $_SERVER['HTTP_REFERER'];
73+
}
74+
75+
protected function abort($code = 404)
76+
{
77+
http_response_code($code);
78+
79+
require base_path("views/{$code}.php");
80+
81+
die();
82+
}
83+
}

0 commit comments

Comments
 (0)