Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
esbenp committed Jun 30, 2015
1 parent defa314 commit e87d84c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 73 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Laravel Package Boilerplate
# Onion

[![Build Status](https://travis-ci.org/esbenp/laravel-package-boilerplate.svg)](https://travis-ci.org/esbenp/laravel-package-boilerplate) [![Coverage Status](https://coveralls.io/repos/esbenp/laravel-package-boilerplate/badge.svg?branch=master)](https://coveralls.io/r/esbenp/laravel-package-boilerplate?branch=master)

A standalone middleware library without dependencies inspired by middleware in Laravel (Illuminate/Pipeline)

## Installation

```bash
composer require laravel-package-boilerplate 0.1.*
composer require optimus/onion 0.1.*
```
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "optimus/laravel-boilerplate",
"name": "optimus/onion",
"autoload": {
"psr-4": {
"Optimus\\LaravelBoilerplate\\": "src/"
"Optimus\\Onion\\": "src/"
}
},
"require": {
"laravel/framework": "~5.1"
},
"require-dev": {
"mockery/mockery": "0.9.*",
Expand Down
19 changes: 0 additions & 19 deletions src/Facade/Package.php

This file was deleted.

11 changes: 11 additions & 0 deletions src/LayerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Optimus\Onion;

use \Closure;

interface LayerInterface {

public function peel($object, Closure $next);

}
80 changes: 80 additions & 0 deletions src/Onion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Optimus\Onion;

use InvalidArgumentException;
use Closure;
use Optimus\Onion\LayerInterface;

class Onion {

private $layers;

public function __construct(array $layers = [])
{
$this->layers = $layers;
}

public function layer($layers)
{
if ($layers instanceof Onion) {
$layers = $layers->toArray();
}

if ($layers instanceof LayerInterface) {
$layers = [$layers];
}

if (!is_array($layers)) {
throw new InvalidArgumentException(get_class($layers) . " is not a valid onion layer.");
}

return new static(array_merge($this->layers, $layers));
}

public function peel($object, Closure $core)
{
$coreFunction = $this->createCoreFunction($core);

// Since we will be "currying" the functions starting with the first
// in the array, the first function will be "closer" to the core.
// This also means it will be run last. However, if the reverse the
// order of the array, the first in the list will be the outer layers.
$layers = array_reverse($this->layers);

// We create the onion by starting initially with the core and then
// gradually wrap it in layers. Each layer will have the next layer "curried"
// into it and will have the current state (the object) passed to it.
$completeOnion = array_reduce($layers, function($nextLayer, $layer){
return $this->createLayer($nextLayer, $layer);
}, $coreFunction);

// We now have the complete onion and can start passing the object
// down through the layers.
return $completeOnion($object);
}

// The inner function of the onion.
// This function will be wrapped on layers
private function createCoreFunction(Closure $core)
{
return function($object) use($core) {
return call_user_func($core, $object);
};
}

// Get an onion layer function.
// This function will get the object from a previous layer and pass it inwards
private function createLayer($nextLayer, $layer)
{
return function($object) use($nextLayer, $layer){
return call_user_func_array([$layer, 'peel'], [$object, $nextLayer]);
};
}

public function toArray()
{
return $this->layers;
}

}
39 changes: 0 additions & 39 deletions src/Provider/LaravelServiceProvider.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/config/package.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/lang/en/lang.php

This file was deleted.

0 comments on commit e87d84c

Please sign in to comment.