Skip to content

Commit 98f7e04

Browse files
committed
Update
1 parent 54937d6 commit 98f7e04

File tree

10 files changed

+6593
-263
lines changed

10 files changed

+6593
-263
lines changed

app/App.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,58 @@
44

55
namespace App;
66

7+
use App\Contracts\EmailValidationInterface;
78
use App\Exceptions\RouteNotFoundException;
9+
use App\Services\Emailable;
10+
use App\Services\AbstractApi;
11+
use Dotenv\Dotenv;
12+
use Illuminate\Container\Container;
13+
use Illuminate\Database\Capsule\Manager as Capsule;
14+
use Illuminate\Events\Dispatcher;
15+
use Symfony\Component\Mailer\MailerInterface;
816

917
class App
1018
{
11-
private static DB $db;
19+
private Config $config;
1220

13-
public function __construct(protected Router $router, protected array $request, protected Config $config)
21+
public function __construct(
22+
protected Container $container,
23+
protected ?Router $router = null,
24+
protected array $request = [],
25+
) {
26+
}
27+
28+
public function initDb(array $config)
1429
{
15-
static::$db = new DB($config->db ?? []);
30+
$capsule = new Capsule();
31+
32+
$capsule->addConnection($config);
33+
$capsule->setEventDispatcher(new Dispatcher($this->container));
34+
$capsule->setAsGlobal();
35+
$capsule->bootEloquent();
1636
}
1737

18-
public static function db(): DB
38+
public function boot(): static
1939
{
20-
return static::$db;
40+
$dotenv = Dotenv::createImmutable(dirname(__DIR__));
41+
$dotenv->load();
42+
43+
$this->config = new Config($_ENV);
44+
45+
$this->initDb($this->config->db);
46+
47+
$this->container->bind(MailerInterface::class, fn() => new CustomMailer($this->config->mailer['dsn']));
48+
49+
$this->container->bind(
50+
EmailValidationInterface::class,
51+
fn() => new Emailable\EmailValidationService($this->config->apiKeys['emailable'])
52+
);
53+
//$this->container->bind(
54+
// EmailValidationInterface::class,
55+
// fn() => new AbstractApi\EmailValidationService($this->config->apiKeys['abstract_api_email_validation'])
56+
//);
57+
58+
return $this;
2159
}
2260

2361
public function run()

app/Config.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @property-read ?array $db
9+
* @property-read ?array $mailer
910
*/
1011
class Config
1112
{
@@ -14,12 +15,22 @@ class Config
1415
public function __construct(array $env)
1516
{
1617
$this->config = [
17-
'db' => [
18-
'host' => $env['DB_HOST'],
19-
'user' => $env['DB_USER'],
20-
'pass' => $env['DB_PASS'],
21-
'database' => $env['DB_DATABASE'],
22-
'driver' => $env['DB_DRIVER'] ?? 'mysql',
18+
'db' => [
19+
'host' => $env['DB_HOST'],
20+
'username' => $env['DB_USER'],
21+
'password' => $env['DB_PASS'],
22+
'database' => $env['DB_DATABASE'],
23+
'driver' => $env['DB_DRIVER'] ?? 'mysql',
24+
'charset' => 'utf8',
25+
'collation' => 'utf8_unicode_ci',
26+
'prefix' => '',
27+
],
28+
'mailer' => [
29+
'dsn' => $env['MAILER_DSN'] ?? '',
30+
],
31+
'apiKeys' => [
32+
'emailable' => $env['EMAILABLE_API_KEY'] ?? '',
33+
'abstract_api_email_validation' => $env['ABSTRACT_API_EMAIL_VALIDATION_API_KEY'] ?? '',
2334
],
2435
];
2536
}

app/Controllers/HomeController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@
44

55
namespace App\Controllers;
66

7+
use App\Attributes\Get;
8+
use App\Attributes\Post;
9+
use App\Attributes\Put;
10+
use App\Attributes\Route;
11+
use App\Enums\HttpMethod;
712
use App\View;
813

914
class HomeController
1015
{
16+
#[Get('/')]
17+
#[Route('/home', HttpMethod::Head)]
1118
public function index(): View
1219
{
1320
return View::make('index');
1421
}
22+
23+
#[Post('/')]
24+
public function store()
25+
{
26+
27+
}
28+
29+
#[Put('/')]
30+
public function update()
31+
{
32+
33+
}
1534
}

app/Router.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,34 @@
44

55
namespace App;
66

7+
use App\Attributes\Route;
78
use App\Exceptions\RouteNotFoundException;
9+
use Illuminate\Container\Container;
810

911
class Router
1012
{
11-
private array $routes;
13+
private array $routes = [];
14+
15+
public function __construct(private Container $container)
16+
{
17+
}
18+
19+
public function registerRoutesFromControllerAttributes(array $controllers)
20+
{
21+
foreach($controllers as $controller) {
22+
$reflectionController = new \ReflectionClass($controller);
23+
24+
foreach($reflectionController->getMethods() as $method) {
25+
$attributes = $method->getAttributes(Route::class, \ReflectionAttribute::IS_INSTANCEOF);
26+
27+
foreach($attributes as $attribute) {
28+
$route = $attribute->newInstance();
29+
30+
$this->register($route->method->value, $route->routePath, [$controller, $method->getName()]);
31+
}
32+
}
33+
}
34+
}
1235

1336
public function register(string $requestMethod, string $route, callable|array $action): self
1437
{
@@ -45,15 +68,13 @@ public function resolve(string $requestUri, string $requestMethod)
4568
return call_user_func($action);
4669
}
4770

48-
if (is_array($action)) {
49-
[$class, $method] = $action;
71+
[$class, $method] = $action;
5072

51-
if (class_exists($class)) {
52-
$class = new $class();
73+
if (class_exists($class)) {
74+
$class = $this->container->get($class);
5375

54-
if (method_exists($class, $method)) {
55-
return call_user_func_array([$class, $method], []);
56-
}
76+
if (method_exists($class, $method)) {
77+
return call_user_func_array([$class, $method], []);
5778
}
5879
}
5980

composer.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
{
22
"require": {
33
"ext-pdo": "*",
4-
"vlucas/phpdotenv": "^5.3"
4+
"vlucas/phpdotenv": "^5.3",
5+
"psr/container": "^2.0",
6+
"symfony/mailer": "^6.0",
7+
"doctrine/dbal": "^3.3",
8+
"doctrine/orm": "^2.11",
9+
"symfony/cache": "^6.0",
10+
"doctrine/migrations": "^3.4",
11+
"illuminate/database": "^9.8",
12+
"illuminate/events": "^9.8",
13+
"ext-curl": "*",
14+
"guzzlehttp/guzzle": "^7.4"
515
},
616
"autoload": {
717
"psr-4": {
818
"App\\": "app/"
919
}
1020
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Tests\\": "tests/"
24+
}
25+
},
1126
"config": {
1227
"optimize-autoloader": true
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "^9.5"
1331
}
1432
}

0 commit comments

Comments
 (0)