Download the PHP package mcustiel/power-route without Composer

On this page you can find all versions of the php package mcustiel/power-route. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package power-route

PowerRoute! is a PHP routing system that can execute different sets of actions based in several components of the HTTP requests and is fully compatible with PSR-7.

The configuration is formed by three main components and defines a binary tree:

In the configuration the actions can be set for the case in which the matcher returns true and for the case in which it returns true, hence building a binary tree.

The full system can be extended by adding input sources, matchers and actions. Also the names used in the configuration to identify the components can be assigned arbitrarily.

The components are grouped forming the nodes of the binary tree, each node looks as following:

Build Status Code Coverage Scrutinizer Code Quality PPM Compatible

SensioLabsInsight

Table of contents

Installation

This project is published in packagist, so you just need to add it as a dependency in your composer.json:

How to use

The configuration

The configuration must be a php array. It must define two keys:

Example

A configuration that always redirects to google.com:

You can use the names you prefer for the input sources, the matchers and the actions and then map them in the factories provided by this library.

The code

After all the configuration is correctly defined, Executor class must be used to walk the graph based in the request received. To create an instance of Executor class, the factories for Actions, Input Sources and Matchers must be created first. Each factory constructor expects an array of Mcustiel\Creature\CreatorInterface objects, indexed by the identificator of the class used in PowerRoute! config file. See following example:

After you have your executor instance, just call start method with the PSR7 request and response:

Or, to boost it up, you can use PHP-PM:

and run it as:

Predefined components

Input sources

Cookie

Allows to match the request body.

Cookie

Allows to execute actions based in cookies from the http request.

Arguments

The name of the cookie.

Header

Allows to execute actions based in headers from the http request.

Arguments

The name of the header.

Method

Returns the http method used to execute request. It receives no parameters.

QueryStringParam

Allows to execute actions based in parameters from the request's query string.

Arguments

The name of the query string parameter.

Url

Allows to execute actions based in the url or parts of it.

Arguments

A string specifying the part of the url to evaluate. With the following possible values:

Matchers

CaseInsensitiveEquals

Useful to compare two strings without taking case into account.

Contains

This matcher returns true if the value from the input source contains as a substring the value received as an argument.

Equals

Returns true if the value from the input source is equal to another value received as argument.

InArray

Returns true if the value from the input source is in a list of values received as argument.

NotEmpty

Returns true if the value from the input source is not empty.

NotNull

Returns true if the value from the input source is not null.

RegExp

Returns true if the value from the input source matches a regular expression received as argument.

Actions

Goto

This is a default action that is always added, it's identifier is the string 'goto'. It allow to jump the execution to another node. It's argument is the name of the node to execute.

DisplayFile

This action displays a file. Its path must be defined as argument.

NotFound

This action sets the http status code to 404 in the response.

Redirect

This action adds a Location header to the response and set the http status code to 302. Its redirection target must be defined as argument.

SaveCookie

This action sets the value of a cookie. It receives as an argument an object with all the needed data for the cookie:

ServerError

Sets the response statusCode to 500. Other error statusCode can also be passed as argument. On invalid error given, sets 500.

SetHeader

This action sets the value of a header. As an argument receives an object with the following keys:

StatusCode

Sets the response statusCode to 200 as default. Other error statusCode can be passed as argument. On invalid error throws an exception.

Extending PowerRoute!

Creating your own actions

To create your own actions to be used through PowerRoute! you have to create a class implementing ActionInterface. If you want to give your action the ability to support placeholders, you you must use PlacheolderEvaluator trait.

TransactionData is an object that is passed as an argument to all actions, it is used to share the request, the response and other data that you may want to share between them.

Inside an action you should retrieve the object you want to modify from TransactionData (request or response object). Then you modify it and set the new object again in TransactionData. This must be done this way because PSR7 are immutable.

You can even init a framework inside an action.

Using PSR7 middleware:

PowerRoute! supports psr-7 middlewares as actions. All you need to do is to map the action name in the config to a class implementing the following method:

You can also map the action name to a callable with that signature.

PowerRoute! will call the middleware and pass the configured argument as the $next argument.

Example:

For the action config:

And the factory setup:

PowerRoute will do something like this:

Examples of an action:

TransactionData class:

This class is passed as an argument to every action and defines two methods to access the current request and the corresponding response (getRequest and getResponse respectively). Also it gives you the ability to save and fetc custom variables throught get($name) and set($name, $value) methods.

Placeholders:

The arguments an action receives can include a placeholder to access values from the TransactionData object. The arguments have the following format:

Where source indicates from where to obtain the value, and name is the identifier associated with the given value.

Possible placeholder sources:

** Note: See PSR7 documentation for more information about previous sources.

Creating your own input sources

The input source is the component used to access data from the request, it uses a matcher uses to validate the data and the request.

It also should extend AbstractArgumentAware to have access to the argument from the configuration and it must implement InputSourceInterface. It must return the value so PowerRoute! gives it to the matcher.

Example of an InputSource:

Creating your own matchers

The matcher is the component in charge of executing a check against the value obtained from the request by the InputSource. To create your own matcher, you must create a class that should extend AbstractArgumentAware to access the argument and must implement MatcherInterface.

Example of a Matcher:

Examples

Phiremock uses PowerRoute, you can check it's config file here.


All versions of power-route with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6
mcustiel/creature Version ^2.0.0
zendframework/zend-diactoros Version ^1.1.3 || ^2.0
mcustiel/mockable-datetime Version ^1.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mcustiel/power-route contains the following files

Loading the files please wait ....