Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Dec 13, 2016
0 parents commit 783cb3f
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
phpunit/
vendor/
composer.lock

.idea/
24 changes: 24 additions & 0 deletions .travis.yml
@@ -0,0 +1,24 @@
language: php

php:
- 7.0
- 7.1

matrix:
include:
- php: 7.0
env: dependencies=lowest
- php: 7.0
env: dependencies=highest
- php: 7.1
env: dependencies=lowest
- php: 7.1
env: dependencies=highest

before_script:
- composer self-update -q
- if [ -z "$dependencies" ]; then composer install; fi;
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest -n; fi;
- if [ "$dependencies" = "highest" ]; then composer update -n; fi;

script: phpunit --coverage-text --verbose
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2016 Dominik Zogg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
@@ -0,0 +1,43 @@
# chubbyphp-lazy-middleware

[![Build Status](https://api.travis-ci.org/chubbyphp/chubbyphp-lazy-middleware.png?branch=master)](https://travis-ci.org/chubbyphp/chubbyphp-lazy-middleware)
[![Total Downloads](https://poser.pugx.org/chubbyphp/chubbyphp-lazy-middleware/downloads.png)](https://packagist.org/packages/chubbyphp/chubbyphp-lazy-middleware)
[![Latest Stable Version](https://poser.pugx.org/chubbyphp/chubbyphp-lazy-middleware/v/stable.png)](https://packagist.org/packages/chubbyphp/chubbyphp-lazy-middleware)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/chubbyphp/chubbyphp-lazy-middleware/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/chubbyphp/chubbyphp-lazy-middleware/?branch=master)

## Description

Allow to lazyload commands, middlewares.

## Requirements

* php: ~7.0
* container-interop/container-interop: ~1.1
* psr/http-message: ~1.0

## Installation

Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-lazy-middleware][1].

## Usage

```{.php}
<?php
use Chubbyphp\Lazy\LazyMiddleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
$container['service'] = function (Request $request, Response $response) {
// run some lazy logic
};
$middleware = new LazyMiddleware($container, 'service');
$response = $middleware($request, $response);
```

[1]: https://packagist.org/packages/chubbyphp/chubbyphp-lazy-middleware

## Copyright

Dominik Zogg 2016
28 changes: 28 additions & 0 deletions composer.json
@@ -0,0 +1,28 @@
{
"name": "chubbyphp/chubbyphp-lazy-middleware",
"description": "Chubbyphp Lazy Middleware",
"keywords": ["chubbyphp", "lazy", "middleware", "psr7", "slim"],
"license": "MIT",
"authors": [
{
"name": "Dominik Zogg",
"email": "dominik.zogg@gmail.com"
}
],
"require": {
"php": "~7.0",
"container-interop/container-interop": "~1.0",
"psr/http-message": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~5.5"
},
"autoload": {
"psr-4": { "Chubbyphp\\Lazy\\": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
26 changes: 26 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="chubbyphp lazy middleware">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="phpunit/coverage" />
</logging>
</phpunit>
46 changes: 46 additions & 0 deletions src/LazyMiddleware.php
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Lazy;

use Interop\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

final class LazyMiddleware
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @var string
*/
private $serviceId;

/**
* @param ContainerInterface $container
* @param string $serviceId
*/
public function __construct(ContainerInterface $container, $serviceId)
{
$this->container = $container;
$this->serviceId = $serviceId;
}

/**
* @param Request $request
* @param Response $response
* @param callable|null $next
*
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $next = null)
{
$middleware = $this->container->get($this->serviceId);

return $middleware($request, $response, $next);
}
}
67 changes: 67 additions & 0 deletions tests/LazyMiddlewareTest.php
@@ -0,0 +1,67 @@
<?php

namespace Chubbyphp\Tests\Lazy;

use Chubbyphp\Lazy\LazyMiddleware;
use Interop\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

/**
* @covers Chubbyphp\Lazy\LazyMiddleware
*/
final class LazyMiddlewareTest extends \PHPUnit_Framework_TestCase
{
public function testInvoke()
{
$container = $this->getContainer([
'service' => function (Request $request, Response $response, callable $next = null) {
return $response;
},
]);

$request = $this->getRequest();
$response = $this->getResponse();

$middleware = new LazyMiddleware($container, 'service');

self::assertSame($response, $middleware($request, $response));
}

/**
* @param array $services
*
* @return ContainerInterface
*/
private function getContainer(array $services): ContainerInterface
{
/** @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */
$container = $this->getMockBuilder(ContainerInterface::class)->setMethods(['get'])->getMockForAbstractClass();

$container
->expects(self::any())
->method('get')
->willReturnCallback(function (string $id) use ($services) {
return $services[$id];
})
;

return $container;
}

/**
* @return Request|\PHPUnit_Framework_MockObject_MockObject
*/
private function getRequest(): Request
{
return $this->getMockBuilder(Request::class)->setMethods([])->getMockForAbstractClass();
}

/**
* @return Response|\PHPUnit_Framework_MockObject_MockObject
*/
private function getResponse(): Response
{
return $this->getMockBuilder(Response::class)->setMethods([])->getMockForAbstractClass();
}
}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,4 @@
<?php

$loader = require __DIR__.'/../vendor/autoload.php';
$loader->setPsr4('Chubbyphp\Tests\Lazy\\', __DIR__);

0 comments on commit 783cb3f

Please sign in to comment.