2017 © Pedro Peláez
 

library pops

PHP object proxy system.

image

eloquent/pops

PHP object proxy system.

  • Sunday, February 9, 2014
  • by ezzatron
  • Repository
  • 2 Watchers
  • 13 Stars
  • 470,889 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 2 % Grown

The README.md

No longer maintained

This package is no longer maintained. See [this statement] for more info., (*1)

[this statement]: https://gist.github.com/ezzatron/713a548735febe3d76f8ca831bc895c0, (*2)

Pops

PHP Object Proxy System., (*3)

Installation and documentation

What is Pops?

Pops is a system for wrapping PHP objects in other objects to modify their behaviour. A Pops proxy will, as much as possible, imitate the object it wraps. It passes along method calls and returns the underlying result, and allows transparent access to properties (for both setting and getting)., (*4)

Pops is the underlying system behind Liberator., (*5)

Creating proxies

Let's write a simple proxy that converts everything to uppercase. Here we have a class:, (*6)

class Confusion
{
    public function wat()
    {
        return "What is this? I don't even...";
    }

    public $derp = 'Has anyone really been far even as decided to use even?';
}

And here is our proxy:, (*7)

use Eloquent\Pops\ProxyObject;

class UppercaseProxyObject extends ProxyObject
{
    public function popsCall($method, array &$arguments)
    {
        return strtoupper(parent::popsCall($method, $arguments));
    }

    public function __get($property)
    {
        return strtoupper(parent::__get($property));
    }
}

We use popsCall() here rather than __call() to get around PHP limitations to do with passing arguments by reference. See calling methods with by-reference parameters for a deeper explanation., (*8)

Now when we access wat() and $derp both normally, and through our proxy, we can see the effect:, (*9)

$confusion = new Confusion;
$proxy = new UppercaseProxyObject($confusion);

echo $confusion->wat(); // outputs "What is this? I don't even..."
echo $proxy->wat();     // outputs "WHAT IS THIS? I DON'T EVEN..."

echo $confusion->derp;  // outputs 'Has anyone really been far even as decided to use even?'
echo $proxy->derp;      // outputs 'HAS ANYONE REALLY BEEN FAR EVEN AS DECIDED TO USE EVEN?'

Recursive proxies

Pops proxies can be applied to any value recursively. This comes in handy when designing, for example, an output escaper (similar to Symfony). Here's an example of how such a system could be created for escaping HTML output:, (*10)

namespace OutputEscaper;

use Eloquent\Pops\Proxy;

/**
 * Escapes output for use in HTML.
 */
class OutputEscaperProxy extends Proxy
{
    /**
     * Get the array proxy class.
     *
     * @return string The array proxy class.
     */
    protected static function proxyArrayClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxyArray';
    }

    /**
     * Get the class proxy class.
     *
     * @return string The class proxy class.
     */
    protected static function proxyClassClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxyClass';
    }

    /**
     * Get the object proxy class.
     *
     * @return string The object proxy class.
     */
    protected static function proxyObjectClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxyObject';
    }

    /**
     * Get the proxy class for primitive values.
     *
     * @return string The proxy class for primitive values.
     */
    protected static function proxyPrimitiveClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxyPrimitive';
    }
}
namespace OutputEscaper;

use Eloquent\Pops\ProxyArray;

/**
 * Wraps an array to escape any sub-values for use in HTML.
 */
class OutputEscaperProxyArray extends ProxyArray
{
    /**
     * Get the proxy class.
     *
     * @return string The proxy class.
     */
    protected static function popsProxyClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxy';
    }
}
namespace OutputEscaper;

use Eloquent\Pops\ProxyClass;

/**
 * Wraps a class to escape any sub-values for use in HTML.
 */
class OutputEscaperProxyClass extends ProxyClass
{
    /**
     * Get the proxy class.
     *
     * @return string The proxy class.
     */
    protected static function popsProxyClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxy';
    }
}
namespace OutputEscaper;

