Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
added support zend stratigility pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ecow committed Oct 1, 2018
1 parent 75efe7a commit 9b7c3ee
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 10 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
[![Build Status](https://scrutinizer-ci.com/g/linkeddatacenter/uSilex/badges/build.png?b=master)](https://scrutinizer-ci.com/g/linkeddatacenter/uSilex/build-status/master)


µSilex (aka micro silex) is a micro framework inspired by Pimple and PSR standards. All with less tha 100 lines of code!
µSilex (aka micro silex) is a micro framework inspired by Pimple and PSR standards. All with less than 100 lines of code!

This project is a try to build a standard conceptual framework for developing micro-services and
This project is a try to build a standard middleware framework for developing micro-services and
APIs endpoints that require maximum performances with a minimum of memory footprint.

Why [Pimple](https://pimple.symfony.com/)? Because it is lazy, consistent, elegant and small (about 80 lines of code). What else?
Why [Pimple](https://pimple.symfony.com/)? Because it is lazy, consistent, fast, elegant and small (about 80 lines of code). What else?

Why [PSR standards](https://www.php-fig.org/psr)? Because it is a successful community project with a lot of good implementations.
Why [PSR standards](https://www.php-fig.org/psr)? Because it is a successful community project with a lot of good introperable implementations (psr15-middlewares, zend stratigility, Guzzle, etc. etc.).

Why µSilex? Silex was a great framework now abandoned in favour of Symfony + Flex. This is good when you need more power and flexibility. But you have to pay a price in terms of complexity and memory footprint.
µSilex it is a new project that covers a small subset of the original Silex project: a µSilex Application is just a Pimple Container implementing the [PSR-15 specifications](https://www.php-fig.org/psr/psr-15/). That's it.
µSilex it is a new project that covers a small subset of the original Silex project: a µSilex Application is just a Pimple Container implementing all [PSR-15 specifications](https://www.php-fig.org/psr/psr-15/). That's it.

As a matter of fact, in the JAMStack, Docker and XaaS era, you can let lot of conventional framework features to other components in the system application architecture (i.e. caching, authentication, security, monitoring, etc. etc).

Expand Down Expand Up @@ -79,7 +79,7 @@ There are tons of libraries that implement great reusable middleware that are fu

You can create your custom framework just selecting the the components that fit your needs.

out-of-the-box µSilex give to you a set of Service Providers (in the src/Provider directory ) and a set of traits (in the src/Application directory).
out-of-the-box µSilex give to you a set of Service Providers (in the src/Provider directory )


µSilex also provides the *Psr11Trait* as an helper to declare a constructor to inject a Pimple Container with PSR11 interface in any class.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "library",
"name": "linkeddatacenter/usilex",
"description": "Super lightweight framework based on a subset of SILEX. Great for micro services and JAMstack",
"keywords": ["LinkedData.Center", "PIMPLE", "SILEX", "psr15", "psr-15", "PSR", "micro services", "JAMstack"],
"keywords": ["LinkedData.Center", "Pimple", "Silex", "psr-15", "PSR", "micro services", "JAMstack", 'middleware'],
"authors": [
{
"name": "Enrico Fagnoni",
Expand Down Expand Up @@ -32,6 +32,7 @@
"middlewares/error-handler": "^1.2",
"middlewares/aura-router": "^1.1",
"middlewares/request-handler": "^1.3",
"relay/relay": "2.x@dev"
"relay/relay": "2.x@dev",
"zendframework/zend-stratigility": "^3.0"
}
}
163 changes: 162 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions html/example4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
require_once __DIR__.'/../vendor/autoload.php';
$time_start = microtime(true);

use uSilex\Application;
use uSilex\Provider\Psr15\ZendPipeServiceProvider;
use uSilex\Provider\Psr7\DiactorosServiceProvider;

use Zend\Diactoros\Response;
use Zend\Diactoros\Server;
use Zend\Stratigility\Middleware\NotFoundHandler;

use function Zend\Stratigility\middleware;
use function Zend\Stratigility\path;

$app = new Application;
$app->register( new ZendPipeServiceProvider());
$app->register( new DiactorosServiceProvider());
$app['basepath'] = '/example4.php';

// Landing page
$app['piper']->pipe(middleware(function ($req, $handler) use ($app) {
if (! in_array($req->getUri()->getPath(), [$app['basepath'].'/', $app['basepath']], true)) {
return $handler->handle($req);
}

$response = new Response();
$response->getBody()->write("This is the home. Try '/hello'");

return $response;
}));
$app['message'] = "Hello World";

// Another page
$app['piper']->pipe(path($app['basepath'].'/hello', middleware(function ($req, $handler) {
$response = new Response();
$response->getBody()->write("Hello. Try '/hello/world'");

return $response;
})));

// Another page
$app['piper']->pipe(path($app['basepath'].'/foo', middleware(function ($req, $handler) use ($app) {
$response = new Response();
$response->getBody()->write($app['message']);

return $response;
})));

// 404 handler
$app['piper']->pipe(new NotFoundHandler(function () {
return new Response();
}));


$app->run();

echo "\n<pre>";
echo "\nmemory_get_usage: ".memory_get_usage ();
echo "\nscript execution time:". (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]);
echo "\n</pre>";
1 change: 1 addition & 0 deletions html/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<li><a href="example2.php">example2</a> like example 1 but using service providers and middleware.</li>
<li><a href="example3.php/hello/world">example3</a> implements a full flagged routing with error management.
Use configuration files in example_3 directory.</li>
<li><a href="example4.php/">example4</a> same example3 but using zend stratigility library.</li>
</ul>

<a href="none.php">Compare time and memory usage</a> with the script that just echo "hello world".
60 changes: 60 additions & 0 deletions src/Provider/Psr15/ZendPipeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the uSilex framework.
*
* (c) Enrico Fagnoni <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* This service provider uses https://docs.zendframework.com/zend-stratigility to define
* uSilex.httpHandler service.
*
* Add this dependency to your project:
*
* composer require zendframework/zend-stratigility
*
* USAGE:
* you need do define the service handler.queue that contains the list
* of middleware to execute. You can use the id of a service that realize a middleware,
* a concrete middleware instance or a callable with the signatur recognized by relay
*
* $app->register( new ZenPipeServiceProvider() );
* $app['piper']->pipe(path('/foo', middleware(function ($req, $handler) {
* $response = new Response();
* $response->getBody()->write('FOO!');
*
* return $response;
* })));
*
* $app['piper']->pipe(new NotFoundHandler(function () {
* return new Response();
* }));
*/
namespace uSilex\Provider\Psr15;

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Zend\Stratigility\MiddlewarePipe;


class ZendPipeServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}
*/
public function register(Container $app)
{
$app['piper']= function($app) {
return new MiddlewarePipe();
};

$app['uSilex.httpHandler'] = function($app) {
return $app['piper'];
};
}

}
1 change: 0 additions & 1 deletion tests/unit/Provider/Psr15/RelayServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Pimple\ServiceProviderInterface;
use uSilex\Application;
use uSilex\Provider\Psr15\RelayServiceProvider;
use Relay\Relay;


class RelayServiceProviderTest extends TestCase
Expand Down
Loading

0 comments on commit 9b7c3ee

Please sign in to comment.