PHPackages                             context/context - 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. [API Development](/categories/api)
4. /
5. context/context

ActiveLibrary[API Development](/categories/api)

context/context
===============

Library that translates between application and model requests/responses

1442[1 issues](https://github.com/beberlei/context/issues)PHP

Since Aug 12Pushed 13y ago2 watchersCompare

[ Source](https://github.com/beberlei/context)[ Packagist](https://packagist.org/packages/context/context)[ RSS](/packages/context-context/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

DEPRECATED
----------

[](#deprecated)

This library was trying to implement a nice way to implement the boundary between application/UI and business model. It fails in my opinion, because it offers no general theme and no guidance as to why this is useful this exact way.

One concept that I did take away from this is service wrapping, for transactional needs for example. This Gist shows the concept of a Transactional Doctrine ORM Service Proxy:

It is easier to implement this yourself in your code as explicit application boundary, rather than to use a generic library like Context though.

If you want to take a look at a much more strict approach to provide a boundary between UI/App and Model, take a look at what could be called a successor library [LiteCQRS](https://github.com/beberlei/litecqrs-php).

Context
=======

[](#context)

Small PHP library to help you shield your business-rules from the controller and presentation layers. To achieve this goal the Data-Context-Interaction and Entity-Boundary-Interceptor patterns are put to action. Context is a framework "for the model".

This library was born while trying to solve problems with todays Web/MVC applications and their focus on the controller. Using MVC frameworks the "documented" way often leads to tight coupling and painful reusability, testing and refactoring experience. Working with a completly seperate service layer is tedious however, requiring lots of manual mapping between application and model layers through Data-Transfer-Objects. Context tries to automate this mapping process as much as possible.

Context does not interfere in your model. It offers a convenience wrapper around your model that acts as translation mechanism between presentation layer and model.

This strict seperation does not even have to lead to overengineering and non-rapid application building. It is very simple to build rapid prototypes on top of the context abstraction.

Features
--------

[](#features)

- Support mapping Requests (HTTP, Console, or any input for that matter) on model-request
- Encourage Observers that notify presentation layer of different model results (subject).
- Transparent transformation of model exceptions into application-level exceptions/errors
- Pluggable delivery mechanisms (Web, CLI, MessageQueues, Mail, Unit-Tests, ..) map to the same model
- Command pattern approach (Could allow to keep transactional log of the domain events: Do, Undo, Redo)
- Simplify transaction-management
- Hooks for Validation/Input Filtering
- Avoid testing applications through controllers/frameworks

Concepts
--------

[](#concepts)

- Context\\Engine is a wrapper to call model commands from in your application.
- Input sources describe what request data is available to the model, for example superglobals or console arguments.
- Parameter Converters are rules to convert input/request-data into model-request data, for example "2010-01-01" into a datetime instance.
- ExceptionHandler catch every exception from the model layer and allow to turn it into an application level exception.

Example: Calculator
-------------------

[](#example-calculator)

This is a very simple README compatible example for a statistical calculator. It computes the average, variance and standard deviation of a list of numbers.

```
class Calculator
{
    public function statistics(array $numbers)
    {
        if (count($numbers) > count(array_filter($numbers, 'is_numeric'))) {
            throw new \InvalidArgumentException("Input values are not numeric.");
        }

        $average           = array_sum($numbers) / count($numbers);
        $square            = array_map(array($this, 'square'), $numbers);
        $variance          = (array_sum($square) / count($square)) - $this->square($average);
        $standardDeviation = sqrt($variance);

        return array(
            'numbers'           => $numbers,
            'average'           => $average,
            'variance'          => $variance,
            'standardDeviation' => $standardDeviation,
        );
    }

    public function square($x)
    {
        if ( ! is_numeric($x)) {
            throw new \InvalidArgumentException("Input values are not numeric.");
        }

        return $x * $x;
    }
}

```

We want to implement a bunch of example applications on top of this model. During this experiment we will not change a single line of this model, yet make it usable from very different types of applications. Lets start with a completly unabstracted PHP/HTTP application:

```
