1. Go to this page and download the library: Download cable/cable-container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cable / cable-container example snippets
$container = \Cable\Container\Factory::create();
// you can give an anonymous function
$container->add('test', function(){
return new Test();
});
// you can give the name of class
$container->add('test', Test::class);
// you can give the already exists class instance
$container->add('test', new Test());
$test = $container->resolve('test');
// you can give arguments,
// they will be used in constructor calling,
// of course if you didnt give an instance
$test->resolve('test', array('message' => 'hello world'));
//you may want to check resolved value is an instance of something
// well you can to that like that
$resolved= $container->resolve('test');
if(!$resolved instanceof MyTestInterface){
}
// the problem that you may want to check multipile interfaces
// well, you can do that like that
try{
// you can always give an array like, expect('test', [MyInterface::class, MySecondInterface::class]);
$container->expect('test', MyTestInterface::class);
}catch(ExpectationException $e){
echo "give me something valid";
}
// if test doesnot return an instanceof MyTestInterface
// container will throw an expectation exception
class Provider extends ServiceProvider{
public function register(){}
public function boot(){
$this->getContainer()->add('test', Test::class);
}
}
$container = \Cable\Container\Factory::create();
$container->addProvider(Provider::class);
// now you can resolve the 'test' service
$test = $container->resolve('test');
/**
*
* @Provider("Your\Provide\Class")
* // you can give multiple providers like @Provider({"FirstProviderClass", "SecondProviderClass"})
*/
class TestClass{
}
use Cable\Container\Annotations\Inject;
$container->add("my_alias_to_resolve", function(){
return "hello world";
});
class TestClass{
/**
*
* @Inject({"$test": "my_alias_to_resolve"})
*
*/
public function __construct($test){
}
}