use Eloquent\Pops\ProxyObject;

/**
 * Wraps an object to escape any sub-values for use in HTML.
 */
class OutputEscaperProxyObject extends ProxyObject
{
    /**
     * Get the proxy class.
     *
     * @return string The proxy class.
     */
    protected static function popsProxyClass()
    {
        return __NAMESPACE__ . '\OutputEscaperProxy';
    }
}
namespace OutputEscaper;

use Eloquent\Pops\ProxyPrimitive;

/**
 * Wraps a primitive to escape its value for use in HTML.
 */
class OutputEscaperProxyPrimitive extends ProxyPrimitive
{
    /**
     * Get the HTML-escaped version of this primitive.
     *
     * @return string The HTML-secaped version of this primitive.
     */
    public function __toString()
    {
        return htmlspecialchars(
            strval($this->popsValue()),
            ENT_QUOTES,
            'UTF-8'
        );
    }
}

The output escaper can now be used like so:, (*11)

use OutputEscaper\OutputEscaperProxy;
use Eloquent\Pops\Safe\SafeProxy;

$list = new ArrayIterator(
    array(
        'foo',
        'bar',
        '',
        SafeProxy::proxy('<em>ooh...</em>'),
    )
);
$proxy = OutputEscaperProxy::proxy($list, true);

echo "

    \n"; foreach ($proxy as $item) { printf("
  • %s
  • \n", $item); } echo "
\n";

Which would output:, (*12)

<ul>
<li>foo</li>
<li>bar</li>
<li>&lt;script&gt;alert(document.cookie);&lt;/script&gt;</li>
<li><em>ooh...</em></li>
</ul>

Note that the above example should NOT be used in production. Output escaping is a complex issue that should not be taken lightly., (*13)

Excluding values from recursion

Note that in the above example, the last list item was wrapped in a Safe proxy. When Pops applies its proxies, it will skip anything marked as safe in this manner., (*14)

Calling methods with by-reference parameters

Because of PHP limitations, methods with arguments that are passed by reference must be called in a special way., (*15)

To explain futher, let's assume our class from before also has a method which accepts a reference:, (*16)

class Confusion
{
    public function butWho(&$wasPhone)
    {
        $wasPhone = 'Hello? Yes this is dog.';
    }
}

This method cannot be proxied normally because the $wasPhone argument is passed by reference. The correct way to call the above butWho() method through a Pops proxy looks like this:, (*17)

use Eloquent\Pops\Proxy;

$proxy = Proxy::proxy(new Confusion);

$wasPhone = null;
$arguments = array(&$wasPhone);

$proxy->popsCall('butWho', $arguments);

echo $wasPhone; // outputs 'Hello? Yes this is dog.'

Note that there must be a variable for the $wasPhone argument, and there must be a variable for the arguments themselves. Neither can be passed directly as a value. The arguments must also contain a reference to $wasPhone argument., (*18)

The Versions

09/02 2014

dev-develop

dev-develop https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

proxy object property method

09/02 2014

dev-master

9999999-dev https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

proxy object property method

09/02 2014

4.1.0

4.1.0.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

proxy object property method

30/01 2014

4.0.1

4.0.1.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

proxy object property method

03/09 2013

4.0.0

4.0.0.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

proxy object property method

04/03 2013

3.1.1

3.1.1.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

proxy object escaping

02/08 2012

3.1.0

3.1.0.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

proxy object escaping

02/08 2012

3.0.1

3.0.1.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

proxy object escaping

02/08 2012

3.0.0

3.0.0.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

proxy object escaping

21/07 2012

2.0.1

2.0.1.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

access proxy object escaping

09/07 2012

2.0.0

2.0.0.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

access proxy object escaping

04/03 2012

1.0.0

1.0.0.0 https://github.com/eloquent/pops

PHP object proxy system.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

access proxy object escaping