2017 © Pedro Peláez
 

library weaver

An experiment in compositional programming.

image

intahwebz/weaver

An experiment in compositional programming.

  • Thursday, November 6, 2014
  • by Danack
  • Repository
  • 1 Watchers
  • 0 Stars
  • 312 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 15 Versions
  • 3 % Grown

The README.md

Weaver

An experiment in compositional programming. Inspired by https://github.com/Ocramius/ProxyManager and https://github.com/ejsmont-artur/phpProxyBuilder, (*1)

Build Status, (*2)

Why?

The two projects listed above are both good ideas, but have serious limitations. The phpProxyBuilder is does not generate correct type-hinting information, which completely stops me from being able to use it., (*3)

The ProxyManager is nice, but has issues with how it interacts with other code and seems to make debugging code incredibly hard. I also couldn't see how to implement a caching proxy., (*4)

This project is an attempt to allow generating various types of decorated versions of classes with a goal of:, (*5)

  • Great flexibility on how they're used., (*6)

  • Retaining ability to debug code., (*7)

  • Low overhead both mentally and by code weight., (*8)

  • Keep type-hinting intact to allow Auryn DI to work correctly., (*9)

Example

We have a class and we want to be able to time the calls to 'executeQuery', (*10)


class TestClass { function __construct($statement, $createLine){ $this->statement = $statement; $this->createLine = $createLine; } function executeQuery($queryString, $foo2) { echo "executing query!"; return 5; } }

Weaved with a 'class' to that holds a timer:, (*11)

<?php


namespace Weaver\Weave;

use Intahwebz\Timer;


class TimerProxy {

    private $timer;

    function __construct(Timer $timer) {
        $this->timer = $timer;
    }

    function reportTimings() {
        $this->timer->dumpTime();
    }
}

And with a tiny bit of glue to bind the two:, (*12)

$timerWeaving = array(
    'executeQuery' => array(
        '$this->timer->startTimer($queryString);', 
        '$this->timer->stopTimer();'
    ),
);

Produces a decorated class:, (*13)

<?php
namespace Example;

use Intahwebz\Timer;

class TimerProxyXTestClass extends \Example\TestClass
{

    private $timer = null;

    public function executeQuery($queryString, $foo2)
    {
        $this->timer->startTimer($queryString);
        $result = parent::executeQuery($queryString, $foo2);
        $this->timer->stopTimer();

        return $result;
    }

    public function reportTimings()
    {
        $this->timer->dumpTime();
    }

    public function __construct($statement, $createLine, \Intahwebz\Timer $timer)
    {
        parent::__construct($statement, $createLine);
                $this->timer = $timer;
    }

}

Because I use a real DI, I can now change my config to include:, (*14)

$injector->alias(TestClass::class, TimerProxyXTestClass::class);

And the Proxied version of the class with the timer attached will be used everywhere that the original class was used., (*15)

TODO

  • Figure out what to do about factories, because having to make a new factory for every combination of thing sucks. e.g. CachedTimedStatementWrapperFactory to make a cached, timed, statementWrapper factory., (*16)

  • Write about difference between this and monkey patching http://en.wikipedia.org/wiki/Monkey_patch Short version Monkey patching runtime only, impossible to debug, not much type safety., (*17)

Terms

  • Source class - the original class that needs to have it's behaviour modified., (*18)

  • Decorator class - the class that will be used to decorate the source class., (*19)

  • Decorated class - the result of the weaving., (*20)

Notes

List of examples that I should implement from Ocramius/ProxyManager, (*21)

Lazy Loading Value Holders (Virtual Proxy) Access Interceptor Value Holder Null Objects Ghost Objects - for lazy loading Lazy References - wat Remote Object, (*22)

Protection Proxy class APIProtectionProxy extends API { protected $count = 0; public function __construct(API $api, $limit) { $this->api = $api; $this->limit = $limit; }, (*23)

public function doStuff() {
    $this->count();
    return $this->api->doStuff();
}

private function count() {
    if (++$this->count > $this->limit) {
        throw new RemoteApiLimit('STAHP!');
    }
}

}, (*24)

The Versions

06/11 2014

dev-master

9999999-dev https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop code generation composition weaving lazyloader

06/11 2014

0.4.5

0.4.5.0 https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop code generation composition weaving lazyloader

11/10 2014

0.4.3

0.4.3.0 https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop code generation composition weaving lazyloader

11/10 2014

0.4.4

0.4.4.0 https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop code generation composition weaving lazyloader

11/10 2014

0.4.2

0.4.2.0 https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop code generation composition weaving lazyloader

10/10 2014
05/10 2014
29/12 2013

0.1.1

0.1.1.0 https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop weaving wtf idonteven

29/12 2013

0.1.0

0.1.0.0 https://github.com/danack/Weaver

An experiment in compositional programming.

  Sources   Download

MIT

The Requires

 

The Development Requires

aop weaving wtf idonteven