wp-starter/contracts
Laravel/PHP contract interfaces for a WordPress starter kit. Defines shared abstractions to keep packages decoupled and implementations swappable, providing a lightweight base for building WordPress integrations in a Laravel-style architecture.
Installation Add the package via Composer:
composer require wp-starter/contracts
No additional configuration is required—this is a pure contract package with no runtime dependencies.
First Use Case: Defining Interfaces Use the package to enforce structure in your Laravel application by defining interfaces for:
PostServiceInterface).PostRepositoryInterface).PostPublishedEventInterface).Example:
use WpStarter\Contracts\Repository\RepositoryInterface;
class PostRepository implements RepositoryInterface
{
// Implement required methods: find(), create(), update(), delete()
}
Where to Look First
src/Contracts directory for built-in interfaces (if any).README.md (if available) for package-specific conventions.Use contracts to decouple data access logic from business logic. Workflow:
UserRepositoryInterface).EloquentUserRepository).Example:
// app/Contracts/UserRepositoryInterface.php
namespace App\Contracts;
use WpStarter\Contracts\Repository\RepositoryInterface;
interface UserRepositoryInterface extends RepositoryInterface
{
public function findByEmail(string $email);
}
// app/Repositories/EloquentUserRepository.php
class EloquentUserRepository implements UserRepositoryInterface
{
// ...
}
Leverage contracts to define service boundaries. Workflow:
OrderServiceInterface).ServiceProvider or standalone class.AppServiceProvider.Example:
// app/Contracts/OrderServiceInterface.php
namespace App\Contracts;
use WpStarter\Contracts\Service\ServiceInterface;
interface OrderServiceInterface extends ServiceInterface
{
public function processPayment(int $orderId);
}
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(
OrderServiceInterface::class,
StripeOrderService::class
);
}
Use contracts to standardize event handling. Workflow:
OrderCreatedEventInterface).Event facade.Example:
// app/Contracts/Events/OrderCreatedEventInterface.php
namespace App\Contracts\Events;
use WpStarter\Contracts\Event\EventInterface;
interface OrderCreatedEventInterface extends EventInterface
{
public function getOrderId(): int;
}
// app/Listeners/SendOrderConfirmation.php
class SendOrderConfirmation implements ShouldHandleOrderCreatedEventInterface
{
public function handle(OrderCreatedEventInterface $event)
{
// Logic here
}
}
Mock interfaces in PHPUnit tests to isolate dependencies. Example:
public function test_order_processing()
{
$mockRepository = $this->createMock(UserRepositoryInterface::class);
$mockRepository->method('findById')->willReturn(new User());
$service = new OrderService($mockRepository);
$this->assertTrue($service->processOrder(1));
}
getId()). Focus on high-level behaviors.extends EloquentRepositoryInterface), they may limit flexibility. Prefer generic interfaces like RepositoryInterface./**
* @method Model|null find(int $id)
* @method Model create(array $data)
*/
interface RepositoryInterface { ... }
use statements in your contracts match the package’s namespace (e.g., WpStarter\Contracts\...).composer dump-autoload if interfaces aren’t recognized.src directory as a source root.config/wp-starter.php exists. All behavior is defined via interfaces.AppServiceProvider:
$this->app->bind(
\WpStarter\Contracts\Repository\RepositoryInterface::class,
\App\Repositories\EloquentRepository::class
);
interface CustomPostRepositoryInterface extends RepositoryInterface
{
public function findPublishedPosts();
}
SoftDeletes trait in repositories).Repository package). Example:
use Spatie\Repository\RepositoryInterface as SpatieRepository;
use WpStarter\Contracts\Repository\RepositoryInterface;
class HybridRepository implements RepositoryInterface, SpatieRepository { ... }
public function scopeActive(): Builder
{
return $this->model->where('is_active', true);
}
How can I help you explore Laravel packages today?