PHP code example of rkr / php-ioc-contract

1. Go to this page and download the library: Download rkr/php-ioc-contract 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/ */

    

rkr / php-ioc-contract example snippets


$serviceDispatcher = new ServiceDispatcher();
$serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function () {
	/* do something every hour */
});
$serviceDispatcher->run();

$serviceDispatcher = new ServiceDispatcher();
$serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) {
	/* do something every hour */
	$businessObject->doSomething();
});
$serviceDispatcher->run();

$container = new Container(erviceDispatcher($container);
$serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) {
	/* do something every hour */
	$businessObject->doSomething();
});
$serviceDispatcher->run();

use Ioc\MethodInvoker;

class ServiceDispatcher {
	/** @var MethodInvoker */
	private $methodInvoker;

	/* ... */

	/** @param MethodInvoker $methodInvoker */
	public function __construct(MethodInvoker $methodInvoker) {
		$this->methodInvoker = $methodInvoker;
	}

	/* ... */

	public function run() {
		foreach($this->registry as $serviceName => $service) {
			if($service['lockUntil'] > time()) {
				continue;
			}
			try {
				$this->methodInvoker->invoke($service['fn'], array('serviceName' => $serviceName));
			} finally {
				$service['lockUntil'] = time() + $service['timeout'];
			}
		}
	}
}