2017 © Pedro Peláez
 

library auto-pimple

Pimple with automated service loading

image

suramon/auto-pimple

Pimple with automated service loading

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 29 Versions
  • 3 % Grown

The README.md

AutoPimple

The Pimple DI container extended with auto-injection functionality, (*1)

PHP Composer Build Status, (*2)

Quick Introduction

If you do not know what Pimple is, or you are not using Pimple v1, this is probably nothing for you. AutoPimple is a drop-in replacement for Pimple (AutoPimple extends Pimple) that can auto-inject your services reducing configuration. This introduction assumes you already know the Pimple v1 API., (*3)

A typical old-style Pimple setup would look like this:, (*4)

namespace MyLittleProject;

class A {}
class B {}
class C { function __construct(A $a, B $b) {} }

$c = new \Pimple();
$c['my_little_project.a'] = $c->share(function($c) { return new A; });
$c['my_little_project.b'] = $c->share(function($c) { return new B; });
$c['my_little_project.c'] = $c->share(function($c) {
    return new C($c['my_little_project.a'], $c['my_little_project.b']);
});
echo get_class($c['my_little_project.c']); // outputs MyLittleProject\C

If you use AutoPimple all the configuration above can be dropped:, (*5)

$c = new \AutoPimple\AutoPimple();
echo get_class($c['my_little_project.c']); // outputs MyLittleProject\C

AutoPimple will automatically convert snake case service names (abc_def.hij) to camel case names (AbcDef\Hij) and will try to inject services based on the type hinting of the constructor parameters., (*6)

Dropping namespace prefixes

Every class can be retrieved automatically (if auto-injections doesn't fail) from the AutoPimple container by converting is full class name to snake case. This can cause pretty long and inconvenient service names. You can drop (or replace) prefixes of the namespace like this:, (*7)

namespace MyCompany\MyProject\RatherLong\Namespace;
class A {}

$c = new \AutoPimple\AutoPimple(array(
    'my_company.my_project.' => '',
    'my_company.my_project.rather_long.namespace.' => '',
    'my_company.my_project.rather_long.' => 'blieblabloe.',
));
// Full path will always work
$c['my_company.my_project.rather_long.namespace.a'];
// We mapped 'my_company.my_project.' to ''
$c['rather_long.namespace.a'];
// We mapped 'my_company.my_project.rather_long.namespace.' to ''
$c['a'];
// We mapped 'my_company.my_project.rather_long.' to 'blieblabloe.'
$c['blieblabloe.a'];
// Even though the service name is different, they all point to the same service instance
var_dump($c['a'] === $c['blieblabloe.a']); // true

Aliases

AutoPimple provides an easy aliasing function. The aliased services are guaranteed to always point to the same data., (*8)

$c['default_timeout'] = 5;
$c->alias('http_timeout', 'default_timeout');

echo $c['http_timeout']; // outputs 5

Handling services with non-auto-injectable constructor parameters

Sometimes the construct parameters of your services cannot be type hinted or are type hinted to interfaces. The former can be resolved once, but the latter should be resolved for each individual service. For example assume we have a class setup like this:, (*9)

namespace MilkFactory;

interface Animal { }
class Cow implements Animal {}
class Sheep implements Animal {}

class Milker {
    function __construct(Animal $aminal, $milkingFrequency) { }
}

$c = new \AutoPimple\AutoPimple(array('milk_factory.' => ''));

As both $animal and $milkingFrequency cannot be injected automatically we can resolve this on multiple ways:, (*10)

// 1) old-style resolution (AutoPimple)
$c['milker'] = $c->share(function($c) {
    return new \MilkFactory\Milker($['cow'], 5);
});

// 2) AutoPimple style using shortened service name
$c->alias('milker.animal', 'cow');
$c['milker.frequency'] = 5;

// 3) AutoPimple style using full service name
$c->alias('milk_factory.milker.animal', 'cow');
$c['milk_factory.milker.frequency'] = 5;

// 4) By usings cows as default
$c->alias('animal', 'cow');
$c['milker.frequency'] = 5;

The last method is perhaps the hardest to understand. By aliasing 'animal' (short for milk_factory.animal which is the snake case for the MilkFactory\Animal interface name) to cow, we tell AutoPimple all Animal interfaces can be auto-injected with a Cow. So we define a default class for the Animal interface. We can always override the default with method (2)., (*11)

Testing auto-injected services

When you want to test auto-injected services but you want to mock certain dependencies of the services, AutoPimple has a really easy way of helping you with this. Assume we have the following setup:, (*12)

class A {
    function __construct(B $b, C $c, D $d, E $e) {}
}

If you want the get the service A but you want to mock the parameter $d with a class MockD, then you can to this with the getModified method:, (*13)

$c->getModified('a', array('d' => new MockD()));

This will create a new instance of A (getModified will ALWAYS create a new instance) and replace the dependency $d with MockD. The second argument of getModified should be an associative array with the snake cased parameter names (NOT paramater class names) as keys and the mocked dependecy instances as values., (*14)

The Versions

09/07 2018

2.2.0

2.2.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/04 2017

dev-master

9999999-dev

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/04 2017

2.1.4

2.1.4.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/01 2016

2.1.3

2.1.3.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

02/01 2016

2.1.2

2.1.2.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

04/12 2014

dev-cache

dev-cache

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

04/12 2014

2.1.1

2.1.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

17/11 2014

2.0.1

2.0.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/09 2014

2.0.0

2.0.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

01/09 2014

1.9.1

1.9.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

01/09 2014

1.9.0

1.9.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

05/08 2014

1.8.0

1.8.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

23/06 2014

1.7.1

1.7.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

23/06 2014

1.7.0

1.7.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

13/05 2014

1.6.2

1.6.2.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

12/05 2014

1.6.1

1.6.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

25/04 2014

1.6.0

1.6.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

04/04 2014

1.5.5

1.5.5.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

22/03 2014

1.5.4

1.5.4.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

19/03 2014

1.5.2

1.5.2.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

28/02 2014

1.5.1

1.5.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

17/02 2014

1.5.0

1.5.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/02 2014

1.4.1

1.4.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/02 2014

1.4.0

1.4.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

11/02 2014

1.3.0

1.3.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

21/01 2014

1.2.0

1.2.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

20/01 2014

1.1.1

1.1.1.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

19/01 2014

1.1.0

1.1.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires

17/01 2014

1.0.0

1.0.0.0

Pimple with automated service loading

  Sources   Download

DO WHAT THE FUCK YOU WANT LICENSE

The Requires

 

The Development Requires