filecage / creator Goto Github PK
View Code? Open in Web Editor NEWcreator is a simple PHP dependency injection / service container that works with typehints and powers www.thomann.de
License: MIT License
creator is a simple PHP dependency injection / service container that works with typehints and powers www.thomann.de
License: MIT License
Currently all unistantiable dependencies get the same Factory name pattern. This should be a configurable callable value per ResourceRegistry
.
Default could be no provider given, resulting in the functionality being disabled.
As of now, Creator iterates over each registered class resource to find out whether it might be a fulfilling instance (implementing an uninstantiable).
Storing the implementing interfaces and abstracts during registration might cause faster lookups. (But also slower registration times - probably requires some measuring).
Whenever an Creator\Exceptions\Unresolvable
occurs because of an unresolvable dependency, it does not tell enough details on why it failed; it's missing the Information of the actual unresolvable dependency.
Creator\Exceptions\UnresolvableInnerDependency
extends Creator\Exceptions\Unresolvable
Dependency Foo\Bar\Baz can not be resolved: Inner dependency
Foo\Bar\Baz\Dependency
demandsFoo\Bar\Baz\Dependency\Inexistent
as$parameterName
, but the resource is unknown
The Creation
class forces an instance if the requested resource depends on an injected instance, but only checks the first level - i.e. the direct dependants of the requested resource.
To ensure deterministic results, Creator should check the complete dependency tree (i.e. any dependency of the dependencies) and re-create any resource that depends on an injected instance.
An example to clarify the issue:
class Foo {
function __construct(Bar $bar) {
$this->bar = $bar;
}
}
class Bar {
function __construct(Baz $baz) {
$this->baz = $baz;
}
}
class Baz {
function __construct($bax = 'Hello World') {
$this->bax = $bax;
}
}
$instanceWithDefaultValue = $creator->create(Foo::class);
// This should output `Hello World`
echo $instanceWithDefaultValue->bar->baz->bax;
$anotherBaz = new Baz('Goodbye cruel world');
$instanceWithCustomValue = $creator->createInjected(Foo::class)->with($anotherBaz)->create();
// This should output `Goodbye cruel world` but actually outputs `Hello World`
echo $instanceWithCustomValue->bar->baz->bax;
Internally, ->create()
checks whether a dependency of the class (Foo
) is contained in the injected resource registry. That check is misleading because it does not consider that Foo
might have a dependency of the injected class Baz
.
It is unsupported to pass a callable to invoke()
that is missing the object context:
creator/lib/InvokableClosure.php
Lines 21 to 25 in 6da60f9
Creator should resolve the object (if it's non-static) and then call the invokable instead of throwing an exception.
Example test case that should pass:
class Foo {
function __construct($bar) {
$this->bar = $bar;
}
}
class BarTransformer {
function transform ($bar) {
return strtoupper($bar);
}
}
class AnyClass {
static function bax (Foo $foo, BarTransformer $barTransformer) {
return $barTransformer->transform($foo->bar);
}
function __construct(Foo $foo) {
$this->foo = $foo;
}
function bar (BarTransformer $barTransformer) {
return $barTransformer->transform($this->foo->bar);
}
}
$creator = new Creator();
$creator->registerClassResource(new Foo('hello world'));
echo $creator->invoke(['AnyClass', 'bax']);
echo $creator->invoke(['AnyClass', 'bar']);
Should output
HELLO WORLD
twice, the first invocation should not register a class resource as the method is only called statically.
Implement PSR-11 interface to be more compatible with other projects
Specification Reference: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md
Meta Document: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container-meta.md
Implementation Package: https://packagist.org/packages/psr/container
Following #3, we should allow registration of lazy factories, i.e. instances of Creator\Interfaces\Factory
which are only being created once the factory is being called.
Improve README.md to ensure easier understanding of usage.
0.8.0
<?php
interface FoobarInterface {
}
class Foobar implements FoobarInterface {
function __construct() {
echo 'Hello ';
}
}
$creator = new Creator\Creator();
$creator->registerFactory(function() { return new Foobar(); }, FoobarInterface::class);
$foobar1 = $creator->create(FoobarInterface::class);
$foobar2 = $creator->create(FoobarInterface::class);
if ($foobar1 === $foobar2) {
echo ' World!';
} else {
echo ' Fail!';
}
Hello World!
Hello Hello Fail!
Proposed interface:
$creator->registerFactory(string $classResourceKey, callable $factory | instanceof Interfaces\Factory) : object
In call stack: after ResourceRegistry
, before any creation routine
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.