PHP code example of tueena-lib / dependency-injection

1. Go to this page and download the library: Download tueena-lib/dependency-injection 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/ */

    

tueena-lib / dependency-injection example snippets


use tueenaLib\dependencyInjection\ServiceLocator;

// The ServiceLocator is immutable. So the register*() methods will return new instances of
// the ServiceLocator on each call.
$serviceLocator = (new ServiceLocator)
  // Define a concrete class.
  // The constructor of the class may on $configuration) { return new SomeApi($configuration->getApiKey()); })
;

// The ServiceLocator provides two more methods, but you probably will never use them.
// Use the injector instead.
if ($serviceLocator->has(MyMailer::class))
	$myMailer = $serviceLocator->get(MyMailer::class);
// The get() method throws an exception, if the service is not registered.

use tueenaLib\dependencyInjection\Injector;

$myObject = Injector::invokeConstructor($serviceLocator, MyClass::class);
$result = Injector::invokeMethod($serviceLocator, $anObject, 'aMethod');
$result = Injector::invokeStaticMethod($serviceLocator, MyClass::class, 'aStaticMethod');
$result = Injector::invokeInvokeMethod($serviceLocator, $anObject);
$result = Injector::invokeFunction($serviceLocator, 'namespace\\myFunction');
$result = Injector::invokeClosure($serviceLocator, function (MyMailer $mailer) { $mailer->sendSomeMessage(); });

return function () {

	$serviceLocator = (new ServiceLocator)
		->registerFactory(IConfiguration::class, function () { return new Configuration(__DIR__ . '/configuration/...'); })
		->registerClass(IMySqlConnection::class, MySqlConnection::class)

		->registerClass(IOrderStorage::class, OrderMySqlStorage::class)
		->registerClass(IOrderInteractor::class, OrderInteractor::class)
	;
	return $serviceLocator;
};

// applications/restApi/init.php

$coreInitializer = plication specific services.
$applicationServiceLocator = $coreServiceLocator
	->registerClass(IWebSecurityPolicy::class, WebSecurityPolicy::class)
;

// some kind of routing...
$router = new Router;
$request = new Request($_GET, ...);
$controllerClassName = $router->resolveRequest();

$controller = Injector::invokeConstructor($applicationServiceLocator, $controllerClassName);
$result = $controller->execute($request);

public function __construct(IWebSecurityPolicy $securityPolicy, IOrderInteractor $orderInteractor)
{
	$this->securityPolicy = $securityPolicy;
	$this->orderInteractor = $orderInteractor;
}

public function execute(HttpRequest $httpRequest)
{
	if ($this->securityPolicy->isIpAddressBlacklistedToOrder($request->getIpAddress()))
		...
	$processNewOrderRequest = self::createProcessNewOrderRequestFromHttpRequest($httpRequest);
	$this->orderInteractor->processNewOrder($processNewOrderRequest);
}

public function __construct(IOrderStorage $storage)
{
	$this->storage = $storage;
}

public function processNewOrder(ProcessNewOrderRequest $request)
{
	$order = self::createOrderFromRequest($request);
	$this->storage->saveNewOrder($order);
	// ...
}