2017 © Pedro Peláez
 

library statical

Static proxy implementation

image

statical/statical

Static proxy implementation

  • Friday, November 14, 2014
  • by johnstevenson
  • Repository
  • 4 Watchers
  • 8 Stars
  • 741 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 3 Versions
  • 5 % Grown

The README.md

Statical

Scrutinizer Code Quality Code Coverage Build Status, (*1)

PHP static proxy library., (*2)

Contents

, (*3)

About

Statical is a tiny PHP library that enables you to call class methods from a static accessor, so the static call to Foo::doSomething() actually invokes the doSomething() method of a specific class instance. To show a more concrete example:, (*4)

# Normal access
$app->get('view')->render('mytemplate', $data);

# Using a static proxy
View::render('mytemplate', $data);

Both examples call the render method of the instantiated view class, with the static proxy version using terse and cleaner code. This may or may not be a good thing and depends entirely on your requirements, usage and point of view., (*5)

How it Works

Everything runs through the Statical\Manager. It needs three pieces of data to create each static proxy:, (*6)

  • an alias (which calls)
  • a proxy class (which invokes the method in)
  • the target class

An alias is the short name you use for method calling: Foo, View or whatever., (*7)

You create a static proxy class like this. Note that its name is irrelevant and it is normally empty:, (*8)

class FooProxy extends \Statical\BaseProxy {}

A target class is the class whose methods you wish to call. It can be either:, (*9)

  • an actual class instance
  • a closure invoking a class instance
  • a reference to something in a container or service-locator that resolves to a class instance.

This data is then registered using either the addProxyInstance() or the addProxyService() methods. See the Usage section for some examples., (*10)

Namespaces

By default, each static proxy is registered in the global namespace. This means that any calls to Foo will not work in a namespace unless they are prefixed with a backslash \Foo. Alternatively you can include a use statement in each file: use \Foo as Foo;., (*11)

Statical includes a powerful namespacing feature which allows you to add namespace patterns for an alias. For example addNamespaceGroup('path', Foo', 'App\\Library') allows you to call Foo in any App\Library or descendant namespace., (*12)

Features

A few features in no particular order. Please see the documentation for more information., (*13)

  • Statical creates a static proxy to itself, aliased as Statical and available in any namespace, allowing you to call the Manager with Statical::addProxyService() or whatever. This feature can be disabled or modified as required., (*14)

  • You can use any type of container/service-locator. If it implements ArrayAccess or has a get method then Statical will discover this automatically, otherwise you need to pass a callable as the target container., (*15)

  • You can use multiple containers when adding proxy services to the Manager., (*16)

  • If you pass a closure as a proxy instance, it will be invoked once to resolve the target instance. You can get a reference to this instance, or in fact any target class, by calling the getInstance() method on your alias, for example Foo::getInstance()., (*17)

  • Statical is test-friendly. If you register a container then it is used to resolve the target instance for every proxied call, allowing you to swap in different objects. You can also replace a proxy by registering a different instance/container with the same alias., (*18)

, (*19)

Usage

Install via composer, (*20)

composer require statical/statical

Below are some examples. Firstly, using a class instance:, (*21)

<?php
$alias = 'Foo';
$proxy = 'Name\\Space\\FooInstance';
$instance = new FooClass();

# Create our Manager
$manager = new Statical\Manager();

# Add proxy instance
$manager->addProxyInstance($alias, $proxy, $instance);

# Now we can call FooClass methods via the static alias Foo
Foo::doSomething();

For a container or service-locator you would do the following:, (*22)

<?php
$alias = 'Foo';
$proxy = 'Name\\Space\\FooService';

# FooService id in container
$id = 'bar';

# Add it to the container
$container->set($id, function ($c) {
  return new FooService($c);
});

# Create our Manager
$manager = new Statical\Manager();

# Add proxy service
$manager->addProxyService($alias, $proxy, $container, $id);

# FooService is resolved from the container each time Foo is called
Foo::doSomething();

If the container id is a lower-cased version of the alias, then you can omit the $id param. Using the above example:, (*23)

<?php
$alias = 'Foo';
...
# FooService id in container
$id = 'foo';
...

# Add proxy service - note we don't need to pass the id
$manager->addProxyService($alias, $proxy, $container);
...

In the above examples, the service is resolved out of the container automatically. If the container doesn't implement ArrayAccess or doesn't have a get method, you need to pass a callable:, (*24)

<?php
$alias = 'Foo';
$proxy = 'Name\\Space\\FooService';

# Our container uses magic calls
$di->foo = new FooService();

# so we need to pass a callable as the container param
$container = array($di, '__get');
...
# Add proxy service
$manager->addProxyService($alias, $proxy, $container);
...

Full usage documentation can be found in the Wiki., (*25)

, (*26)

License

Statical is licensed under the MIT License - see the LICENSE file for details, (*27)

The Versions

14/11 2014

dev-master

9999999-dev http://github.com/johnstevenson/statical

Static proxy implementation

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by John Stevenson

static facade static proxy static interface statical

14/11 2014

v1.1.0

1.1.0.0 http://github.com/johnstevenson/statical

Static proxy implementation

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by John Stevenson

static facade static proxy static interface statical

12/11 2014

v1.0.0

1.0.0.0 http://github.com/johnstevenson/statical

Static proxy implementation

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by John Stevenson

static facade static proxy static interface statical