PHP code example of psg / sr2-implementation

1. Go to this page and download the library: Download psg/sr2-implementation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

psg / sr2-implementation example snippets


use Psg\Sr1\Implementation\Factory\Sr1Factory;
use Psg\Sr2\Implementation\{Beforeware, Frontware, Middleware, Backware, Afterware, LayeredApp};

$core = function($request, $response, $app){
	echo "Request Headers:\n";
	var_export($request->getHeaders());
	echo "\nadding body\n";
	return $response->withBodyString("\nBODY\n");
};

$App = new LayeredApp;
$App->add(new Beforeware);
$App->add(new Frontware);
$App->add(new Middleware);
$App->add(new Middleware);
$App->add(new Backware);
$App->add(new Afterware);
$App->core($core);

$Factory = new Sr1Factory();
$request = $Factory->createServerRequest('GET', 'http://bobery.com');
$App->handle($request);

/*>
Before
Front
Middle
wrap1{
Middle
wrap2{
Request Headers:
array (
  'Host' =>
  array (
    0 => 'bobery.com',
  ),
  'frontware' =>
  array (
    0 => '1',
  ),
  'middleware' =>
  array (
    0 => '1',
    1 => '2',
  ),
)
adding body
}wrap2
}wrap1
Back

==========RESPONSE {=============
array (
  'middleware' =>
  array (
    0 => '2',
    1 => '1',
  ),
  'Backware' =>
  array (
    0 => '1',
  ),
)wrap1{
wrap2{

BODY
}wrap2
}wrap1

==========} RESPONSE=============
After
*/

# Exit in frontware
$App = new LayeredApp;
$App->add(new Beforeware);
$App->add(new Frontware(['exit'=>true]));
$App->add(new Middleware);
$App->add(new Middleware);
$App->add(new Backware);
$App->add(new Afterware);
$App->core($core);
/*>
Before
Front
Back

==========RESPONSE {=============
array (
  'exit_at' =>
  array (
    0 => 'frontware',
  ),
  'Backware' =>
  array (
    0 => '1',
  ),
)
==========} RESPONSE=============
After
*/

# Exit in middleware
$App = new LayeredApp;
$App->add(new Beforeware);
$App->add(new Frontware);
$App->add(new Middleware(['exit'=>true]));
$App->add(new Middleware);
$App->add(new Backware);
$App->add(new Afterware);
$App->core($core);

/*>
Before
Front
Middle
Back

==========RESPONSE {=============
array (
  'exit_at' =>
  array (
    0 => 'middleware id 1',
  ),
  'Backware' =>
  array (
    0 => '1',
  ),
)
==========} RESPONSE=============
After
*/