1. Go to this page and download the library: Download constanze-standard/di 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/ */
constanze-standard / di example snippets
use ConstanzeStandard\Container\Container;
use ConstanzeStandard\DI\Annotation\Params;
use ConstanzeStandard\DI\Manager;
}
};
$container = new Container();
$container->add('foo', 'bar');
$manager = new Manager($container);
$manager->call($injectionTest);
// 输出: bar
function injection_test($foo) {
echo $foo;
}
$manager->call('injection_test', ['foo' => 'bar']);
// 输出: bar
function injection_test($foo) {
echo $foo;
}
$manager->call('injection_test', ['bar']);
// 输出: bar
class Serve {
public $foo = 'bar';
}
$container->add(Serve::class, new Serve);
function injection_test(Serve $serve) {
echo $serve->foo;
};
$manager->call('injection_test');
// 输出: bar
$injectionTest = new class() {
/**
* @Params(arg1 = "foo")
*/
public function __invoke($arg1)
{
echo $arg1;
}
};
$container = new Container();
$container->add('foo', 'bar');
$manager = new Manager($container);
$manager->call($injectionTest);
// 输出: bar
class Client
{
/**
* @Params(foo = "foo")
*/
public function __construct($foo)
{
$this->foo = $foo;
}
}
$container->add('foo', 'bar');
$client = $manager->instance(Client::class);
echo $client->foo;
// 输出:bar
use ConstanzeStandard\DI\Annotation\Property;
class Client
{
/** @Property("foo") */
public $foo;
}
$container->add('foo', 'bar');
$client = new Client();
$manager->resolvePropertyAnnotation($client);
echo $client->foo;
// 输出:bar
class serve { }
$container->add(serve::class, new Serve());
function resolver_test(Serve $serve, $foo) { }
$resolver = $manager->getCallableResolver('resolver_test');
$params = $resolver->resolveParameters(['foo' => 'bar']);
print_r($params);
/* 输出:
Array
(
[0] => serve Object
(
)
[1] => bar
)
*/
class Client
{
public function __construct(serve $serve, $foo)
{
}
}
$resolver = $manager->getConstructResolver(Client::class);
$params = $resolver->resolveParameters(['foo' => 'bar']);
print_r($params);
/* 输出:
Array
(
[0] => serve Object
(
)
[1] => bar
)
*/
use ConstanzeStandard\DI\Annotation\Property;
class Client
{
/** @Property("foo") */
public $property1;
}
$instance = new Client();
$reflectionClass = new \ReflectionClass($instance);
$reflectionProperty = $reflectionClass->getProperty('property1');
$resolver = $manager->getAnnotationResolver();
$property = $resolver->getProperty($reflectionProperty);
$name = $property->getName(); // foo
use ConstanzeStandard\Container\Container;
use ConstanzeStandard\DI\Annotation\Params;
class Client
{
/**
* @Params(
* foo = "foo"
* )
*/
public function test($foo, $abc = 1)
{
}
}
$container = new Container();
$container->add('foo', 'bar');
$instance = new Client();
$reflectionMethod = new \ReflectionMethod($instance, 'test');
$resolver = $manager->getAnnotationResolver();
$parameters = $resolver->resolveMethodParameters($reflectionMethod);
print_r($parameters);
/* 输出:
Array
(
[foo] => bar
)
*/
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\PhpFileCache;
$reader = new CachedReader(
new AnnotationReader(),
new PhpFileCache(__DIR__ . '/cache'),
$debug = true
);
$annotationResolver = new AnnotationResolver($container, $reader);
$manager = new Manager($container, null, $annotationResolver);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.