troelskn / bucket Goto Github PK
View Code? Open in Web Editor NEWBasic di-container for php.
License: BSD 2-Clause "Simplified" License
Basic di-container for php.
License: BSD 2-Clause "Simplified" License
When setting an instance with bucket_Container::set(), the classname is lowercased and then stored in bucket_Scope::$instances. But since bucket_Container::get() doesn't lowercase the classname, the instance you set isn't found, and a new instance is created instead.
Am I correct in understanding that any class that has an array or string parameter would require its own factory? (i.e. there's no way to say: when instantiating PageController use $config for the parameter 'config'?)
codio@float-dance:~/workspace/bucket$ php bucket.test.php
bucket.test.php
1) Exception [ReflectionException: Class ClassThatDoesntExist does not exist] sh
ould be type [TriedToAutoloadException] at [/home/codio/workspace/bucket/bucket.
test.php line 105]
in test_undefined_class_triggers_autoload
in TestOfBucketAutoload
Exception 1!
Unexpected exception of type [ReflectionException] with message [Class ClassThat
DoesntExist does not exist] in [/home/codio/workspace/bucket/lib/bucket.inc.php
line 146]
in test_undefined_class_triggers_autoload
in TestOfBucketAutoload
Exception 2!
Unexpected exception of type [ReflectionException] with message [Class ClassThat
DoesntExist does not exist] in [/home/codio/workspace/bucket/lib/bucket.inc.php
line 146]
in test_autoload_gets_canonical_classname
in TestOfBucketAutoload
FAILURES!!!
Test cases run: 5/5, Passes: 27, Failures: 1, Exceptions: 2
Hi Troelskn,
Firstly, thanks a lot for your great work with Bucket. I downloaded the last version as of today and tried your example code as below:
$bucket = new bucket_Container();
$bucket->registerImplementation('Zap', 'Foo');
$cuux = $bucket->get('Cuux');
var_dump($bucket);
Result from var_dump:
protected 'instances' =>
array
'foo' =>
object(Foo)[12]
...
'cuux' =>
object(Cuux)[11]
...
Shouldn't I need to call $bucket->create('Cuux'); before or does it autocreate the object properly if it doesn't exist when running $bucket->get('Cuux')?
Thanks!
I'm creating an application that will use self-contained modules, and each module would have it's own factory class. The factory I will pass to Bucket will basically be a proxy to the module factories through __call. But since Bucket uses method_exists and not is_callable, that can't be done. How about changing method_exists to is_callable?
@troelskn Could you protect the master branch so it can only be updated using pull requests?
I have extended bucket to be "self aware" and in my case act as a singelton and be able to insert itself as a parameter when creating instances - maybe something you would consider adding to the source?
Just wondering if there is some plan on making it compatible with PHP 5.3 namespaces?
There are some issues with relative vs absolute scope (which can be overcome) but the main issue is with the factory calls (cant make a new_\My\Name\Space\Class function). Without a proper rewrite there are two quick ways to resolve this:
function create($classname) {
$classname = $this->scope->getImplementation($classname);
$funcname=strtr($classname,'\\','_');
if (isset($this->factory->{'new_' . strtolower($funcname)})) {
return call_user_func($this->factory->{'new_'.$funcname}, $this);
}
if (method_exists($this->factory, 'new_' . $funcname)) {
return $this->factory->{'new_'.$funcname}($this);
}
return $this->createThroughReflection($classname);
}
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.