PHP code example of subcosm / hive-foundation

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

    

subcosm / hive-foundation example snippets


use Subcosm\Hive\Container\HiveNode;

$root = new HiveNode();

use Subcosm\Hive\Container\HiveNode;

$root = new HiveNode();

$root->set('foo', 'bar');

use Subcosm\Hive\Container\HiveNode;

$root = new HiveNode();

$root->set('foo', function() {
    return 'Hello World';
});

use Subcosm\Hive\Container\HiveNode;

$root = new HiveNode();

$root->set('foo', function() {
    return 'Hello World';
});

echo $root->get('foo'); // = Hello World

use Subcosm\Hive\Container\HiveNode;

$root = new HiveNode();

$root->set('router', function() use ($root) {
    return $root->get('router.factory')->factorize();
});

$root->set('router.factory', function() {
    return new RouterFactory();
});

$router = $root->get('router');

use Subcosm\Hive\Container\HiveNode;

$root = new HiveNode();

$root->set('logger', function() {
    return new Monolog\Logger();
});

$routing = $root->node('router', true);

$root->set('router', function() use ($routing) {
    return $routing->get('factory')->factorize();
});

$routing->set('factory', function() use ($routing) {
    return new RouteFactory($routing->get('~logger'));
});

$router = $root->get('router');

// or

$router = $routing->get('~router');

use Subcosm\Hive\Container\DeclarativeHiveNode;

$node = new DeclarativeHiveNode();

$node->entity('foo', function($value) {
    if ( ! is_string($value) ) {
        throw new InvalidArgumentException('Value must be string for foo');
    }
    
    return $value;
});

$node->set('foo', 12345); // throws the exception
$node->set('foo', '12345'); // sets the value

use Subcosm\Hive\Container\DeclarativeHiveNode;

$node = new DeclarativeHiveNode();

$node->defaultEntity(function($value) {
    return is_array($value) || is_object($value) ? json_encode($value) : (string) $value;
});

$node->set('foo', 12345); // foo => '12345'
$node->set('foo', ['foo' => 'bar']); // foo => {"foo":"bar"}

use Subcosm\Hive\{
    Container\HiveNode,
    Loader\ArrayLoader
};

$node = new HiveNode();
$loader = new ArrayLoader();
$loader->load([
    'foo.bar' => 'baz'
]);

$loader->injectInto($node);

echo $node->get('foo.bar'); // => "baz"