1. Go to this page and download the library: Download stellarwp/pipeline 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/ */
stellarwp / pipeline example snippets
use StellarWP\Pipeline\Pipeline;
// Create a new pipeline instance.
$pipeline = new Pipeline();
// Send a string through the pipeline.
$result = $pipeline->send( 'a sample string that is passed through to all pipes.' )
->through(
'ucwords',
'trim'
)->then_return();
// The output would be stored in $result and would be:
// A Sample String That Is Passed Through To All Pipes.
echo $result;
use StellarWP\Pipeline\Pipeline;
// Create a new pipeline instance.
$pipeline = new Pipeline();
// Declare the pipes that you want to run against the
// string in the order you want them to execute.
// The method `pipes()` is an alias of `through()`.
$pipeline->pipes(
'ucwords',
'trim'
);
// Add another pipe to the pipeline.
// The method `add_pipe()` is an alias of `pipe()`.
$pipeline->add_pipe( 'strrev' );
// Declare what you are sending through the pipeline.
$pipeline->send( 'potato ' );
// Process the pipeline and get the result.
// The method `run()` is an alias of `then_return()`.
$result = $pipeline->run();
// The result will be: `otatoP`
echo $result;
use StellarWP\Pipeline\Pipeline;
$pipeline = new Pipeline();
$pipeline->pipes(
static function ( $passable, Closure $next ) {
$passable = str_ireplace( 'All', 'All The', $passable );
return $next( $passable );
},
'ucwords'
);
$pipeline->send( 'a sample string that is passed through to all pipes. ' );
$result = $pipeline->run();
// The output would be stored in $result and would be:
// A Sample String That Is Passed Through To All The Pipes.
echo $result;
use StellarWP\Pipeline\Contracts\Pipe;
class SweetUppercasePipe implements Pipe {
public function handle( $passable, Closure $next ) {
$passable = ucwords( $passable );
return $next( $passable );
}
}
use StellarWP\Pipeline\Contracts\Pipe;
class TrimTheStringPipe implements Pipe {
public function handle( $passable, Closure $next ) {
$passable = trim( $passable );
return $next( $passable );
}
}
use StellarWP\Pipeline\Pipeline;
$pipeline = new Pipeline();
$pipeline->pipes(
new SweetUppercasePipe(),
new TrimTheStringPipe()
);
$pipeline->send( 'a sample string that is passed through to all pipes. ' );
$result = $pipeline->run();
// The output would be stored in $result and would be:
// A Sample String That Is Passed Through To All Pipes.
echo $result;
class DifferentSweetUppercasePipe {
public function execute( $passable, Closure $next ) {
$passable = ucwords( $passable );
return $next( $passable );
}
}
class DifferentTrimTheStringPipe {
public function execute( $passable, Closure $next ) {
$passable = trime( $passable );
return $next( $passable );
}
}
use StellarWP\Pipeline\Pipeline;
$pipeline = new Pipeline();
// Set the method to use the `execute()` method instead of the default `handle()`.
$pipeline->via( 'execute' );
$pipeline->pipes(
new DifferentSweetUppercasePipe(),
new DifferentTrimTheStringPipe()
);
$pipeline->send( 'a sample string that is passed through to all pipes. ' );
$result = $pipeline->run();
// The output would be stored in $result and would be:
// A Sample String That Is Passed Through To All Pipes.
echo $result;
use StellarWP\Pipeline\Pipeline;
$pipeline = new Pipeline();
$pipeline->pipes(
'trim',
static function ( $passable, Closure $next ) {
if ( $passable === 'bork' ) {
return $passable;
}
return $next( $passable );
},
'ucwords'
);
$pipeline->send( 'bork ' );
$result = $pipeline->run();
// The output would be stored in $result and would be: "bork"
// It would not get to the `ucwords` pipe.
echo $result;
$pipeline->send( 'cowbell ' );
$result = $pipeline->run();
// The output would be stored in $result and would be: "Cowbell" because it WOULD get to the `ucwords` pipe due to
// the second pipe only returning if the value is "bork".
echo $result;
use StellarWP\Pipeline\Pipeline;
// Create a new pipeline instance.
$pipeline = new Pipeline();
// Declare the pipes that you want to run against the
// string in the order you want them to execute.
// The method `pipes()` is an alias of `through()`.
$pipeline->pipes(
'ucwords',
'trim'
);
// Declare what you are sending through the pipeline.
$pipeline->send( 'a sample string that is passed through to all pipes. ' );
// Process the pipeline and get the result.
$result = $pipeline->then( static function ( $passable ) {
return str_ireplace( 'A Sample', 'A Nice Long', $passable );
} );
// The output would be stored in $result and would be:
// A Nice Long String That Is Passed Through To All Pipes.
echo $result;
use StellarWP\Pipeline\Pipeline;
use MyProject\Container;
// Create a new container instance.
$container = new Container();
$pipeline = new Pipeline( $container );
// Let's add some classes to the pipeline that we declared in a previous example.
$pipeline->pipes(
SweetUppercasePipe::class,
TrimTheStringPipe::class
);
$pipeline->send( 'a sample string that is passed through to all pipes. ' );
$result = $pipeline->run();
// The output would be stored in $result and would be:
// A Sample String That Is Passed Through To All Pipes.
echo $result;
namespace MyProject\Providers;
use StellarWP\Pipeline\Pipeline;
use MyProject\Container;
use MyProject\Response\Intake_Response;
use MyProject\Response\Failed_Response;
class Service_Provider {
/**
* @var string
*/
const REQUEST_PIPELINE = 'myproject.request-pipeline';
/**
* @var ContainerInterface
*/
protected $container;
/**
* @param ContainerInterface $container
*/
public function __construct( ContainerInterface $container ) {
$this->container = $container;
}
/**
* Register some services into the container.
*/
public function register() {
// Bind `request-pipeline` to the container as a singleton. The first time that `->get( 'request-pipeline' )` is
// called, the pipeline will will be instantiated and returned. Subsequent calls to `->get( 'request-pipeline' )`
// will return the same instance of the pipeline.
$this->container->singleton( self::REQUEST_PIPELINE, function(): Pipeline {
$pipeline = new Pipeline( $this->container );
$pipeline->pipes(
Intake_Response::class,
Failed_Response::class,
);
return $pipeline;
} );
// Bind the class name of Listener to the container. Any time that `->get( Listener::class )` is called, a new
// instance of the Listener will be returned with the `request-pipeline` injected into the constructor.
$this->container->bind( Listener::class, static function ( ContainerInterface $container ): Listener {
return new Listener( $container->get( self::REQUEST_PIPELINE ) );
} );
}
}
namespace MyProject\Response;
use WP_REST_Request;
use WP_REST_Response;
class Response_Transporter {
/**
* @var WP_REST_Request
*/
public $request;
/**
* @var WP_REST_Response
*/
public $response;
/**
* @param WP_REST_Request $request
* @param WP_REST_Response $response
*/
public function __construct( WP_REST_Request $request, WP_REST_Response $response ) {
$this->request = $request;
$this->response = $response;
}
}
namespace MyProject\Response;
use StellarWP\Pipeline\Contracts\Pipe;
use WP_REST_Response;
use WP_Http;
class Intake_Response implements Pipe {
public static $name = 'Response received';
public static $endpoint = '/myproject/v1/borkborkbork';
public function handle( Response_Transporter $transporter, Closure $next ): WP_REST_Response {
// If the response is for the endpoint we're looking for, we'll process it.
// Otherwise, it'll just keep moving through the pipeline.
if ( $transporter->request->get_route() === static::$endpoint ) {
$params = (array) $transporter->response->get_data();
$status = $transporter->response->get_status();
$data = [
'status' => $status,
'params' => $params,
];
/**
* Advertise that we've received the response and what its data is.
*
* @param string $name The name of the response.
* @param array $data The data that was received.
*/
do_action( 'myproject/rest/event', static::$name, $data );
}
// Pass the transporter on to the next pipe in the pipeline.
return $next( $transporter );
}
}
namespace MyProject\Response;
use StellarWP\Pipeline\Contracts\Pipe;
use WP_REST_Response;
class Failed_Response implements Pipe {
public static $name = 'Response failed';
public static $endpoint = '/myproject/v1/borkborkbork';
public function handle( Response_Transporter $transporter, Closure $next ): WP_REST_Response {
// If the response is for the endpoint we're looking for, we'll process it.
// Otherwise, it'll just keep moving through the pipeline.
if ( $transporter->request->get_route() === static::$endpoint ) {
$status = $transporter->response->get_status();
$success = $status >= WP_Http::OK && $status < WP_Http::BAD_REQUEST;
// If the response was successful, let's keep moving through the pipeline.
if ( $success ) {
return $next( $transporter );
}
/**
* Oh no! The response was not successful. Let's notify our application that something went wrong.
*
* @param string $name The name of the response.
* @param array $data The data associated with the error.
*/
do_action( 'myproject/rest/event', static::$name, [
'error-params' => $transporter->response->get_data(),
] );
}
// Pass the transporter on to the next pipe in the pipeline.
return $next( $transporter );
}
}
namespace MyProject\Listeners;
use MyProject\Container;
use MyProject\Response\Response_Transporter;
use WP_REST_Request;
use WP_REST_Response;
class Listener {
/**
* @var Pipeline
*/
protected $response_pipeline;
/**
* @param Pipeline $response_pipeline
*/
public function __construct( Pipeline $response_pipeline ) {
$this->response_pipeline = $response_pipeline;
}
/**
* @param WP_REST_Response $response The response that was received.
* @param WP_REST_Request $request The request that was made.
*/
public function handle_response( WP_REST_Response $response, WP_REST_Request $request ): void {
$response = rest_ensure_response( $response );
if ( is_wp_error( $response ) ) {
$response = rest_convert_error_to_response( $response );
}
return $this->response_pipeline->send( new Response_Transporter( $request, $response ) )->then_return();
}
namespace MyProject;
use MyProject\Container;
use MyProject\Listeners\Listener;
use MyProject\Providers\Service_Provider;
// Typically the next three lines would be done in a more application-relevant location, however, for the sake of
// this example, we'll just
$response = rest_do_request( $request );
// Get an instance of the Listener class.
$listener = $container->get( Listener::class );
// Pass the request to the listener, which will invoke the pipeline.
$listener->handle_response( $response, $request );
// If the request was successful, the `myproject/rest/event` action will be fired once to indicate
// that the response was received.
// If the request was NOT successful, the `myproject/rest/event` action will be fired once to indicate
// that the response was received. And a second time to indicate that there was an error.
// We can do the same thing for other requests.
// Likewise, these lines would likely be done in a class somewhere. These would probably live in different classes.
$request = new WP_REST_Request( 'GET', '/myproject/v1/something-else' );
$response = rest_do_request( $request );
$listener = $container->get( Listener::class );
$listener->handle_response( $response, $request );
$request = new WP_REST_Request( 'GET', 'myproject/v1/borkborkbork' );
$response = rest_do_request( $request );
$listener = $container->get( Listener::class );
$listener->handle_response( $response, $request );
public function pipe( array|mixed $pipes ): self
$pipeline->pipe( 'ucwords' );
// or
$pipeline->add_pipe( 'ucwords' );
// or
$pipeline->pipe( [ 'ucwords', 'trim' ] );
public function send( mixed $passable ): self
// Send a scalar.
$pipeline->send( 'Some string' );
// Send an object.
$pipeline->send( $my_object );
public function set_container( ContainerInterface $container ): self
$pipeline->set_container( $container );
public function then( Closure $destination = null ): mixed
$pipeline->then();
// Use the alias.
$pipeline->run();
// Provide a function to run before returning the result.
$pipeline->then( 'trim' );
// Provide a closure to run before returning the result.
$pipeline->then( static function ( $passable ) {
return trim( $passable );
} );
// Provide an object as a pipe to run before returning the result.
$pipeline->then( new TrimTheStringPipe() );
// Provide an class name as a pipe to run before returning the result.
$pipeline->then( TrimTheStringPipe::class );
public function then_return(): mixed
$pipeline->then_return();
// Use an alias.
$pipeline->thenReturn();
// Use the other alias.
$pipeline->run_and_return();
public function through( array|mixed $pipes ): self
// You can provide any number of pipes.
$pipeline->through( 'ucwords', 'trim' );
// Using the alias.
$pipeline->pipes( 'ucwords', 'trim' );
// Pass an array of pipes.
$pipeline->through( [ 'ucwords', 'trim' ] );
// Pass closures as pipes.
$pipeline->through( static function ( $passable, Closure $next ) {
$passable = str_ireplace( 'All', 'All The', $passable );
return $next( $passable );
} );
// Pass objects as pipes.
$pipeline->through( new SweetUppercasePipe(), new TrimTheStringPipe() );
// If you have a container, you can pass class names as pipes.
$pipeline->through( SweetUppercasePipe::class, TrimTheStringPipe::class );
public function via( string $method ): self
// Set the method used in all classes in the pipeline to process the data as `execute()`.
$pipeline->via( 'execute' );
// Set the method used in all classes in the pipeline to process the data as `borkborkbork()`.
$pipeline->via( 'borkborkbork' );