PHPackages                             thombas/revised-service-pattern - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. thombas/revised-service-pattern

ActiveLibrary

thombas/revised-service-pattern
===============================

A package that adds a service pattern which extends from the established models.

1.0.5(9mo ago)0512MITPHPPHP ^8.0CI passing

Since May 1Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/Thombas/revised-service-pattern)[ Packagist](https://packagist.org/packages/thombas/revised-service-pattern)[ RSS](/packages/thombas-revised-service-pattern/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (4)Versions (9)Used By (0)

Revised Service Pattern for Laravel
===================================

[](#revised-service-pattern-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/768e7d5fe0079b15f0cb31a9d01f7e8cb68e923afb499cafa3d58a6bbd2fd02b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74686f6d6261732f726576697365642d736572766963652d7061747465726e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thombas/revised-service-pattern)[![Total Downloads](https://camo.githubusercontent.com/6cd1db9f846350f00257dc7a070a70cea7b533bbece93259135fbb98b24bab4c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74686f6d6261732f726576697365642d736572766963652d7061747465726e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thombas/revised-service-pattern)[![License: MIT](https://camo.githubusercontent.com/2316cb2316de0a774043e3a619d9acb91e12de1f0fb0b210a60a62a505552d09/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f54686f6d6261732f726576697365642d736572766963652d7061747465726e3f7374796c653d666c61742d737175617265)](https://github.com/Thombas/revised-service-pattern/blob/main/LICENSE)[![Tests](https://github.com/Thombas/revised-service-pattern/actions/workflows/tests.yml/badge.svg)](https://github.com/Thombas/revised-service-pattern)

---

🚀 Introduction
--------------

[](#-introduction)

**Revised Service Pattern** is a Laravel package that extends Laravel’s native HTTP client with a clean, structured singleton-based service pattern for interacting with third-party RESTful APIs.

It’s built for developers who want:

- ✅ Rapid third-party API integration
- ✅ Organized endpoint structure
- ✅ Full compatibility with Laravel's HTTP tools

---

✨ Features
----------

[](#-features)

- Lightweight and framework-friendly
- Singleton-based API access pattern
- Extends Laravel’s `Http` facade for consistency
- Auto-discovered endpoints and templates
- Artisan commands for bootstrapping services, templates, and stubs
- Testing-friendly with no boilerplate or conditional logic pollution

---

📦 Installation
--------------

[](#-installation)

```
composer require thombas/revised-service-pattern
```

---

🛠️ Getting Started
------------------

[](#️-getting-started)

### 1️⃣ Create a Base Service

[](#1️⃣-create-a-base-service)

Define your base service to handle global API config (e.g. base URL, headers, auth).

```
use Thombas\RevisedServicePattern\Services\RestApiService;

class MyApiService extends RestApiService
{
    protected function setup(): static
    {
        return $this->baseUrl('https://api.example.com')->asJson();
    }
}
```

### 2️⃣ Create Endpoint Services

[](#2️⃣-create-endpoint-services)

Endpoints extend your base service. Each represents one API route and defines method, URL, and parameters.

```
class FetchUserEndpoint extends MyApiService
{
    protected function setup(): static
    {
        return parent::setup()
            ->setUrl('user')
            ->setMethod(ServiceMethodEnum::Get)
            ->setParamaters(['id' => 123]);
    }
}
```

> ✅ **Best Practice:** Keep one class per endpoint for maintainability and clear logic separation.

---

### ⚙️ Artisan Commands

[](#️-artisan-commands)

Scaffold services and templates using built-in Artisan commands.

```
php artisan template:create OpenWeather
```

Add an endpoint directly:

```
php artisan template:create OpenWeather --endpoint=Thunderstorms
```

Use subdirectories for grouping:

```
php artisan template:create OpenWeather --endpoint=Thunderstorms/Frequency
```

> **Note:** Both `/` and `\` are accepted as path separators.

---

📡 Endpoint Lifecycle
--------------------

[](#-endpoint-lifecycle)

Endpoint classes support full control over request logic using lifecycle hooks:

### `__construct()`

[](#__construct)

Receive parameters:

```
public function __construct(protected string $userId)
{
    parent::__construct();
}
```

### `setup()`

[](#setup)

Configure request:

```
protected function setup(): static
{
    return parent::setup()
        ->setUrl('user')
        ->setMethod(ServiceMethodEnum::Get)
        ->setParamaters(['id' => 123]);
}
```

### `before()`

[](#before)

Optional pre-request logic (e.g. caching):

```
protected function before(): ?Response
{
    return Cache::get("user_{$this->userId}");
}
```

### `validate()`

[](#validate)

Inspect API response and throw errors:

```
protected function validate(Response $response): void
{
    if ($response->json('status') !== 'success') {
        throw new \Exception("API error: " . $response->json('message'));
    }
}
```

### `after()`

[](#after)

Post-request processing (e.g. caching, saving):

```
protected function after(Response $response): void
{
    Cache::put("user_{$this->userId}", $response->json('data'), now()->addMinutes(10));
}
```

### `format()`

[](#format)

Return response in preferred format:

```
protected function format(Response $response): mixed
{
    return [
        'id' => $response->json('data.id'),
        'name' => $response->json('data.full_name'),
    ];
}
```

---

🧩 Accessing Endpoints
---------------------

[](#-accessing-endpoints)

Use your service to statically call any registered endpoint:

```
$response = MyApiService::fetchUser();
```

- Endpoints must be in the same directory as your base service.
- Each endpoint class name must be unique to avoid conflicts.

---

📦 Templates
-----------

[](#-templates)

Templates structure and validate input payloads for your endpoints.

### Example

[](#example)

```
public function __construct(public string $code = 'test')
{
    parent::__construct();
}

public function __invoke(): array
{
    return ['Code' => $this->code];
}

protected function validator(): ?\Illuminate\Validation\Validator
{
    return \Illuminate\Support\Facades\Validator::make(
        ['code' => $this->code],
        ['code' => ['required', 'min:3', 'max:255']]
    );
}
```

Use as a parameter:

```
$response = MyApiService::fetchUser(new FetchUserTemplate());
```

---

🧪 Testing
---------

[](#-testing)

Run PHPUnit tests with:

```
vendor/bin/phpunit
```

---

📜 Changelog
-----------

[](#-changelog)

Check out the [CHANGELOG](https://github.com/Thombas/revised-service-pattern/blob/main/CHANGELOG.md) for version history.

---

🤝 Contributing
--------------

[](#-contributing)

Contributions are welcome!
See [CONTRIBUTING.md](https://github.com/Thombas/revised-service-pattern/blob/main/.github/CONTRIBUTING.md).

---

🔒 Security
----------

[](#-security)

For security concerns, please refer to our [security policy](https://github.com/Thombas/revised-service-pattern/security/policy).

---

🏆 Credits
---------

[](#-credits)

- [Thomas Fielding](https://github.com/Thombas)

---

🪪 License
---------

[](#-license)

Licensed under the [MIT License](https://github.com/Thombas/revised-service-pattern/blob/main/LICENSE.md).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance55

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Recently: every ~6 days

Total

8

Last Release

298d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce1f76be13bfe5f2d81441712abea15af85413f46ff7623b76472bbc1e03ecc0?d=identicon)[Thombas](/maintainers/Thombas)

---

Top Contributors

[![Thombas](https://avatars.githubusercontent.com/u/15570381?v=4)](https://github.com/Thombas "Thombas (2 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/thombas-revised-service-pattern/health.svg)

```
[![Health](https://phpackages.com/badges/thombas-revised-service-pattern/health.svg)](https://phpackages.com/packages/thombas-revised-service-pattern)
```

###  Alternatives

[spatie/laravel-failed-job-monitor

Get notified when a queued job fails

1.0k2.6M4](/packages/spatie-laravel-failed-job-monitor)[mozex/laravel-scout-bulk-actions

A Laravel Scout extension for bulk importing and flushing of all models.

1033.4k](/packages/mozex-laravel-scout-bulk-actions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
