Laravel-Injection And Inversion Of Dependencies

Uncouple, that’s the idea

¿Problem?

<?phpnamespace App\Repositories\NoteRepository;class NoteRepository {}

Is time to code!

php artisan make:controller NoteController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NoteController extends Controller
{
public function index()
{
$notes = [];
return view('welcome', compact('notes'));
}

public function create(Request $request)
{
return redirect('/');
}

public function delete(int $id)
{
return redirect('/');
}
}
<?php

use App\Http\Controllers\NoteController;
use Illuminate\Support\Facades\Route;

Route::get('/', [NoteController::class, 'index']);
Route::post('/', [NoteController::class, 'create']);
Route::delete('/{id}', [NoteController::class, 'delete']);
<?php

namespace App\Providers;

use App\Core\Repositories\Interfaces\NoteRepository;
use App\Core\Repositories\NoteRepositoryInMemory;
use App\Core\Services\NoteService;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app
->singleton(
NoteRepository::class,
NoteRepositoryInMemory::class
);
$this->app
->singleton(
NoteService::class,
function ($app) {
return new NoteService(
$app->make(NoteRepository::class)
);
}
);
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
}
<?php

namespace App\Core\Repositories\Interfaces;

interface NoteRepository
{
public function create(string $title, string $description): array;
public function getAll(): array;
public function delete(int $id): void;
}
<?php

namespace App\Core\Repositories;

use App\Core\Repositories\Interfaces\NoteRepository;

class NoteRepositoryInMemory implements NoteRepository
{
public function __construct()
{
if (!session()->has('notes')) {
session([
'notes' => []
]);
}
}

public function getAll(): array
{
return session('notes');
}

public function create(string $name, string $description): array
{
$new = [
"id" => count(session('notes')) + 1,
"name" => $name,
"content" => $description
];
$notes = session('notes');
array_unshift($notes, $new);
session(['notes' => $notes]);
return $new;
}

public function delete(int $id): void
{
$notes = array_filter(
session('notes'),
function ($note) use ($id) {
return $note['id'] != $id;
}
);
session([
'notes' => $notes
]);
}
}
<?php

namespace App\Core\Services;

use App\Core\Repositories\Interfaces\NoteRepository;

class NoteService
{
function __construct(NoteRepository $repository)
{
$this->repository = $repository;
}

public function create(string $name, string $content)
{
return $this->repository->create($name, $content);
}
public function getAll()
{
return $this->repository->getAll();
}

public function delete($id)
{
return $this->repository->delete($id);
}
}
<?php

namespace App\Http\Controllers;

use App\Core\Services\NoteService;
use Illuminate\Http\Request;

class NoteController extends Controller
{
function __construct(NoteService $service)
{
$this->service = $service;
}

public function index()
{
$notes = $this->service->getAll();
return view('welcome', compact('notes'));
}

public function create(Request $request)
{
$request->validate([
'name' => 'required',
'content' => 'required',
]);
$this->service
->create(
$request->name,
$request->content
);
return redirect('/');
}

public function delete(int $id)
{
$this->service->delete($id);
return redirect('/');
}
}

let’s see the magic

<?php

namespace App\Core\Repositories;

use App\Core\Repositories\Interfaces\NoteRepository;
use App\Models\Note;

class NoteRepositoryEloquent implements NoteRepository
{
public function getAll(): array
{
return Note::all()->toArray();
}

public function create(string $name, string $content): array
{

$note = [
'name' => $name,
'content' => $content,
];
Note::create($note);
return $note;
}

public function delete(int $id): void
{
Note::destroy($id);
}
}
...
public function register()
{
$this->app
->singleton(
NoteRepository::class,
NoteRepositoryEloquent::class
);
$this->app
->singleton(
NoteService::class,
function ($app) {
return new NoteService(
$app->make(NoteRepository::class)
);
}
);
}

” Le vent se lève!… Il faut tenter de vivre!”