Skip to content

Commit

Permalink
move folders and create core service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosjfernandes committed Jul 5, 2024
1 parent 4fb18b4 commit 07f4c78
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 100 deletions.
2 changes: 0 additions & 2 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
Expand Down
3 changes: 2 additions & 1 deletion bootstrap/providers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

return [
App\Providers\AppServiceProvider::class,
App\Core\Providers\CoreServiceProvider::class,
App\User\Providers\UserServiceProvider::class,
];
2 changes: 1 addition & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => env('AUTH_MODEL', App\Models\User::class),
'model' => env('AUTH_MODEL', \App\User\Model\User::class),
],

// 'users' => [
Expand Down
1 change: 0 additions & 1 deletion database/.gitignore

This file was deleted.

35 changes: 0 additions & 35 deletions database/migrations/0001_01_01_000001_create_cache_table.php

This file was deleted.

8 changes: 0 additions & 8 deletions routes/console.php

This file was deleted.

5 changes: 0 additions & 5 deletions routes/web.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Core/Database/Seed/DatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Core\Database\Seed;

use App\User\Database\Seed\UserSeeder;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
public function __construct() {}

/**
* Run the database seeds.
*/
public function run(): void
{
$this->call([
UserSeeder::class,
]);
}
}
Binary file added src/Core/Database/database.sqlite
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers;
namespace App\Core\Http\Controller;

abstract class Controller
{
Expand Down
9 changes: 9 additions & 0 deletions src/Core/Http/Routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Illuminate\Support\Facades\Route;

Route::middleware('web')->group(function () {
Route::get('/', function () {
return view('core::welcome');
});
});
96 changes: 96 additions & 0 deletions src/Core/Providers/BaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\Core\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use ReflectionClass;

abstract class BaseServiceProvider extends ServiceProvider
{
private string $migrationsPath = '../Database/Migration';

private string $routesPath = '../Http/Routes';

private array $routeFiles = [];

private string $viewsPath = '../Resources/Views';

private string $viewsNamespace = '';

public function boot(): void
{
Model::shouldBeStrict();

$this->loadMigrationsFrom($this->basePath()."/$this->migrationsPath");

$this->loadViewsFrom($this->basePath()."/$this->viewsPath", $this->getViewsNamespace());

$this->bootRoutes();
}

protected function basePath(): string
{
$reflection = new ReflectionClass($this);

return dirname($reflection->getFileName());
}

public function bootRoutes(): void
{
$files = blank($this->routeFiles)
? glob($this->basePath()."/$this->routesPath/*.php")
: $this->routeFiles;

foreach ($files as $file) {
require $file;
}
}

public function setMigrationsPath(string $path): self
{
$this->migrationsPath = $path;

return $this;
}

public function setRoutesPath(string $path): self
{
$this->routesPath = $path;

return $this;
}

public function setRouteFiles(array $routeFiles): self
{
$this->routeFiles = $routeFiles;

return $this;
}

public function setViewsPath(string $path, string $namespace = ''): self
{
$this->viewsPath = $path;
$this->viewsNamespace = $namespace;

return $this;
}

public function setViewsNamespace(string $namespace): self
{
$this->viewsNamespace = $namespace;

return $this;
}

private function getViewsNamespace(): string
{
if ($this->viewsNamespace) {
return $this->viewsNamespace;
}

$folderName = basename(dirname($this->basePath()));

return str($folderName)->snake()->value();
}
}
17 changes: 17 additions & 0 deletions src/Core/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Core\Providers;

use App\Core\Database\Seed\DatabaseSeeder;

class CoreServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
$this->app->bind('DatabaseSeeder', function ($app) {
return new DatabaseSeeder;
});

parent::boot();
}
}
1 change: 1 addition & 0 deletions src/Core/Resources/Views/welcome.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome</h1>
11 changes: 0 additions & 11 deletions src/Http/Controllers/HomeController.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Providers/AppServiceProvider.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

namespace Database\Factories;
namespace App\User\Database\Factory;

use App\User\Model\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;

protected $model = User::class;

/**
* Define the model's default state.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Database\Seeders;
namespace App\User\Database\Seed;

use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\User\Model\User;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;

class UserSeeder extends Seeder
{
/**
* Seed the application's database.
Expand Down
10 changes: 8 additions & 2 deletions src/Models/User.php → src/User/Model/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace App\Models;
namespace App\User\Model;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\User\Database\Factory\UserFactory;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand Down Expand Up @@ -44,4 +45,9 @@ protected function casts(): array
'password' => 'hashed',
];
}

protected static function newFactory(): Factory
{
return UserFactory::new();
}
}
7 changes: 7 additions & 0 deletions src/User/Providers/UserServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\User\Providers;

use App\Core\Providers\BaseServiceProvider;

class UserServiceProvider extends BaseServiceProvider {}

0 comments on commit 07f4c78

Please sign in to comment.