1. Go to this page and download the library: Download takielias/contract-resolver library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
takielias / contract-resolver example snippets
namespace App\Contracts\Repositories;
interface UserRepositoryInterface
{
public function findById(int $id): ?User;
public function create(array $data): User;
public function update(int $id, array $data): bool;
public function delete(int $id): bool;
}
namespace App\Repositories;
use App\Contracts\Repositories\UserRepositoryInterface;
use App\Models\User;
class UserRepository implements UserRepositoryInterface
{
public function findById(int $id): ?User
{
return User::find($id);
}
public function create(array $data): User
{
return User::create($data);
}
public function update(int $id, array $data): bool
{
return User::where('id', $id)->update($data);
}
public function delete(int $id): bool
{
return User::destroy($id);
}
}
namespace App\Http\Controllers;
use App\Contracts\Repositories\UserRepositoryInterface;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function __construct(
private UserRepositoryInterface $userRepository
) {}
public function show(int $id)
{
$user = $this->userRepository->findById($id);
return response()->json($user);
}
}
namespace App\Services;
use App\Contracts\Services\ProductServiceInterface;
class ProductService implements ProductServiceInterface
{
//
}
namespace App\Contracts\Repositories;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
interface UserRepositoryInterface
{
public function paginate(int $perPage = 15): LengthAwarePaginator;
public function findById(int $id): ?User;
public function findByEmail(string $email): ?User;
public function create(array $data): User;
public function update(int $id, array $data): bool;
public function delete(int $id): bool;
public function search(string $query): \Illuminate\Database\Eloquent\Collection;
}
namespace App\Repositories;
use App\Contracts\Repositories\UserRepositoryInterface;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class UserRepository implements UserRepositoryInterface
{
public function paginate(int $perPage = 15): LengthAwarePaginator
{
return User::paginate($perPage);
}
public function findById(int $id): ?User
{
return User::find($id);
}
public function findByEmail(string $email): ?User
{
return User::where('email', $email)->first();
}
public function create(array $data): User
{
return User::create($data);
}
public function update(int $id, array $data): bool
{
return User::where('id', $id)->update($data);
}
public function delete(int $id): bool
{
return User::destroy($id);
}
public function search(string $query): \Illuminate\Database\Eloquent\Collection
{
return User::where('name', 'like', "%{$query}%")
->orWhere('email', 'like', "%{$query}%")
->get();
}
}
namespace App\Contracts\Services;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
interface UserServiceInterface
{
public function getAllUsers(int $perPage = 15): LengthAwarePaginator;
public function getUserById(int $id): ?User;
public function createUser(array $data): User;
public function updateUser(int $id, array $data): bool;
public function deleteUser(int $id): bool;
public function searchUsers(string $query): \Illuminate\Database\Eloquent\Collection;
}
namespace App\Services;
use App\Contracts\Repositories\UserRepositoryInterface;
use App\Contracts\Services\UserServiceInterface;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Hash;
class UserService implements UserServiceInterface
{
public function __construct(
private UserRepositoryInterface $userRepository
) {}
public function getAllUsers(int $perPage = 15): LengthAwarePaginator
{
return $this->userRepository->paginate($perPage);
}
public function getUserById(int $id): ?User
{
return $this->userRepository->findById($id);
}
public function createUser(array $data): User
{
$data['password'] = Hash::make($data['password']);
return $this->userRepository->create($data);
}
public function updateUser(int $id, array $data): bool
{
if (isset($data['password'])) {
$data['password'] = Hash::make($data['password']);
}
return $this->userRepository->update($id, $data);
}
public function deleteUser(int $id): bool
{
return $this->userRepository->delete($id);
}
public function searchUsers(string $query): \Illuminate\Database\Eloquent\Collection
{
return $this->userRepository->search($query);
}
}
namespace App\Http\Controllers;
use App\Contracts\Services\UserServiceInterface;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function __construct(
private UserServiceInterface $userService
) {}
public function index(Request $request)
{
$users = $this->userService->getAllUsers($request->get('per_page', 15));
return response()->json($users);
}
public function show(int $id)
{
$user = $this->userService->getUserById($id);
if (!$user) {
return response()->json(['message' => 'User not found'], 404);
}
return response()->json($user);
}
public function store(StoreUserRequest $request)
{
$user = $this->userService->createUser($request->validated());
return response()->json($user, 201);
}
public function update(UpdateUserRequest $request, int $id)
{
$updated = $this->userService->updateUser($id, $request->validated());
if (!$updated) {
return response()->json(['message' => 'User not found'], 404);
}
return response()->json(['message' => 'User updated successfully']);
}
public function destroy(int $id)
{
$deleted = $this->userService->deleteUser($id);
if (!$deleted) {
return response()->json(['message' => 'User not found'], 404);
}
return response()->json(['message' => 'User deleted successfully']);
}
public function search(Request $request)
{
$query = $request->get('q', '');
$users = $this->userService->searchUsers($query);
return response()->json($users);
}
}
return [
/*
|--------------------------------------------------------------------------
| Auto-binding Paths
|--------------------------------------------------------------------------
|
| Define the paths where the package should scan for interfaces and
| their implementations. The package will automatically bind
| interfaces to their corresponding implementations.
|
*/
'paths' => [
'contracts' => [
'repositories' => app_path('Contracts/Repositories'),
'services' => app_path('Contracts/Services'),
],
'implementations' => [
'repositories' => app_path('Repositories'),
'services' => app_path('Services'),
],
],
/*
|--------------------------------------------------------------------------
| Naming Conventions
|--------------------------------------------------------------------------
|
| Define the naming conventions used for automatic binding.
| The package will use these patterns to match interfaces
| with their implementations.
|
*/
'naming' => [
'interface_suffix' => 'Interface',
'remove_segments' => ['Contracts'],
],
/*
|--------------------------------------------------------------------------
| Auto-binding
|--------------------------------------------------------------------------
|
| Enable or disable automatic binding of interfaces to implementations.
| When enabled, the package will automatically scan and bind interfaces
| during the service provider registration.
|
*/
'auto_binding' => [
'enabled' => true,
'cache_bindings' => true,
],
];