PHP code example of pyrsmk / chernozem

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

    

pyrsmk / chernozem example snippets


$chernozem = new Chernozem\Container();

// Set a value
$chernozem['foo'] = 72;

// Get a value
echo $chernozem['foo'];

// Test a value
if(isset($chernozem['foo'])) {
	// 'foo' value exists
}

// Remove a value
unset($chernozem['foo']);

// Set a value
$chernozem->set('foo', 72);

// Get a value
echo $chernozem->get('foo');

// Test a value
if($chernozem->has('foo')) {
	// 'foo' value exists
}

// Remove a value
$chernozem->remove('foo');

// Set a value
$chernozem->setFoo(72);

// Get a value
echo $chernozem->getFoo();

$chernozem = new Chernozem\Container([
	'foo' => 72,
	'bar' => 33
]);

$chernozem[] = 'foo';

$myclass = new Stdclass();
$chernozem[$myclass] = 72;
// Print '72'
echo $chernozem[$myclass];

$chernozem->clear();

$chernozem['some_service'] = $chernozem->factory(function($chernozem) {
	return new Some_Service();
});

$chernozem['some_service'] = $chernozem->service(function($chernozem) {
	return new Some_Service();
});

class MyService implements Chernozem\ServiceProviderInterface {

	public function register(Interop\Container\ContainerInterface $container) {
		$container['some_service1'] = new Some_Service();
		$container['some_service2'] = new Another_Service();
		$container['an_option'] = '[email protected]';
	}

}

$chernozem->register(new MyService());

// Set a list of fruits
$chernozem['fruits'] = ['apple', 'banana', 'pear'];
// Set type hinting
$chernozem->hint('fruits', 'array');
// Oops! Wrong type!
$chernozem['fruits'] = 72;

// Set a 'mailer' service
$chernozem['mailer'] = $chernozem->service(function($chernozem) {
	return new Mailer();
});
// Mark as read onyl
$chernozem->readonly('mailer');

$chernozem->setter('foo_service', function($service) {
	throw new Exception("'foo_service' is already set!");
	return $service; // Never run, it's only for the example
});

$chernozem->getter('foo_service', function($service) {
	$service->executeSomeAction();
	$service->executeAnotherAction();
	return $service;
});

$delegate_container = new SomeVendor\Container();

// ... some stuff ...

$chernozem->delegate($delegate_container);

$chernozem['some_service'] = $chernozem->service(function($delegate_container) {
	$some_service = new Some_Service();
	// Load an option that is registered in the delegate container
	$some_service->setOption('some_option', $delegate_container->get('some_option'));
	return $some_service;
});

// Instantiate containers
$container1 = new SomeVendor\Container();
$container2 = new AnotherVendor\Container();

// ... some stuff ...

// Add containers to the composite container
$composite = new Chernozem\Composite();
$composite->add($container1);
$composite->add($container2);

echo $composite['foo'];

if(isset($composite['foo'])){
	// the 'foo' key exists
}

echo count($chernozem);

foreach($chernozem as $id => $value) {
	var_dump($value);
}

$values = $chernozem->toArray();

$chernozem['some_service'] = $chernozem->service(function($c) {
	// do stuff
});

$closure = $chernozem->raw('some_service');

// Get 'foo' array
$foo = $chernozem['foo'];
// Add 'bar' value to the array
$foo['bar'] = 42;
// Update 'foo' value
$chernozem['foo'] = $foo;

$chernozem['foo'] = new Chernozem\Container(['bar' => 72]);
// Print '72'
echo $chernozem['foo']['bar'];
$chernozem['foo']['bar'] = 42;
// Print '42'
echo $chernozem['foo']['bar'];

$chernozem['service'] = $chernozem->service(function() {
	// First service
});

unset($chernozem['service']);

$chernozem['service'] = $chernozem->service(function() {
	// New service
});