PHP Classes
elePHPant
Icontem

Decorate: Alter functions running code before and after

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-02-02 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 343 All time: 6,450 This week: 1,164Up
Version License PHP version Categories  
decorate 1BSD License5.4PHP 5, Design Patterns
Description Author  

This class can alter functions adding code before and after they run.

It implements the decorator design pattern by taking a function and creating a new one that executes another given function before or after the original function is run.

The class returns the newly created function as a reference to the callable function.

Innovation Award  
PHP Programming Innovation award nominee
February 2014
Number 5
Sometimes you need to alter the behavior of functions by doing something before or after the original function code. That is the purpose of the decorator design pattern.

This class implements the decorator pattern by calling custom code before or after the original function code.

Manuel Lemos
Picture of jstar88
Name: jstar88 <contact>
Classes: 6 packages by
Country: Italy Italy
Age: ???
All time rank: 146158 in Italy Italy
Week rank: 1628 Down63 in Italy Italy Down
Innovation award
Innovation award
Nominee: 2x

Details provided by the author  
Decorate
========

A PHP class usefull to decorate functions


## Introduction

This class represent the beginning of functional programming in PHP.  
The decorator patter can easly make your application modular while functions transform it in to an event-driven application.  

The goal of this class is to decorate functions, taking advantage of objects to make a queue-like structure and store in it partial results and functions.

Note that with the other my class https://github.com/jstar88/Editable it can show you a full dynamic decorated system usefull, for example, for a plugin system or to build a modular application.


## Usage

Usage it's pretty easy: require the main class

```php 
 require "Decorate.php";
 ```
 
 and then you can use its 2 methods:
 
### onBefore
 
 ```php
 $function = Decorate::onBefore($function, $newFunction);
 ```
 
 Where
 
 * ```$function``` is the callable function to intercept
 * ```$newFunction``` is the callable function that will be called before the other one.Note that the arguments of this function are the same of ```$function```, in this way you have all the things usefull to build your interceptor-logic
 * the method return a ```FunctionEmulator``` class
 
### onAfter
 
 ```php
 $function = Decorate::onAfter($function, $newFunction);
 ```
 
Where
 
 * ```$function``` is the callable function
 * ```$newFunction``` is the callable function that will be called after the other one.Note that the argument of this function is just the return of the other function
 * the method return a ```FunctionEmulator``` class
 
### the return: FunctionEmulator

This object is really a function emulator and you can use it as a standard function, but you also call its method!  
Expecially you can use this object to decorate him again like a callable function.  
Not only,it's a object where inside are stored the original function and the new one.  You can use these methods:

* ```getPreReturn()```: this function can be called with how many arguments you want and they are the same arguments of your main function to call.  
It will return the original function return.

* ```getParent()```: this function will return the original function or older FunctionEmulator. In this way you can iterate on the history and find all the result of aggregated functions. 

## Advanced usage

If have you noticed that functions aggregations can be represented and easily manipulated by a LinkedList, then you got it!  
I provide two implementations of LikedList:  
 
 * ```OnAfterDecorativeLinkedList```
 * ```OnBeforeDecorativeLinkedList```

These classes extends the standard PHP ```SplDoublyLinkedList``` so you can add,replace,move etc functions as API say.  
Also ,implementing the ```FunctionEmulable``` interface they can be called as functions.  
An example:  

```php
    require '../../Decorate.php';

    $a = new OnAfterDecorativeLinkedList();
    $a->push(function(){ echo 1;});
    $a->push(function(){ echo 2;});
    $a->push(function(){ echo 3;});
    $a(); // print 123

    echo '<br>';

    $a = new OnBeforeDecorativeLinkedList();
    $a->push(function(){ echo 1;});
    $a->push(function(){ echo 2;});
    $a->push(function(){ echo 3;});
    $a();  // print 321
    ```
  Files folder image Files  
File Role Description
Files folder imageADT (2 files)
Files folder imagecore (4 files)
Files folder imageexamples (4 directories)
Plain text file Decorate.php Class The main class
Accessible without login Plain text file LICENSE.mid Lic. License text
Accessible without login HTML file offline-doc.html Doc. doc
Accessible without login Plain text file README.md Doc. git documentation

  Files folder image Files  /  ADT  
File Role Description
  Plain text file OnAfterDecorativeLinkedList.php Class Class source
  Plain text file OnBeforeDecorativeLinkedList.php Class Class source

  Files folder image Files  /  core  
File Role Description
  Plain text file FunctionEmulable.php Class Class source
  Plain text file FunctionEmulator.php Class Class source
  Plain text file OnAfterFunctionEmulator.php Class Class source
  Plain text file OnBeforeFunctionEmulator.php Class Class source

  Files folder image Files  /  examples  
File Role Description
Files folder imageADT (1 file)
Files folder imageaggregations (1 file)
Files folder imageonAfter (1 file)
Files folder imageonBefore (2 files)

  Files folder image Files  /  examples  /  ADT  
File Role Description
  Accessible without login Plain text file linkedlist.php Example Example script

  Files folder image Files  /  examples  /  aggregations  
File Role Description
  Accessible without login Plain text file strings.php Example Example script

  Files folder image Files  /  examples  /  onAfter  
File Role Description
  Accessible without login Plain text file round.php Example Example script

  Files folder image Files  /  examples  /  onBefore  
File Role Description
  Accessible without login Plain text file bandwidth.php Example Example script
  Accessible without login Plain text file login.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%Total:343All time:6,450
 This week:0This week:1,164Up
 User Comments (1)  
 
nice class!
1 year ago (Ovunc Tukenmez)
80%StarStarStarStarStar