This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support zend stratigility pipe
- Loading branch information
Showing
8 changed files
with
318 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.