Releases: guide42/suda
Version 0.17
On __invoke
, make class when string is not found in the registry.
$di = new Registry;
$di(SomeClass::class);
// will make an instance of SomeClass and call __invoke method
before:
$di = new Registry;
$di[SomeClass::class] = function(callable $make) {
return $make();
};
$di(SomeClass::class);
BC-BREAK
Now Registry::make
throws UnexpectedValueException
when is not instantiable.
BC-BREAK
Now InvalidArgumentException
is thrown when trying to reflect an interface.
BC-BREAK
Remove shortcuts to create build a concrete and with arguments.
before:
$di = new Registry;
$di[AbstractClass::class] = ConcreteClass::class;
now:
$di = new Registry;
$di[AbstractClass::class] = function(callable $make) {
return $make(ConcreteClass::class);
};
before:
$di = new Registry;
$di[ConcreteClass::class] = ['arg0'];
now:
$di = new Registry;
$di[ConcreteClass::class] = function(callable $make) {
return $make(ConcreteClass::class, ['arg0']);
};
BC-BREAK
Remove argument resolve.
before:
$di = new Registry;
$di['param_from_registry'] = 'some value';
$di[DependencyClass::class] = function() {
return new DependencyClass
};
$di[SomeClass::class] = function(callable $make) {
return $make(SomeClass::class, [
'$param_from_registry',
DependencyClass::class,
]);
};
now:
$di = new Registry;
$di[DependencyClass::class] = function() {
return new DependencyClass
};
$di[SomeClass::class] = function(callable $make, DependencyClass $dep) {
return $make(SomeClass::class, [
'some value',
$dep,
]);
};
Version 0.16.0
Old make
function is public again, now with cache implemented.
BC-BREAK
Now Registry::__invoke
will resolve context from delegate. Resolves parameters for factories again. The first parameter must be callable $make
and then the dependencies.
BC-BREAK
Now $make
and Registry::make
throws CyclicDependency
. New exceptions replaces RuntimeException
: set and unset throws Frozen
and get throws NotFound
.
Version 0.15.0
Constructor now accepts a 3rd parameter for the reflection cache callable.
BC-BREAK
Removed Registry::setDelegate
and Registry::setReflector
.
Before:
$di = new Registry($values);
$di->setDelegate($delegate);
$di->setReflector($reflector);
Now:
$di = new Registry($values, $delegate, $reflector);
BC-BREAK
$di = new Registry([Engine::class => V8::class]);
$v8 = $di[Engine::class];
// Before this line was possible,
// now it throws RuntimeException
$di[Engine::class] = function() {
return new OtherEngine;
};
NEW
New Registry::freeze
to disallow new values or factories to be assigned. Returns the quantity of frozen entries.
$di = new Registry(['foo' => 'bar', 'bar' => 'baz']);
assert($di->freeze('foo') === 1);
assert($di->freeze('bar') === 2);
BC-BREAK
Before:
class A {
public $name;
function __construct($name = '$name') { $this->name = $name; }
}
$di = new Registry([
'name' => 'John',
A::class => A::class,
]);
assert($di[A::class]->name === 'John');
Now:
class A {
public $name;
function __construct($name) { $this->name = $name; }
}
$di = new Registry([
'name' => 'John',
A::class => ['name' => '$name'],
]);
assert($di[A::class]->name === 'John');
BC-BREAK
Before:
$delegate = new Registry;
$delegate[A::class] = A::class;
$di = new Registry;
$di[B::class] = function($c, callable $make) {
$a0 = $make(A::class, ['this', 'args', 'are', 'ignored']);
$a1 = $make(A::class, ['also', 'this']);
// This behaviour is no more,
// now there are a new instance of A each time
assert($a0 === $a1);
return new B($a0);
};
Now:
$delegate = new Registry;
$delegate[A::class] = A::class;
$di = new Registry;
$di[B::class] = function($c, callable $make) {
$a0 = $c[A::class];
$a1 = $c[A::class];
// Now they are both retrieved from the delegate
// registry that is passed to factories
assert($a0 === $a1);
return new B($a0);
};
Version 0.14.0
Support for PSR-11 has been dropped. This library is not part of a framework. Now it requires PHP 7.2.
BC-BREAK
Doesn't resolve parameters for factories anymore.
Before:
$di->offsetSet(Engine::class, function($c, $make, V8 $v8) {
return $v8;
});
Now:
$di->offsetSet(Engine::class, function($c, $make) {
return $make(V8::class);
});
BC-BREAK
Replace Registry::withDelegate
with Registry::setDelegate
.
Before:
$di = $di->withDelegate($delegate);
Now:
$di->setDelegate($delegate);
New
New Registry::setReflector
to assign a function to cache the creation of Reflection
objects.
Example:
$di->setReflector(function($class, string $method = null) use($reflectorRepo) {
if ($method === null) {
return $reflectorRepo->reflClass($class);
} else {
return $reflectorRepo->reflMethod($class, $method);
}
});
v0.13.0
BC-BREAKS
-
Factory call to make without parameters use make for key class instead of lookup in delegate.
$di = new Registry; $di[stdClass::class] = function() { return $make(); }; assert($di[stdClass::class] instanceof stdClass);
-
Call a function will resolve parameters from it's own instead of delegate.
$delegate = new Registry; $delegate[stdClass::class] = [['foo' => 'bar']]; $di = new Registry([], $delegate); $di[stdClass::class] = [['bar' => 'baz']]; $di(function(stdClass $data) { assert($data->bar === 'baz'); });
-
Resolve parameters default values.
class Hasher { public $algo; function __construct($algo = '$algo') { $this->algo = $algo; } } $di = new Registry; $di['algo'] = 'md5'; $di[Hasher::class] = []; assert($di[Hasher::class]->algo === 'md5');
v0.12.0
BC-BREAK Factories are given an instance of delegate instead it's own.
Example:
class Hasher {
public $algo;
function __construct($algo = 'md5') {
$this->algo = $algo;
}
}
$config = new Registry([
'algo' => 'sha1',
]);
$di = new Registry([], $config);
$di[Hasher::class] = ['$algo'];
assert($di[Hasher::class]->algo === 'sha1');
v0.11.0
-
[BC-BREAK] Mark
Registry::make
as private. -
[BC-BREAK] Doesn't call automagically params that are callables.
$di = new Registry; $di['rand'] = function() { return 14; }; // before $di['rand']; // after $di($di['rand']);
-
Factories parameters are resolved and injected.
$di = new Registry; $di[Engine::class] = V8::class; $di[Car::class] = function($c, $make, Engine $engine) { return new Car($engine); };
-
Call functions and inject parameters with
Registry::__invoke
.$di = new Registry; $di[Engine::class] = V8::class; $di(function(Engine $engine) { assert($engine instanceof V8); });
v0.10.0
- Delegate in PSR-11 is done with
\suda\psr11\Container::withDelegate
. - BC-BREAK PSR-11 support is done by wrapping
\suda\Registry
instead of extend it.
Example:
$delegate = new Container(new Registry([
Engine::class => V8::class,
]));
$container = new Container(new Registry([
Car::class => Car::class,
]));
$container = $container->withDelegate($delegate);
$container->get(Car::class); // new Car(new V8)
v0.9.0
PSR-11 support. Example:
$container = new \suda\psr11\Container;
$container[Engine::class] = V8::class;
$container->has(Engine::class); // true
$container->has(Car::class); // false
$container->get(Engine::class); // new V8
$container->make(Car::class); // new Car(new V8)
BC-BREAKS:
- Removed resolve by parameter name.
- Arguments are not revolved as parameter unless dollar sign is prefix.
Replace:
$di = new \suda\Registry(['dsn' => 'sqlite://:memory:']);
$di[PDO::class] = [/* param is resolver from name */];
$pdo = $di[PDO::class];
With:
$di = new \suda\Registry(['dsn' => 'sqlite://:memory:']);
$di[PDO::class] = ['$dsn'];
$pdo = $di[PDO::class];