├── .editorconfig ├── .github ├── CONTRIBUTING.md ├── FUNDING.yml ├── SECURITY.md └── workflows │ ├── bc-check.yml │ ├── phpstan.yml │ ├── pint.yml │ └── run-tests.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json ├── composer.lock ├── phpstan.neon ├── phpunit.xml.dist ├── pint.json ├── src ├── EnhancedPipelineServiceProvider.php ├── Events │ ├── PipeExecutionFinished.php │ ├── PipeExecutionStarted.php │ ├── PipelineFinished.php │ └── PipelineStarted.php ├── Pipeline.php └── Traits │ ├── HasDatabaseTransactions.php │ └── HasEvents.php └── tests ├── OriginalPipelineTest.php ├── PipelineEventsTest.php ├── PipelineRunTest.php ├── PipelineTest.php └── TestCase.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_size = 4 6 | indent_style = space 7 | end_of_line = lf 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - # **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ["https://paypal.me/observername"] 2 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you discover any security related issues, please email `contact@observer.name` instead of using the issue tracker. 4 | -------------------------------------------------------------------------------- /.github/workflows/bc-check.yml: -------------------------------------------------------------------------------- 1 | name: bc-check 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | backwards-compatibility-check: 13 | name: "Backwards Compatibility Check" 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v2 17 | with: 18 | fetch-depth: 0 19 | - name: "Install PHP" 20 | uses: shivammathur/setup-php@v2 21 | with: 22 | php-version: "8.3" 23 | - name: "Install dependencies" 24 | run: "composer install" 25 | - name: "Install BC check" 26 | run: "composer require --dev roave/backward-compatibility-check" 27 | - name: "Check for BC breaks" 28 | run: "vendor/bin/roave-backward-compatibility-check" 29 | -------------------------------------------------------------------------------- /.github/workflows/phpstan.yml: -------------------------------------------------------------------------------- 1 | name: phpstan 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | larastan: 11 | name: "Running Larastan check" 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: '8.3' 20 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl 21 | coverage: none 22 | 23 | - name: Cache composer dependencies 24 | uses: actions/cache@v2 25 | with: 26 | path: vendor 27 | key: composer-${{ hashFiles('composer.lock') }} 28 | 29 | - name: Run composer install 30 | run: composer install -n --prefer-dist 31 | 32 | - name: Run phpstan 33 | run: ./vendor/bin/phpstan 34 | -------------------------------------------------------------------------------- /.github/workflows/pint.yml: -------------------------------------------------------------------------------- 1 | name: pint 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | pint: 11 | name: "Running Laravel Pint check" 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: '8.3' 20 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl 21 | coverage: none 22 | 23 | - name: Cache composer dependencies 24 | uses: actions/cache@v2 25 | with: 26 | path: vendor 27 | key: composer-${{ hashFiles('composer.lock') }} 28 | 29 | - name: Run composer install 30 | run: composer install -n --prefer-dist 31 | 32 | - name: Run Laravel Pint 33 | run: ./vendor/bin/pint --test 34 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | test: 11 | runs-on: ${{ matrix.os }} 12 | strategy: 13 | fail-fast: true 14 | matrix: 15 | os: [ubuntu-latest] 16 | php: [8.1, 8.2, 8.3, 8.4] 17 | laravel: ['10.*', '11.*'] 18 | stability: [prefer-lowest, prefer-stable] 19 | include: 20 | - laravel: 10.* 21 | testbench: 8.* 22 | - laravel: 11.* 23 | testbench: 9.* 24 | exclude: 25 | - laravel: 11.* 26 | php: 8.1 27 | 28 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} 29 | 30 | steps: 31 | - name: Checkout code 32 | uses: actions/checkout@v3 33 | 34 | - name: Setup PHP 35 | uses: shivammathur/setup-php@v2 36 | with: 37 | php-version: ${{ matrix.php }} 38 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, fileinfo, sodium 39 | coverage: pcov 40 | 41 | - name: Setup problem matchers 42 | run: | 43 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 44 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 45 | 46 | - name: Install dependencies 47 | run: | 48 | composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:^2.64.1" --no-interaction --no-update 49 | composer update --${{ matrix.stability }} --prefer-dist --no-interaction 50 | 51 | - name: Execute tests 52 | run: vendor/bin/phpunit --coverage-clover build/clover.xml 53 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .php_cs 3 | .php_cs.cache 4 | .phpunit.result.cache 5 | build 6 | coverage 7 | phpunit.xml 8 | psalm.xml 9 | testbench.yaml 10 | vendor 11 | node_modules 12 | .php-cs-fixer.cache 13 | .phpunit.cache 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Michael Rubel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Enhanced Pipeline in Laravel](https://user-images.githubusercontent.com/37669560/183900755-de9856b2-012e-4a56-a99f-dd46d70538be.png) 2 | 3 | # Laravel Enhanced Pipeline 4 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/michael-rubel/laravel-enhanced-pipeline.svg?style=flat-square&logo=packagist)](https://packagist.org/packages/michael-rubel/laravel-enhanced-pipeline) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/michael-rubel/laravel-enhanced-pipeline.svg?style=flat-square&logo=packagist)](https://packagist.org/packages/michael-rubel/laravel-enhanced-pipeline) 6 | [![Code Quality](https://img.shields.io/scrutinizer/quality/g/michael-rubel/laravel-enhanced-pipeline.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/michael-rubel/laravel-enhanced-pipeline/?branch=main) 7 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/michael-rubel/laravel-enhanced-pipeline.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/michael-rubel/laravel-enhanced-pipeline/?branch=main) 8 | [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-enhanced-pipeline/run-tests.yml?branch=main&style=flat-square&label=tests&logo=github)](https://github.com/michael-rubel/laravel-enhanced-pipeline/actions) 9 | [![PHPStan](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-enhanced-pipeline/phpstan.yml?branch=main&style=flat-square&label=larastan&logo=laravel)](https://github.com/michael-rubel/laravel-enhanced-pipeline/actions) 10 | 11 | Laravel Pipeline with DB transaction support, events and additional methods. 12 | 13 | The package requires `PHP 8.1` or higher and `Laravel 10` or higher. 14 | 15 | --- 16 | 17 | ## #StandWithUkraine 18 | [![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md) 19 | 20 | ## Installation 21 | Install the package using composer: 22 | ```bash 23 | composer require michael-rubel/laravel-enhanced-pipeline 24 | ``` 25 | 26 | ## Usage 27 | Import modified pipeline to your class: 28 | ```php 29 | use MichaelRubel\EnhancedPipeline\Pipeline; 30 | ``` 31 | 32 | Then use the pipeline: 33 | ```php 34 | Pipeline::make() 35 | ->withEvents() 36 | ->withTransaction() 37 | ->send($data) 38 | ->through([ 39 | // your pipes 40 | ]) 41 | ->onFailure(function ($data, $exception) { 42 | // do something when exception caught 43 | 44 | return $data; 45 | })->then(function ($data) { 46 | // do something when all pipes completed their work 47 | 48 | return $data; 49 | }); 50 | ``` 51 | 52 | You can as well instantiate the pipeline using the service container or manually: 53 | ```php 54 | app(Pipeline::class) 55 | ... 56 | 57 | (new Pipeline(app())) 58 | ... 59 | 60 | (new Pipeline) 61 | ->setContainer(app()) 62 | ... 63 | ``` 64 | 65 | You can use the `run` method to execute a single pipe: 66 | ```php 67 | $pipeline = Pipeline::make(); 68 | 69 | $pipeline->run(Pipe::class, $data); 70 | ``` 71 | 72 | By default, `run` uses the `handle` method in your class as an entry point, but if you use a different method name in your pipelines, you can fix that by adding code to your ServiceProvider: 73 | ```php 74 | $this->app->resolving(Pipeline::class, function ($pipeline) { 75 | return $pipeline->via('execute'); 76 | }); 77 | ``` 78 | 79 | If you want to override the original [Pipeline](https://github.com/laravel/framework/blob/9.x/src/Illuminate/Pipeline/Pipeline.php) resolved through IoC Container, you can add binding in the ServiceProvider `register` method: 80 | ```php 81 | $this->app->singleton(\Illuminate\Pipeline\Pipeline::class, \MichaelRubel\EnhancedPipeline\Pipeline::class); 82 | ``` 83 | 84 | ## Transaction 85 | Usage of `withTransaction` method will enable a [`manual DB transaction`](https://laravel.com/docs/9.x/database#manually-using-transactions) throughout the pipeline execution. 86 | 87 | ## Events 88 | Usage of `withEvents` method will enable [`Laravel Events`](https://laravel.com/docs/9.x/events#introduction) throughout the pipeline execution. 89 | 90 | #### Available events 91 | - [`PipelineStarted`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipelineStarted.php) - fired when the pipeline starts working; 92 | - [`PipelineFinished`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipelineFinished.php) - fired when the pipeline finishes its work; 93 | - [`PipeExecutionStarted`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeExecutionStarted.php) - fired **before** execution of the pipe; 94 | - [`PipeExecutionFinished`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeExecutionFinished.php) - fired **after** execution of the pipe. 95 | 96 | ## Testing 97 | ```bash 98 | composer test 99 | ``` 100 | 101 | ## Credits 102 | - [chefhasteeth](https://github.com/chefhasteeth) for base implementation of DB transaction in Pipeline. 103 | - [rezaamini-ir](https://github.com/rezaamini-ir) for inspiration to create a pipeline with `onFailure` method. See [#PR](https://github.com/laravel/framework/pull/42634) 104 | 105 | ## License 106 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 107 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "michael-rubel/laravel-enhanced-pipeline", 3 | "description": "Laravel Pipeline with DB transaction support, events and additional methods.", 4 | "keywords": [ 5 | "michael-rubel", 6 | "laravel", 7 | "laravel-enhanced-pipeline" 8 | ], 9 | "homepage": "https://github.com/michael-rubel/laravel-enhanced-pipeline", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Michael Rubel", 14 | "email": "contact@observer.name", 15 | "role": "Maintainer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1", 20 | "illuminate/contracts": "^10.0|^11.0", 21 | "spatie/laravel-package-tools": "^1.12" 22 | }, 23 | "require-dev": { 24 | "laravel/pint": "^1.0", 25 | "nunomaduro/collision": "^7.0|^8.0", 26 | "larastan/larastan": "^2.0", 27 | "orchestra/testbench": "^8.0|^9.0", 28 | "phpunit/phpunit": "^9.5|^10.5" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "MichaelRubel\\EnhancedPipeline\\": "src" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "MichaelRubel\\EnhancedPipeline\\Tests\\": "tests" 38 | } 39 | }, 40 | "scripts": { 41 | "test": "./vendor/bin/testbench package:test --no-coverage", 42 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 43 | }, 44 | "config": { 45 | "sort-packages": true 46 | }, 47 | "extra": { 48 | "laravel": { 49 | "providers": [ 50 | "MichaelRubel\\EnhancedPipeline\\EnhancedPipelineServiceProvider" 51 | ] 52 | } 53 | }, 54 | "minimum-stability": "dev", 55 | "prefer-stable": true 56 | } 57 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - ./vendor/larastan/larastan/extension.neon 3 | 4 | parameters: 5 | 6 | paths: 7 | - src 8 | 9 | level: max 10 | 11 | ignoreErrors: 12 | - '#Parameter \#1 \$object_or_class of function method_exists expects object\|string, mixed given\.#' 13 | - '#Trying to invoke mixed but it(.*) not a callable\.#' 14 | 15 | checkMissingIterableValueType: false 16 | 17 | reportUnmatchedIgnoredErrors: false 18 | 19 | checkOctaneCompatibility: true 20 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ./src 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "laravel", 3 | "rules": { 4 | "declare_strict_types": true 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/EnhancedPipelineServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-enhanced-pipeline'); 19 | } 20 | 21 | /** 22 | * Register the package. 23 | */ 24 | public function registeringPackage(): void 25 | { 26 | if (! $this->app->bound('events')) { 27 | $this->app->register(EventServiceProvider::class, true); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Events/PipeExecutionFinished.php: -------------------------------------------------------------------------------- 1 | container = $container; 68 | } 69 | 70 | /** 71 | * Create a new class instance. 72 | */ 73 | public static function make(?Container $container = null): Pipeline 74 | { 75 | if (! $container) { 76 | $container = ContainerConcrete::getInstance(); 77 | } 78 | 79 | return $container->make(static::class); 80 | } 81 | 82 | /** 83 | * Set the object being sent through the pipeline. 84 | * 85 | * @param mixed $passable 86 | * @return $this 87 | */ 88 | public function send($passable) 89 | { 90 | $this->passable = $passable; 91 | 92 | return $this; 93 | } 94 | 95 | /** 96 | * Set the array of pipes. 97 | * 98 | * @param array|mixed $pipes 99 | * @return $this 100 | */ 101 | public function through($pipes) 102 | { 103 | $this->pipes = is_array($pipes) ? $pipes : func_get_args(); 104 | 105 | return $this; 106 | } 107 | 108 | /** 109 | * Push additional pipes onto the pipeline. 110 | * 111 | * @param array|mixed $pipes 112 | * @return $this 113 | */ 114 | public function pipe($pipes) 115 | { 116 | array_push($this->pipes, ...(is_array($pipes) ? $pipes : func_get_args())); 117 | 118 | return $this; 119 | } 120 | 121 | /** 122 | * Set the method to call on the pipes. 123 | * 124 | * @param string $method 125 | * @return $this 126 | */ 127 | public function via($method) 128 | { 129 | $this->method = $method; 130 | 131 | return $this; 132 | } 133 | 134 | /** 135 | * Run the pipeline with a final destination callback. 136 | * 137 | * @return mixed 138 | */ 139 | public function then(Closure $destination) 140 | { 141 | try { 142 | $this->fireEvent(PipelineStarted::class, 143 | $destination, 144 | $this->passable, 145 | $this->pipes(), 146 | $this->useTransaction, 147 | ); 148 | 149 | $this->beginTransaction(); 150 | 151 | $pipeline = array_reduce( 152 | array_reverse($this->pipes()), 153 | $this->carry(), 154 | $this->prepareDestination($destination) 155 | ); 156 | 157 | $result = $pipeline($this->passable); 158 | 159 | $this->commitTransaction(); 160 | 161 | $this->fireEvent(PipelineFinished::class, 162 | $destination, 163 | $this->passable, 164 | $this->pipes(), 165 | $this->useTransaction, 166 | $result, 167 | ); 168 | 169 | return $result; 170 | } catch (Throwable $e) { 171 | $this->rollbackTransaction(); 172 | 173 | if ($this->onFailure) { 174 | return ($this->onFailure)($this->passable, $e); 175 | } 176 | 177 | return $this->handleException($this->passable, $e); 178 | } 179 | } 180 | 181 | /** 182 | * Run the pipeline and return the result. 183 | * 184 | * @return mixed 185 | */ 186 | public function thenReturn() 187 | { 188 | return $this->then(function ($passable) { 189 | return $passable; 190 | }); 191 | } 192 | 193 | /** 194 | * Get the final piece of the Closure onion. 195 | * 196 | * @return \Closure 197 | */ 198 | protected function prepareDestination(Closure $destination) 199 | { 200 | return function ($passable) use ($destination) { 201 | return $destination($passable); 202 | }; 203 | } 204 | 205 | /** 206 | * Get a Closure that represents a slice of the application onion. 207 | * 208 | * @return \Closure 209 | */ 210 | protected function carry() 211 | { 212 | return function ($stack, $pipe) { 213 | return function ($passable) use ($stack, $pipe) { 214 | $this->fireEvent(PipeExecutionStarted::class, $pipe, $passable); 215 | 216 | if (is_callable($pipe)) { 217 | // If the pipe is a callable, then we will call it directly, but otherwise we 218 | // will resolve the pipes out of the dependency container and call it with 219 | // the appropriate method and arguments, returning the results back out. 220 | $result = $pipe($passable, $stack); 221 | 222 | $this->fireEvent(PipeExecutionFinished::class, $pipe, $passable); 223 | 224 | return $result; 225 | } elseif (! is_object($pipe)) { 226 | [$name, $parameters] = $this->parsePipeString($pipe); 227 | 228 | // If the pipe is a string we will parse the string and resolve the class out 229 | // of the dependency injection container. We can then build a callable and 230 | // execute the pipe function giving in the parameters that are required. 231 | $pipe = $this->getContainer()->make($name); 232 | 233 | $parameters = array_merge([$passable, $stack], $parameters); 234 | } else { 235 | // If the pipe is already an object we'll just make a callable and pass it to 236 | // the pipe as-is. There is no need to do any extra parsing and formatting 237 | // since the object we're given was already a fully instantiated object. 238 | $parameters = [$passable, $stack]; 239 | } 240 | 241 | $carry = method_exists($pipe, $this->method) 242 | ? $pipe->{$this->method}(...$parameters) 243 | : $pipe(...$parameters); 244 | 245 | $this->fireEvent(PipeExecutionFinished::class, $pipe, $passable); 246 | 247 | return $this->handleCarry($carry); 248 | }; 249 | }; 250 | } 251 | 252 | /** 253 | * Parse full pipe string to get name and parameters. 254 | * 255 | * @param string $pipe 256 | * @return array 257 | */ 258 | protected function parsePipeString($pipe) 259 | { 260 | [$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []); 261 | 262 | if (is_string($parameters)) { 263 | $parameters = explode(',', $parameters); 264 | } 265 | 266 | return [$name, $parameters]; 267 | } 268 | 269 | /** 270 | * Get the array of configured pipes. 271 | * 272 | * @return array 273 | */ 274 | protected function pipes() 275 | { 276 | return $this->pipes; 277 | } 278 | 279 | /** 280 | * Get the container instance. 281 | * 282 | * @return \Illuminate\Contracts\Container\Container 283 | * 284 | * @throws \RuntimeException 285 | */ 286 | protected function getContainer() 287 | { 288 | if (! $this->container) { 289 | throw new RuntimeException('A container instance has not been passed to the Pipeline.'); 290 | } 291 | 292 | return $this->container; 293 | } 294 | 295 | /** 296 | * Set the container instance. 297 | * 298 | * @return $this 299 | */ 300 | public function setContainer(Container $container) 301 | { 302 | $this->container = $container; 303 | 304 | return $this; 305 | } 306 | 307 | /** 308 | * Set callback to be executed on failure pipeline. 309 | * 310 | * @return $this 311 | */ 312 | public function onFailure(Closure $callback) 313 | { 314 | $this->onFailure = $callback; 315 | 316 | return $this; 317 | } 318 | 319 | /** 320 | * Run a single pipe. 321 | */ 322 | public function run(string $pipe, mixed $data = true): mixed 323 | { 324 | return $this 325 | ->send($data) 326 | ->through([$pipe]) 327 | ->thenReturn(); 328 | } 329 | 330 | /** 331 | * Handle the value returned from each pipe before passing it to the next. 332 | * 333 | * @param mixed $carry 334 | * @return mixed 335 | */ 336 | protected function handleCarry($carry) 337 | { 338 | return $carry; 339 | } 340 | 341 | /** 342 | * Handle the given exception. 343 | * 344 | * @param mixed $passable 345 | * @return mixed 346 | * 347 | * @throws \Throwable 348 | */ 349 | protected function handleException($passable, Throwable $e) 350 | { 351 | throw $e; 352 | } 353 | } 354 | -------------------------------------------------------------------------------- /src/Traits/HasDatabaseTransactions.php: -------------------------------------------------------------------------------- 1 | useTransaction = true; 22 | 23 | return $this; 24 | } 25 | 26 | /** 27 | * Begin the transaction if enabled. 28 | */ 29 | protected function beginTransaction(): void 30 | { 31 | if (! $this->useTransaction) { 32 | return; 33 | } 34 | 35 | DB::beginTransaction(); 36 | } 37 | 38 | /** 39 | * Commit the transaction if enabled. 40 | */ 41 | protected function commitTransaction(): void 42 | { 43 | if (! $this->useTransaction) { 44 | return; 45 | } 46 | 47 | DB::commit(); 48 | } 49 | 50 | /** 51 | * Rollback the transaction if enabled. 52 | */ 53 | protected function rollbackTransaction(): void 54 | { 55 | if (! $this->useTransaction) { 56 | return; 57 | } 58 | 59 | DB::rollBack(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Traits/HasEvents.php: -------------------------------------------------------------------------------- 1 | useEvents = true; 20 | 21 | return $this; 22 | } 23 | 24 | /** 25 | * Fire the event if enabled. 26 | * 27 | * @param mixed ...$params 28 | */ 29 | protected function fireEvent(string $event, ...$params): void 30 | { 31 | if (! $this->useEvents) { 32 | return; 33 | } 34 | 35 | event(new $event(...$params)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/OriginalPipelineTest.php: -------------------------------------------------------------------------------- 1 | send('foo') 25 | ->through([PipelineTestPipeOne::class, $pipeTwo]) 26 | ->then(function ($piped) { 27 | return $piped; 28 | }); 29 | 30 | $this->assertSame('foo', $result); 31 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 32 | $this->assertSame('foo', $_SERVER['__test.pipe.two']); 33 | 34 | unset($_SERVER['__test.pipe.one'], $_SERVER['__test.pipe.two']); 35 | } 36 | 37 | public function test_pipeline_usage_with_objects() 38 | { 39 | $result = (new Pipeline(new Container)) 40 | ->send('foo') 41 | ->through([new PipelineTestPipeOne]) 42 | ->then(function ($piped) { 43 | return $piped; 44 | }); 45 | 46 | $this->assertSame('foo', $result); 47 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 48 | 49 | unset($_SERVER['__test.pipe.one']); 50 | } 51 | 52 | public function test_pipeline_usage_with_invokable_objects() 53 | { 54 | $result = (new Pipeline(new Container)) 55 | ->send('foo') 56 | ->through([new PipelineTestPipeTwo]) 57 | ->then( 58 | function ($piped) { 59 | return $piped; 60 | } 61 | ); 62 | 63 | $this->assertSame('foo', $result); 64 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 65 | 66 | unset($_SERVER['__test.pipe.one']); 67 | } 68 | 69 | public function test_pipeline_usage_with_callable() 70 | { 71 | $function = function ($piped, $next) { 72 | $_SERVER['__test.pipe.one'] = 'foo'; 73 | 74 | return $next($piped); 75 | }; 76 | 77 | $result = (new Pipeline(new Container)) 78 | ->send('foo') 79 | ->through([$function]) 80 | ->then( 81 | function ($piped) { 82 | return $piped; 83 | } 84 | ); 85 | 86 | $this->assertSame('foo', $result); 87 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 88 | 89 | unset($_SERVER['__test.pipe.one']); 90 | 91 | $result = (new Pipeline(new Container)) 92 | ->send('bar') 93 | ->through($function) 94 | ->thenReturn(); 95 | 96 | $this->assertSame('bar', $result); 97 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 98 | 99 | unset($_SERVER['__test.pipe.one']); 100 | } 101 | 102 | public function test_pipeline_usage_with_pipe() 103 | { 104 | $object = new stdClass; 105 | 106 | $object->value = 0; 107 | 108 | $function = function ($object, $next) { 109 | $object->value++; 110 | 111 | return $next($object); 112 | }; 113 | 114 | $result = (new Pipeline(new Container)) 115 | ->send($object) 116 | ->through([$function]) 117 | ->pipe([$function]) 118 | ->then( 119 | function ($piped) { 120 | return $piped; 121 | } 122 | ); 123 | 124 | $this->assertSame($object, $result); 125 | $this->assertEquals(2, $object->value); 126 | } 127 | 128 | public function test_pipeline_usage_with_invokable_class() 129 | { 130 | $result = (new Pipeline(new Container)) 131 | ->send('foo') 132 | ->through([PipelineTestPipeTwo::class]) 133 | ->then( 134 | function ($piped) { 135 | return $piped; 136 | } 137 | ); 138 | 139 | $this->assertSame('foo', $result); 140 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 141 | 142 | unset($_SERVER['__test.pipe.one']); 143 | } 144 | 145 | public function test_then_method_is_not_called_if_the_pipe_returns() 146 | { 147 | $_SERVER['__test.pipe.then'] = '(*_*)'; 148 | $_SERVER['__test.pipe.second'] = '(*_*)'; 149 | 150 | $result = (new Pipeline(new Container)) 151 | ->send('foo') 152 | ->through([ 153 | fn ($value, $next) => 'm(-_-)m', 154 | fn ($value, $next) => $_SERVER['__test.pipe.second'] = 'm(-_-)m', 155 | ]) 156 | ->then(function ($piped) { 157 | $_SERVER['__test.pipe.then'] = '(0_0)'; 158 | 159 | return $piped; 160 | }); 161 | 162 | $this->assertSame('m(-_-)m', $result); 163 | // The then callback is not called. 164 | $this->assertSame('(*_*)', $_SERVER['__test.pipe.then']); 165 | // The second pipe is not called. 166 | $this->assertSame('(*_*)', $_SERVER['__test.pipe.second']); 167 | 168 | unset($_SERVER['__test.pipe.then']); 169 | } 170 | 171 | public function test_then_method_input_value() 172 | { 173 | $result = (new Pipeline(new Container)) 174 | ->send('foo') 175 | ->through([function ($value, $next) { 176 | $value = $next('::not_foo::'); 177 | 178 | $_SERVER['__test.pipe.return'] = $value; 179 | 180 | return 'pipe::'.$value; 181 | }]) 182 | ->then(function ($piped) { 183 | $_SERVER['__test.then.arg'] = $piped; 184 | 185 | return 'then'.$piped; 186 | }); 187 | 188 | $this->assertSame('pipe::then::not_foo::', $result); 189 | $this->assertSame('::not_foo::', $_SERVER['__test.then.arg']); 190 | 191 | unset($_SERVER['__test.then.arg']); 192 | unset($_SERVER['__test.pipe.return']); 193 | } 194 | 195 | public function test_pipeline_usage_with_parameters() 196 | { 197 | $parameters = ['one', 'two']; 198 | 199 | $result = (new Pipeline(new Container)) 200 | ->send('foo') 201 | ->through(PipelineTestParameterPipe::class.':'.implode(',', $parameters)) 202 | ->then(function ($piped) { 203 | return $piped; 204 | }); 205 | 206 | $this->assertSame('foo', $result); 207 | $this->assertEquals($parameters, $_SERVER['__test.pipe.parameters']); 208 | 209 | unset($_SERVER['__test.pipe.parameters']); 210 | } 211 | 212 | public function test_pipeline_via_changes_the_method_being_called_on_the_pipes() 213 | { 214 | $pipelineInstance = new Pipeline(new Container); 215 | $result = $pipelineInstance->send('data') 216 | ->through(PipelineTestPipeOne::class) 217 | ->via('differentMethod') 218 | ->then(function ($piped) { 219 | return $piped; 220 | }); 221 | $this->assertSame('data', $result); 222 | } 223 | 224 | public function test_pipeline_throws_exception_on_resolve_without_container() 225 | { 226 | $this->expectException(RuntimeException::class); 227 | $this->expectExceptionMessage('A container instance has not been passed to the Pipeline.'); 228 | 229 | (new Pipeline)->send('data') 230 | ->through(PipelineTestPipeOne::class) 231 | ->then(function ($piped) { 232 | return $piped; 233 | }); 234 | } 235 | 236 | public function test_pipeline_then_return_method_runs_pipeline_then_returns_passable() 237 | { 238 | $result = (new Pipeline(new Container)) 239 | ->send('foo') 240 | ->through([PipelineTestPipeOne::class]) 241 | ->thenReturn(); 242 | 243 | $this->assertSame('foo', $result); 244 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 245 | 246 | unset($_SERVER['__test.pipe.one']); 247 | } 248 | 249 | public function test_pipeline_conditionable() 250 | { 251 | $result = (new Pipeline(new Container)) 252 | ->send('foo') 253 | ->when(true, function (Pipeline $pipeline) { 254 | $pipeline->pipe([PipelineTestPipeOne::class]); 255 | }) 256 | ->then(function ($piped) { 257 | return $piped; 258 | }); 259 | 260 | $this->assertSame('foo', $result); 261 | $this->assertSame('foo', $_SERVER['__test.pipe.one']); 262 | unset($_SERVER['__test.pipe.one']); 263 | 264 | $_SERVER['__test.pipe.one'] = null; 265 | $result = (new Pipeline(new Container)) 266 | ->send('foo') 267 | ->when(false, function (Pipeline $pipeline) { 268 | $pipeline->pipe([PipelineTestPipeOne::class]); 269 | }) 270 | ->then(function ($piped) { 271 | return $piped; 272 | }); 273 | 274 | $this->assertSame('foo', $result); 275 | $this->assertNull($_SERVER['__test.pipe.one']); 276 | unset($_SERVER['__test.pipe.one']); 277 | } 278 | } 279 | 280 | class PipelineTestPipeOne 281 | { 282 | public function handle($piped, $next) 283 | { 284 | $_SERVER['__test.pipe.one'] = $piped; 285 | 286 | return $next($piped); 287 | } 288 | 289 | public function differentMethod($piped, $next) 290 | { 291 | return $next($piped); 292 | } 293 | } 294 | 295 | class PipelineTestPipeTwo 296 | { 297 | public function __invoke($piped, $next) 298 | { 299 | $_SERVER['__test.pipe.one'] = $piped; 300 | 301 | return $next($piped); 302 | } 303 | } 304 | 305 | class PipelineTestParameterPipe 306 | { 307 | public function handle($piped, $next, $parameter1 = null, $parameter2 = null) 308 | { 309 | $_SERVER['__test.pipe.parameters'] = [$parameter1, $parameter2]; 310 | 311 | return $next($piped); 312 | } 313 | } 314 | -------------------------------------------------------------------------------- /tests/PipelineEventsTest.php: -------------------------------------------------------------------------------- 1 | offsetUnset('events'); 28 | $this->assertFalse(app()->bound('events')); 29 | 30 | app()->register(EnhancedPipelineServiceProvider::class, true); 31 | $this->assertTrue(app()->bound('events')); 32 | } 33 | 34 | public function test_fires_pipeline_started_event() 35 | { 36 | app(Pipeline::class) 37 | ->withEvents() 38 | ->send('data') 39 | ->thenReturn(); 40 | 41 | Event::assertDispatched(function (PipelineStarted $event) { 42 | $this->assertInstanceOf(Closure::class, $event->destination); 43 | $this->assertSame('data', $event->passable); 44 | $this->assertSame([], $event->pipes); 45 | $this->assertFalse($event->useTransaction); 46 | 47 | return true; 48 | }); 49 | } 50 | 51 | public function test_fires_pipeline_finished_event() 52 | { 53 | app(Pipeline::class) 54 | ->withEvents() 55 | ->send('data') 56 | ->thenReturn(); 57 | 58 | Event::assertDispatched(function (PipelineFinished $event) { 59 | $this->assertInstanceOf(Closure::class, $event->destination); 60 | $this->assertSame('data', $event->passable); 61 | $this->assertSame([], $event->pipes); 62 | $this->assertFalse($event->useTransaction); 63 | $this->assertSame('data', $event->result); 64 | 65 | return true; 66 | }); 67 | } 68 | 69 | public function test_fires_pipe_execution_started_event() 70 | { 71 | app(Pipeline::class) 72 | ->withEvents() 73 | ->send('data') 74 | ->through([ 75 | TestPipe::class, 76 | TestPipe::class, 77 | ]) 78 | ->thenReturn(); 79 | 80 | Event::assertDispatched(function (PipeExecutionStarted $event) { 81 | $this->assertInstanceOf(TestPipe::class, app($event->pipe)); 82 | $this->assertSame('data', $event->passable); 83 | 84 | return true; 85 | }, 2); 86 | } 87 | 88 | public function test_fires_pipe_execution_started_event_but_fails_to_finish() 89 | { 90 | app(Pipeline::class) 91 | ->withEvents() 92 | ->send('data') 93 | ->through(PipelineWithException::class) 94 | ->onFailure(fn () => true) 95 | ->thenReturn(); 96 | 97 | Event::assertDispatched(function (PipeExecutionStarted $event) { 98 | $this->assertInstanceOf(PipelineWithException::class, app($event->pipe)); 99 | $this->assertSame('data', $event->passable); 100 | 101 | return true; 102 | }); 103 | 104 | Event::assertNotDispatched(PipeExecutionFinished::class); 105 | } 106 | 107 | public function test_fires_pipe_execution_finished_event() 108 | { 109 | app(Pipeline::class) 110 | ->withEvents() 111 | ->send('data') 112 | ->through([ 113 | TestPipe::class, 114 | TestPipe::class, 115 | ]) 116 | ->thenReturn(); 117 | 118 | Event::assertDispatched(function (PipeExecutionFinished $event) { 119 | $this->assertInstanceOf(TestPipe::class, $event->pipe); 120 | $this->assertSame('data', $event->passable); 121 | 122 | return true; 123 | }, 2); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /tests/PipelineRunTest.php: -------------------------------------------------------------------------------- 1 | run(Action::class); 14 | 15 | $this->assertTrue($executed); 16 | } 17 | 18 | public function test_run_returns_passed_data() 19 | { 20 | $data = ['test' => 'yeah']; 21 | 22 | $executed = Pipeline::make()->run(Action::class, with($data)); 23 | 24 | $this->assertSame('yeah', $executed['test']); 25 | } 26 | 27 | public function test_run_has_customizable_method() 28 | { 29 | $executed = Pipeline::make() 30 | ->via('execute') 31 | ->run(ActionExecute::class); 32 | 33 | $this->assertTrue($executed); 34 | } 35 | 36 | public function test_run_has_customizable_method_via_container() 37 | { 38 | $this->app->resolving(Pipeline::class, function ($pipeline) { 39 | return $pipeline->via('execute'); 40 | }); 41 | 42 | $executed = Pipeline::make()->run(ActionExecute::class); 43 | 44 | $this->assertTrue($executed); 45 | } 46 | } 47 | 48 | class Action 49 | { 50 | public function handle(mixed $data, \Closure $next): mixed 51 | { 52 | return $next($data); 53 | } 54 | } 55 | 56 | class ActionExecute 57 | { 58 | public function execute(mixed $data, \Closure $next): mixed 59 | { 60 | return $next($data); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/PipelineTest.php: -------------------------------------------------------------------------------- 1 | setContainer(app()) 18 | ->send('data') 19 | ->through(PipelineWithException::class) 20 | ->onFailure(function ($piped) { 21 | return 'error'; 22 | })->then(function ($piped) { 23 | return $piped; 24 | }); 25 | 26 | $this->assertEquals('error', $result); 27 | } 28 | 29 | public function test_exception_is_handled_by_on_failure_with_piped_data_passed() 30 | { 31 | $result = app(\MichaelRubel\EnhancedPipeline\Pipeline::class) 32 | ->send('data') 33 | ->through(PipelineWithException::class) 34 | ->onFailure(function ($piped, $exception) { 35 | $this->assertInstanceOf(\Exception::class, $exception); 36 | 37 | return $piped; 38 | })->then(function ($piped) { 39 | return $piped; 40 | }); 41 | 42 | $this->assertEquals('data', $result); 43 | } 44 | 45 | /** @test */ 46 | public function runs_through_an_entire_pipeline() 47 | { 48 | $function1 = function ($piped, $next) { 49 | $piped = $piped + 1; 50 | 51 | return $next($piped); 52 | }; 53 | 54 | $function2 = function ($piped, $next) { 55 | $piped = $piped + 2; 56 | 57 | return $next($piped); 58 | }; 59 | 60 | $result = Pipeline::make() 61 | ->send(0) 62 | ->through($function1, $function2) 63 | ->thenReturn(); 64 | 65 | $this->assertSame(3, $result); 66 | } 67 | 68 | /** @test */ 69 | public function throws_exception_from_pipeline() 70 | { 71 | $this->expectException(\UnexpectedValueException::class); 72 | 73 | Pipeline::make() 74 | ->send('test') 75 | ->through(fn () => throw new \UnexpectedValueException) 76 | ->thenReturn(); 77 | } 78 | 79 | /** @test */ 80 | public function throws_exception_with_invalid_pipe_type() 81 | { 82 | $this->expectException(BindingResolutionException::class); 83 | 84 | Pipeline::make() 85 | ->send('test') 86 | ->through('not a callable or class string') 87 | ->thenReturn(); 88 | } 89 | 90 | /** @test */ 91 | public function accepts_class_strings_as_pipes() 92 | { 93 | $result = Pipeline::make() 94 | ->send('test data') 95 | ->through(TestPipe::class) 96 | ->thenReturn(); 97 | 98 | $this->assertSame('test data', $result); 99 | } 100 | 101 | /** @test */ 102 | public function successfully_completes_a_database_transaction() 103 | { 104 | $database = DB::spy(); 105 | 106 | Pipeline::make() 107 | ->withTransaction() 108 | ->send('test') 109 | ->through( 110 | fn ($data, $next) => $next($data) 111 | )->thenReturn(); 112 | 113 | $database->shouldHaveReceived('beginTransaction')->once(); 114 | $database->shouldHaveReceived('commit')->once(); 115 | } 116 | 117 | /** @test */ 118 | public function rolls_the_database_transaction_back_on_failure() 119 | { 120 | $database = DB::spy(); 121 | 122 | rescue( 123 | fn () => Pipeline::make() 124 | ->withTransaction() 125 | ->send('test') 126 | ->through(fn () => throw new \UnexpectedValueException) 127 | ->thenReturn(), 128 | ); 129 | 130 | $database->shouldHaveReceived('beginTransaction')->once(); 131 | $database->shouldHaveReceived('rollBack')->once(); 132 | } 133 | 134 | /** @test */ 135 | public function rolls_the_database_transaction_back_on_failure_when_on_failure_method_used() 136 | { 137 | $database = DB::spy(); 138 | 139 | rescue( 140 | fn () => Pipeline::make() 141 | ->withTransaction() 142 | ->send('test') 143 | ->through(fn () => throw new \UnexpectedValueException) 144 | ->onFailure(fn () => true) 145 | ->thenReturn(), 146 | ); 147 | 148 | $database->shouldHaveReceived('beginTransaction')->once(); 149 | $database->shouldHaveReceived('rollBack')->once(); 150 | } 151 | 152 | /** @test */ 153 | public function test_can_override_original_pipeline() 154 | { 155 | $this->app->singleton(OriginalPipeline::class, Pipeline::class); 156 | 157 | $pipeline = app(OriginalPipeline::class); 158 | $this->assertInstanceOf(Pipeline::class, $pipeline); 159 | } 160 | 161 | /** @test */ 162 | public function test_can_override_enhanced_pipeline() 163 | { 164 | $this->app->singleton(Pipeline::class, OriginalPipeline::class); 165 | 166 | $pipeline = app(Pipeline::class); 167 | $this->assertInstanceOf(OriginalPipeline::class, $pipeline); 168 | } 169 | } 170 | 171 | class PipelineWithException 172 | { 173 | public function handle($piped, $next) 174 | { 175 | throw new \Exception('Foo'); 176 | } 177 | } 178 | 179 | class TestPipe 180 | { 181 | public function handle($passable) 182 | { 183 | return $passable; 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | set('testing'); 27 | } 28 | } 29 | --------------------------------------------------------------------------------