PHPackages                             ionews/light-service-php - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. ionews/light-service-php

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

ionews/light-service-php
========================

Provides a simple interface to implement complex actions through a serie of simple actions

v0.4.2(8y ago)06.5kGPL-2.0PHP

Since May 23Pushed 8y ago4 watchersCompare

[ Source](https://github.com/ionews/light-service-php)[ Packagist](https://packagist.org/packages/ionews/light-service-php)[ Docs](https://github.com/Mcbarros/light-service-php)[ RSS](/packages/ionews-light-service-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (7)Used By (0)

LightService PHP
================

[](#lightservice-php)

[![Latest Stable Version](https://camo.githubusercontent.com/a4ac6b1b6c1462db286564f802c480cc5af75ebe228a826e63365951b890e7de/68747470733a2f2f706f7365722e707567782e6f72672f696f6e6577732f6c696768742d736572766963652d7068702f762f737461626c652e737667)](https://packagist.org/packages/ionews/light-service-php)[![License](https://camo.githubusercontent.com/bc25f14ea96367e907740c7c5f206238b09d416665f3984e13c6ccdb98fdb814/68747470733a2f2f706f7365722e707567782e6f72672f696f6e6577732f6c696768742d736572766963652d7068702f6c6963656e73652e737667)](https://packagist.org/packages/ionews/light-service-php)[![Build Status](https://camo.githubusercontent.com/f9e28e5b0acb8e440900c69096919e47336d43dfb89cb1bbfe7cd72528f3b5fb/68747470733a2f2f7472617669732d63692e6f72672f4d63626172726f732f6c696768742d736572766963652d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Mcbarros/light-service-php)[![Coverage Status](https://camo.githubusercontent.com/71b230ff04f4df6362438410343511f46f46b3c8d3f25e2bf1756e71eef37363/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f4d63626172726f732f6c696768742d736572766963652d7068702f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/Mcbarros/light-service-php?branch=master)[![Dependency Status](https://camo.githubusercontent.com/d3be50fca38be3a17d93f85b0c4c64e9e10bd3709344c3d41fee981d30b9ba49/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3533383464303532313463313538383462333030303036322f62616467652e737667)](https://www.versioneye.com/user/projects/5384d05214c15884b3000062)[![HHVM Status](https://camo.githubusercontent.com/e99cffe63dbc293652ae2dda5dcd0c53b03bf164baf554f2ea550754c051a431/687474703a2f2f6868766d2e683463632e64652f62616467652f696f6e6577732f6c696768742d736572766963652d7068702e706e67)](http://hhvm.h4cc.de/package/ionews/light-service-php)

Small piece of software intended to enforce SRP on PHP apps, thought to be "light" and not use any dependencies. Heavily based on the ideas proposed by two ruby gems:

- [LightService](https://github.com/adomokos/light-service)
- [Interactor](https://github.com/collectiveidea/interactor)

Concept
-------

[](#concept)

Each action should have a single responsibility that must be implemented in the `perform` method. An action can access databases, send emails, call services and etc. When an action is executed, it receives a context which can be read and modified.

To perform more complex operations you must use an organizer chaining multiple actions, which will share the same context during execution. In fact, an organizer is nothing more than an action with a specific implementation, meaning that an action and an organizer share the very same interface. This is useful so you can include an organizer as an action inside another organizer.

Action examples:

```
class GenerateRandomPassword extends Action {
  protected function perform() {
    $length = 8;
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $password = '';

    for ($i = 0; $i < $length; $i++) {
      $password .= $chars[rand(0, strlen($chars) - 1)];
    }

    $this->context->password = $password;
  }
}

class UpdateUserPassword extends Action {
  protected function perform() {
    $user_id = $this->context->user_id;
    $password = $this->context->password;
    // access the database using the method of your choice and update the password
  }
}
```

Organizer example:

```
class ResetUserPassword extends Organizer {
  protected $organize = ['GenerateRandomPassword', 'UpdateUserPassword', 'EmailUserWithPassword'];
}
```

Call example (from a MVC controller):

```
class UserController extends BaseController {
  public function resetPassword() {
    $result = ResetUserPassword::execute(['user_id' => $this->request->id]);

    if ($result->success()) {
      // use $result->getContext() to access the results and redirect the app
    } else {
      // error, use $result->getFailureMessage() to access any failure message
    }
  }
}
```

**Keep in mind that you shouldn't use this approach everywhere in your app, but only in the really complex parts of it.**

Fail, Halt and Exceptions
-------------------------

[](#fail-halt-and-exceptions)

An action may fail, meaning that it couldn't achieve its goal. To make an action fail just call the `fail` method (optionally passing a message).

```
class SomeAction extends Action {
  protected function perform() {
    $this->fail('Oh noes');
  }
}

$result = SomeAction::execute([]);
$result->success(); // false
$result->failure(); // true
$result->halted(); // false
$result->getFailureMessage(); // 'Oh noes'
```

If the action is executing inside an organizer and fails, it will prevent the execution of the subsequents actions. If an action implements a `rollback` method, it will be called after a subsequent action fails. Example: if `EmailUserWithPassword` fails to send an e-mail to the user, we could implement an `rollback` method in the `UpdateUserPassword` to undo the update. Inside the `rollback` method you can access the context in the same way as in `perform`.

```
class UpdateUserPassword extends Action {
  protected function perform() {
    $user_id = $this->context->user_id;
    $password = $this->context->password;
    // access the database using the method of your choice and update the password
  }

  protected function rollback() {
    // undo the update password
  }
}
```

**You shouldn't re-implement the `rollback` method of an organizer, unless you really know what you're doing.**

It's possible to stop the execution chain without fail: using `halt`. Basically it will prevent any subsequent actions of execute, but the result remains a success. You can test if an action/organizer was halted using the `halted` method.

```
class SomeAction extends Action {
  protected function perform() {
    $this->halt();
  }
}

$result = SomeAction::execute([]);
$result->success(); // true
$result->failure(); // false
$result->halted(); // true
```

All exceptions that occur when performing an action are caught automatically and passed to the `caught` method. By default, actions re-throw then, on the other hand, organizers first call the `rollback` method before re-throwing. This is done so all performed actions are rolled back before the exception propagation. You can change this behaviour re-implementing the `caught` method.

Before and After
----------------

[](#before-and-after)

A `before` method can be implemented if you need to do any setup pre-execution. If the `fail` method is called inside the `before`, `perform` will never be called. In the same way, an `after` method can be implemented so you can do any cleanup, but keep in mind that if `before` or `perform` fails it will never be called.

```
class SomeAction extends Action {
  protected function before() {
    // any setup
  }

  protected function perform() {
    // perform
  }

  protected function after() {
    // cleanup
  }
}
```

Expects and Promises
--------------------

[](#expects-and-promises)

Expectations and promises can be defined for each action. If an action has a set of expectations, it will automatically fails if these aren't met.

```
class UpdateUserPassword extends Action {
  protected $expects = ['user_id', 'password'];

  protected function perform() {
    $user_id = $this->context->user_id;
    $password = $this->context->password;
    // access the database using the method of your choice and update the password
  }
}

$result = UpdateUserPassword::execute(['user_id' => 1]);
$result->success(); // false
$result->getFailureMessage(); // 'Expectations were not met'
```

Similarly, an action will fail if a set of promises are defined and these are not present in the context at the end of execution.

```
class GenerateRandomPassword extends Action {
  protected $promises = ['password'];

  protected function perform() {
    $length = 8;
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $password = '';

    for ($i = 0; $i < $length; $i++) {
      $password .= $chars[rand(0, strlen($chars) - 1)];
    }

    //$this->context->password = $password;
  }
}

$result = GenerateRandomPassword::execute([]);
$result->success(); // false
$result->getFailureMessage(); // 'Promises were not met'
```

This feature is particularly useful so you can explicitly define the interface between the actions.

Iterator Action
---------------

[](#iterator-action)

It's an action that will be performed over an array.

```
class SomeAction extends IteratorAction {
  protected $over = 'key_of_the_array_in_context'

  protected function each($key, $value) {
    ...
  }
}
```

Requirements
------------

[](#requirements)

- PHP 5.3+

Installation and Usage
----------------------

[](#installation-and-usage)

Contributing
------------

[](#contributing)

You know the drill!

License
-------

[](#license)

Released under GPLv2

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.3% 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 ~249 days

Recently: every ~311 days

Total

6

Last Release

3129d ago

### Community

Maintainers

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

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

---

Top Contributors

[![mcbarros](https://avatars.githubusercontent.com/u/3949890?v=4)](https://github.com/mcbarros "mcbarros (26 commits)")[![CauanCabral](https://avatars.githubusercontent.com/u/83092?v=4)](https://github.com/CauanCabral "CauanCabral (1 commits)")

---

Tags

businessservicerulesactionOrganizerinteractor

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ionews-light-service-php/health.svg)

```
[![Health](https://phpackages.com/badges/ionews-light-service-php/health.svg)](https://phpackages.com/packages/ionews-light-service-php)
```

###  Alternatives

[prettus/laravel-validation

Laravel Validation Service

40710.8M52](/packages/prettus-laravel-validation)[ruler/ruler

A simple stateless production rules engine for modern PHP.

1.1k794.6k3](/packages/ruler-ruler)[mi-schi/phpmd-extension

Contains extra phpmd rules from clean code book and the best practices of my experiences.

40882.0k5](/packages/mi-schi-phpmd-extension)[suin/php-cs-fixer-rules

A Rule set for PHP-CS-Fixer mainly targeting PHP 7.1 or higher.

13116.7k3](/packages/suin-php-cs-fixer-rules)[hybridlogic/validation

A simple, extensible validation library for PHP with support for filtering and validating any input array along with generating client side validation code.

641.1k](/packages/hybridlogic-validation)

PHPackages © 2026

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