2017 © Pedro Peláez
 

library class-triggers

Inject magic actions before and after any method of any class

image

isra00/class-triggers

Inject magic actions before and after any method of any class

  • Sunday, April 29, 2018
  • by isra00
  • Repository
  • 1 Watchers
  • 5 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Inject logic to any method of any PHP class, dinamically

Build Status, (*1)

Partial and experimental implementation of Aspect-Oriented Programming. It gives you the ability to capture and manipulate the inputs, outputs and execution of any method of any class in PHP., (*2)

Basic usage

$a = new AnyClass;
$a = new ClassTriggers($a);

$a->bind($targetMethod, $trigger, function($arguments) {
    //Injected logic.
});

Example

use ClassTriggers\ClassTriggers;

class Car
{
    private $position = 0;

    public function forward($increment)
    {
        $this->position += $increment;
    }

    public function getPosition()
    {
        return $this->position;
    }
}

$interceptedCar = new ClassTriggers(new Car);

$interceptedCar->bind('forward', 'preMethod', function(&$arguments) {
    echo 'You have requested forward(' . $arguments[0] . ")\n";

    if ($this->position + $arguments[0] > 10) {
        echo "Can't forward more than 10. Stopping method execution.\n";
        return ClassTriggers::COND_STOP_EXECUTION;
    }
});

Requiremens

  • PHP 5.4
  • Composer

Installation

You can install it via Composer, and use it instantly thanks to the Composer autoloader:, (*3)

"require": {
    "isra00/class-triggers": "dev-master"
}

More info

I wrote a tutorial in Spanish: https://github.com/isra00/blog/blob/master/2012-12-14-inyectar-logica-en-los-metodos-de-cualquier-clase-php-dinamicamente.md, (*4)

If you have any comments, ideas, questions... don't hesitate to raise an issue in GitHub :-), (*5)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

The Versions

29/04 2018

dev-master

9999999-dev http://github.com/isra00/class-triggers

Inject magic actions before and after any method of any class

  Sources   Download

LGPL v3.0 LGPL-3.0-or-later

The Requires

  • php >=5.4

 

magic classes objects methods aop triggers