PHP code example of giuseppe-mazzapica / faber
1. Go to this page and download the library: Download giuseppe-mazzapica/faber 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/ */
giuseppe-mazzapica / faber example snippets php
$container = new GM\Faber();
php
global $myapp;
$myapp = new GM\Faber();
php
$myapp = new GM\Faber();
$id = $myapp->getId(); // will be something like 'faber_5400d85c5a18c'
php
add_action( 'faber_' . $container->getId() . '_init', function( $container ) {
// do something with container
});
php
// define some services
$container['bar'] = function ( $cont, $args ) {
return new My\App\Bar( $cont['foo'] );
};
$container['foo'] = function ( $cont, $args ) {
return new My\App\Foo();
};
php
$bar = $container['bar']; // $bar is an instance of My\App\Bar
php
$bar = $container->get( 'bar' ); // $bar is an instance of My\App\Bar
php
$bar_1 = $container['bar'];
$bar_2 = $container->get( 'bar' );
var_dump( $bar_1 === $bar_2 ); // TRUE
$foo = $container['foo'];
// here getFoo() returns the instance of Foo injected in Bar constructor
$bar_foo = $bar_1->getFoo();
var_dump( $foo === $bar_foo ); // TRUE
php
class Post {
private $wp_post;
private $options;
function __construct( $id, Options $options ) {
$this->wp_post = get_post( $id );
$this->options = $options;
}
}
php
$container['post'] = function ( $cont, $args ) {
return new Post( $args['id'], $cont['options'] );
};
$container['options'] = function ( $cont, $args ) {
return new Options;
};
php
$post_1 = $container->get( 'post', [ 'id' => 1 ] );
$post_2 = $container->get( 'post', [ 'id' => 2 ] );
$post_1_again = $container->get( 'post', [ 'id' => 1 ] );
var_dump( $post_1 === $post_2 ); // FALSE: different args => different object
var_dump( $post_1 === $post_1_again ); // TRUE: same args => same object
php
$post_1 = $container->get( 'post', [ 'id' => 1 ] );
$post_1_again = $container->get( 'post', [ 'id' => 1 ] );
$post_1_fresh = $container->make( 'post', [ 'id' => 1 ] );
var_dump( $post_1 === $post_1_again ); // TRUE
var_dump( $post_1 === $post_1_fresh ); // FALSE: make() force fresh instances
php
class Foo {
function getBar() {
return new Bar;
}
}
class Bar {
function getBaz() {
return new Baz;
}
}
class Baz {
function getResult() {
return 'Result!';
}
}
php
$container['foo'] = function() {
return new Foo;
}
php
$result = $container['foo']->getBar()->getBaz()->getResult(); // 'Result!'
php
$result = $container['foo->getBar->getBaz->getResult'] // 'Result!'
php
// define properties
$container->add( 'version', '1.0.0' );
$container['timeout'] = HOUR_IN_SECONDS;
$container['app_path'] = plugin_dir_path( __FILE__ );
$container['app_assets_url'] = plugins_url( 'assets/' , __FILE__ );
// get properties
$version = $container['version'];
$timeout = $container->get( 'timeout' );
php
$app_path = $container->prop( 'app_path' );
php
$container->protect( 'greeting', function() {
return date('a') === 'am' ? 'Good Morning' : 'Good Evening';
});
php
$def = [
'timeout' => HOUR_IN_SECONDS,
'version' => '1.0.0',
'foo' => function( $container, $args ) {
return new Foo;
},
'bar' => function( $container, $args ) {
return new Bar( $container['foo'] );
}
];
$container = new GM\Faber( $def );
php
$container = new GM\Faber;
$container->load( $def ); // $def is same of above
php
// file must return an array
return [
'timeout' => HOUR_IN_SECONDS,
'version' => '1.0.0',
'foo' => function( $container, $args ) {
return new Foo;
},
'bar' => function( $container, $args ) {
return new Bar( $container['foo'] );
}
];
php
$container = new GM\Faber;
$container->loadFile( 'full/path/to/definitions/file.php' );
php
add_action( 'faber_' . $container->getId() . '_init', function( $container ) {
$container->loadFile( 'full/path/to/definitions.php' );
});
php
$container[ 'foo' ] = 'Foo';
$container[ 'bar' ] = 'Bar';
echo $container[ 'foo' ]; // Output "Foo"
echo $container[ 'bar' ]; // Output "Bar"
$container[ 'foo' ] = 'New Foo';
$container->update( 'bar', 'New Bar' );
echo $container[ 'foo' ]; // Output "New Foo"
echo $container[ 'bar' ]; // Output "New Bar"
php
$container[ 'foo_service' ] = function() {
return Foo;
};
$container->protect( 'closure', function() {
return 'Hello World!';
});
$container[ 'foo_service' ] = 'a string'; // will fail
$container->update( 'closure', [ 'a', 'b' ] ); // will fail
php
$container[ 'foo' ] = 'Foo';
$container[ 'bar' ] = 'Bar';
unset( $container[ 'foo' ] );
$container->remove( 'foo' );
php
$container[ 'foo' ] = 'Foo!';
$container->freeze( 'foo' );
unset( $container[ 'foo' ] ); // will fail
echo $container[ 'foo' ]; // still output "Foo!"
$container->update( 'foo', 'New Foo!' ); // will fail
echo $container[ 'foo' ]; // still output "Foo!"
$container->unfreeze( 'foo' );
$container[ 'foo' ] = 'New Foo!'; // will success
echo $container[ 'foo' ]; // output "New Foo!"
$container->remove( 'foo' ); // will success
echo $container[ 'foo' ]; // will output an error message
php
$foo = GM\Faber::instance('myapp')
->load( $defs )
->loadFile('defs.php')
->add( 'foo', 'Foo!' )
->add( 'bar', 'Bar!' )
->protect( 'hello', function() { return 'Hello!'; } )
->freeze( 'foo' )
->freeze( 'hello' )
->unfreeze( 'foo' )
->update( 'foo', 'New Foo!' )
->remove( 'bar' )
->get('foo'); // return 'New Foo!'
php
// define a service
$container['foo'] = function( $c, $args ) {
return new Foo( $args );
};
// create some objects
$foo_1 = $container->get( 'foo', [ 'id' => 'foo_1' ] );
$foo_2 = $container->get( 'foo', [ 'id' => 'foo_2' ] );
$foo_3 = $container->get( 'foo', [ 'id' => 'foo_3' ] );
php
$foo_3_bis = $container->get( 'foo', [ 'id' => 'foo_3' ] );
var_dump( $foo_3_bis === $foo_3 ); // TRUE: objects are cached
php
$foo_3_key = $container->getObjectKey( 'foo', [ 'id' => 'foo_3' ] );
echo $foo_3_key; // "foo_00000002264fbf000000005edc5c64_3cb01b53c9fbca307016d562fa42ce"
php
$key = $container->getObjectKey( 'foo', [ 'id' => 'foo_3' ] );
if ( $container->isCachedObject( $key ) ) {
// Yes, objects is cached
} else {
// No, objects is not cached
}
php
// define a service
$container['foo'] = function( $c, $args ) {
return new Foo( $args );
};
// create some objects
$foo_1 = $container->get( 'foo', [ 'id' => 'foo_1' ] );
$foo_2 = $container->get( 'foo', [ 'id' => 'foo_2' ] );
// get object keys
$key_1 = $container->getObjectKey( 'foo', [ 'id' => 'foo_1' ] );
$key_2 = $container->getObjectKey( 'foo', [ 'id' => 'foo_2' ] );
// test: are object cached?
var_dump( $container->isCachedObject( $key_1 ) ); // TRUE
var_dump( $container->isCachedObject( $key_2 ) ); // TRUE
// freeze first object
$container->freeze( $key_1 );
// delete factory closure
unset( $container['foo'] );
// test: are objects related to deleted factory deleted as well?
var_dump( $container->isCachedObject( $key_1 ) ); // TRUE: still there because frozen
var_dump( $container->isCachedObject( $key_2 ) ); // FALSE: vanished
// get cached object
$cached_foo = $container->get( $key_1 );
php
$foo = $container->getAndFreeze( 'foo', [ 'id' => 'foo_1' ] );
$key = $container->getObjectKey( 'foo', [ 'id' => 'foo_1' ] );
var_dump( $container->isFrozen( $key ) ); // TRUE
php
$faber = GM\Faber::i( 'test' ); // or $faber = new GM\Faber()
$faber['foo'] = function() {
return new Foo;
};
$sleep = serialize( $faber );
$wakeup = unserialize( $sleep );
$foo = $wakeup['foo'];
var_dump( $foo instanceof Foo ); // TRUE
php
$container[ 'foo' ] = 'Foo!';
$foo = $container[ 'foo' ];
if ( ! is_wp_error( $foo ) ) {
echo $foo; // Output "Foo!";
}
$meh = $container[ 'meh' ]; // 'meh' is an unregistered ID: $meh is a WP_Error object
if ( ! is_wp_error( $meh ) ) {
echo $meh; // not executed
}