Skip to content

Added syntax highlighting in code samples #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,54 @@ Enables the usage of callables as stack middlewares.
Here is a contrived example showing how a callable can be used to easily act
as a stack middleware for a silex application:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
```php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

$app = new Silex\Application();
$app = new Silex\Application();

$app->get('/', function (Request $request) {
if ('success' === $request->attributes->get('callable_middleware')) {
return new Response('SUCCESS');
}
$app->get('/', function (Request $request) {
if ('success' === $request->attributes->get('callable_middleware')) {
return new Response('SUCCESS');
}

return new Response('FAILED', 500);
});
return new Response('FAILED', 500);
});

$inlineMiddleware = function(
HttpKernelInterface $app,
Request $request,
$type = HttpKernelInterface::MASTER_REQUEST,
$catch = true
) {
$request->attributes->set('callable_middleware', 'success');
$inlineMiddleware = function(
HttpKernelInterface $app,
Request $request,
$type = HttpKernelInterface::MASTER_REQUEST,
$catch = true
) {
$request->attributes->set('callable_middleware', 'success');

$response = $app->handle($request, $type, $catch);
$response->setContent('['.$response->getContent().']');
$response = $app->handle($request, $type, $catch);
$response->setContent('['.$response->getContent().']');

return $response;
};
return $response;
};

$stack = (new Stack\Builder())
->push('Stack\Inline', $inlineMiddleware);

$app = $stack->resolve($app);
$stack = (new Stack\Builder())
->push('Stack\Inline', $inlineMiddleware);

$app = $stack->resolve($app);
```

## Usage

The method signature for the callable is similar to `HttpKernelInterface::handle`
except that it requires an `HttpKernelInterface` instance as its first argument.
A simple passthru inline middleware would look like this:

$app = new Stack\Inline($app, function(
HttpKernelInterface $app,
Request $request,
$type = HttpKernelInterface::MASTER_REQUEST,
$catch = true
) {
return $app->handle($request, $type, $catch);
});
```php
$app = new Stack\Inline($app, function(
HttpKernelInterface $app,
Request $request,
$type = HttpKernelInterface::MASTER_REQUEST,
$catch = true
) {
return $app->handle($request, $type, $catch);
});
```