PHP code example of earc / di
1. Go to this page and download the library: Download earc/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' );
earc / di example snippets
use eArc \DI \DI ;
DI::init();
di_get(SomeClass::class);
di_param('some.parameter' );
class alphaCentauri {
public function construct ()
{
$this ->dependency1 = di_get(DependencyOne::class);
$this ->dependency2 = di_get(DependencyTwo::class);
$this ->parameterAlpha = di_param('alpha' );
$this ->parameterBeta = di_param('beta' );
$this ->parameterGamma = di_param('gamma' );
}
}
class Example
{
public function getResult ($param)
{
$math = di_get(Math::class);
return $math->calculate($param, di_param('pi' ));
}
}
function depending_on_injection ($param)
{
$math = di_get(Math::class);
return $math->calculate($param, di_param('pi' ));
}
$result = depending_on_injection(42 );
di_param('base_key' )['level1' ]['level2' ]['parameter_name' ];
di_param('base_key.level1.level2.parameter_name' );
di_import_param([
'data' => [
'server' => [
'mysql' => [
'user' => 'foo' ,
'db' => 'bar' ,
'password' => 'x23W!_bxTff' ,
],
],
],
]);
di_import_param(
Yaml::parse(
file_get_contents('/path/to/config.yml' )
)
);
interface ParameterInterface
{
const VENDOR_DIR = 'earc.vendor_directory' ;
const ROOT_DIRECTORIES = 'earc.event_tree.directories' ;
const BLACKLIST = 'earc.event_tree.blacklist' ;
}
di_register_factory(SomeInterface::class, [Factory::class, 'build' ]);
di_decorate(SomeInterface::class, ThisImplementsTheInterface::class);
di_decorate(ServiceContainingAnError::class, ServiceDecorator::class);
get_class(di_get(ServiceContainingAnError::class));
get_class(di_make(ServiceContainingAnError::class));
$staticService = di_static(AbstractService::class);
$staticService::staticMethod();
di_decorate(StaticService::class, AbstractServiceDecorator::class);
$staticService = di_static(StaticService::class);
$staticService::staticMethod();
use eArc \DI \DI ;
DI::init();
di_import_param(
Yaml::parse(
file_get_contents('/path/to/config.yml' )
)
);
DI::importParameter();
di_decorate(Service::class, ServiceDecorator::class);
di_decorate(ServiceDecorator::class, MegaDecorator::class);
di_get_decorator(Service::class);
get_class(di_get(Service::class));
di_decorate(Service::class, Service::class);
di_is_decorated(Service::class);
get_class(di_get(Service::class));
use eArc \DI \DI ;
DI::init();
di_import_param(
Yaml::parse(
file_get_contents('/path/to/config.yml' )
)
);
DI::importParameter();
$getObj = di_get(Service::class);
$makeObj = di_get(Service::class);
$mockedService = TestCase::createMock(Service::class);
di_mock(Service::class, $mockedService);
Assert::assertSame($mockedService, di_get(Service::class));
Assert::assertSame($mockedService, di_make(Service::class));
Assert::assertSame($getObj, di_get(Service::class));
Assert::assertSame($makeObj, di_make(Service::class));
di_static(StaticService::class)::staticMethod();
di_decorate(StaticService::class, StaticMock::class);
di_static(StaticService::class)::staticMethod();
Assert::assertSame(true , di_is_mocked(ServiceClass));
Assert::assertSame(true , di_is_mocked(AnotherService::class));
di_clear_mock(Service::class);
Assert::assertSame($mockedService, di_get(Service::class));
Assert::assertSame($mockedService, di_make(Service::class));
Assert::assertSame($getObj, di_get(Service::class));
Assert::assertSame($makeObj, di_make(Service::class));
di_clear_mock();
di_mock(Service::class, (object) ['iAmMock' => 1 ]);
di_mock(ServiceDecorator::class, (object) ['iAmMock' => 2 ]);
di_decorate(Service::class, ServiceDecorator::class);
Assert::assertSame(true , di_is_mocked(Service::class));
Assert::assertSame(true , di_is_mocked(ServiceDecorator::class));
Assert::assertSame(1 , di_get(Service::class)->iAmMock);
Assert::assertSame(2 , di_get(Service::class)->iAmMock);
Assert::assertSame(2 , di_get(ServiceDecorator::class)->iAmMock);
di_tag('tag.name' , Service002::class);
di_tag('tag.name' , Service007::class);
di_tag('tag.name' , Service014::class);
foreach (di_get_tagged('tag.name' ) as $handlerName => $argument) {
$handler = di_get($handlerName);
if ($handler->canHandleTask($task)) {
$result = $handler->handleTask($task);
return $result;
}
}
throw new TaskNotHandledException($task);
function something_cool ($times) {
return $times.' x icecream' ;
}
di_static('something_cool' )(42 );
function something_cool_but_its_winter ($times) {
return $times.' x hot tea' ;
}
di_decorate('something_cool' , 'something_cool_but_its_winter' );
di_static('something_cool' )(42 );
$ PHP Fatal error: Uncaught Error: Maximum function nesting level of '256' reached , aborting !