diff --git a/README.md b/README.md index 2a62094..cc562d6 100644 --- a/README.md +++ b/README.md @@ -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.* ``` diff --git a/composer.json b/composer.json index 6324ce0..b573d85 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/src/Facade/Package.php b/src/Facade/Package.php deleted file mode 100644 index ab31fd7..0000000 --- a/src/Facade/Package.php +++ /dev/null @@ -1,19 +0,0 @@ -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; + } + +} diff --git a/src/Provider/LaravelServiceProvider.php b/src/Provider/LaravelServiceProvider.php deleted file mode 100644 index 06608e3..0000000 --- a/src/Provider/LaravelServiceProvider.php +++ /dev/null @@ -1,39 +0,0 @@ -loadConfig(); - $this->registerAssets(); - } - - public function boot() - { - $this->loadLangFile(); - } - - private function registerAssets() - { - $this->publishes([ - __DIR__.'/../config/package.php' => config_path('package.php') - ]); - } - - private function loadConfig() - { - if ($this->app['config']->get('package') === null) { - $this->app['config']->set('package', require __DIR__.'/../config/package.php'); - } - } - - private function loadLangFile() - { - $this->loadTranslationsFrom(__DIR__.'/../lang', 'package'); - } - -} diff --git a/src/config/package.php b/src/config/package.php deleted file mode 100644 index ca5d8ed..0000000 --- a/src/config/package.php +++ /dev/null @@ -1,5 +0,0 @@ -