PHP code example of everest / container

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

    

everest / container example snippets


use Everest\Container\Container;

$container = (new Container())
	->value('factor', 2)
	->service('multiplier', ['factor', 'Vendor\\Project\\Multiplier'])
	->factory('double', ['multiplier', function($theMultiplierService){
		return function($number) use ($theMultiplierService) {
			return $multiplierServcie->multiply($number);
		};
	}]);

echo $container['factor']; // 2
echo $container['double'](10); // 20


function some_function($A) {
	echo "function: $A";
}

class Foo {
	public static function bar($A) {
		echo "static method: $A";
	}

	public function baz($A) {
		echo "method: $A";
	}
}

$object = new Foo;

// Setup container

$container = (new Container)
	// Add some content
	->value('A', 'Some value')
	->value('InnerCallbackObject', $object)
	->value('InnerCallbackClosure', function($A){
		echo "inner: $A";
	})

	// Case 1: Closure
	->factory(['A', function($A) {
			echo "closure $A";
		}])

	// Case 2: Function
	->factory('Function', ['A', 'some_function'])

	// Case 3: Static method
	->factory('Static1',  ['A', [Foo::CLASS, 'bar']])

	// Case 4: Static method variant
	->factory('Static2',  ['A', Foo::CLASS . '::bar'])

	// Case 5: Public method
	->factory('Public',   ['A', [$object, 'baz']])

	// Case 7: Container internal callback object
	->factory('Inner',    ['A', ['InnerCallbackObject', 'baz']])

	// Case 7: Container internal callback closure
	->factory('Inner',    ['A', ['InnerCallbackClosure']])

$container = (new Container)
	->value('A', 'Value');

$container = (new Container)
	->value('DependencyA', 'Value')

	// With dependency hint
	->factory('Name', ['DependencyA', function($a) {
		echo $a; // Value
	}])

	// Auto resolve
	->factory('Name', function($DependencyA) {
		echo $DependencyA; // Value
	}]);


class Foo {
	public function __construct($DependencyA) {
		echo $DependencyA; // Value
	}
}

$container = (new Container)
	->value('DependencyA', 'Value')

	// With dependency hint
	->factory('Name', ['DependencyA', Foo::CLASS])
	
	// Auto resolve
	->factory('Name', Foo::CLASS);

$container = (new Container)
	->factory('SomeName', [function(){
		return 'Hello';
	}])
	->decorator('SomeName', ['DecoratedInstance', function($org) {
		return $org . 'World';
	}]);

echo $container['SomeName']; // HelloWorld

class PrefixerProvider {
	private $prefix = 'Hello';

	public $factory;

	public function __construct()
	{
		$this->factory = ['Name', [$this, 'factory']];
	}

	public function setPrefix(string $prefix) : void
	{
		$this->prefix = $prefix;
	}

	public function factory(string $name) : string
	{
		return sprtinf('%s %s', $this->prefix, $name);
	}
}


$container = (new Container)
	->factory('Name', [function(){
		return 'Justus';
	}])
	->provider('PrefixedName', new PrefixerProvider))
	->config(['PrefixedNameProvider', function($provider) {
		$provider->setPrefix('Goodbye');
	}]);

echo $container['PrefixedName']; // Goodbye Justus