PHP code example of brightnucleus / injector

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

    

brightnucleus / injector example snippets


class BookReader
{
    /** @var BookInterface */
    protected $book;

    public function __construct(BookInterface $book)
    {
        $this->book = $book;
    }

    public function read()
    {
        echo $this->book->getContents();
    }
}


$bookReader = $injector->make('BookReader');
// This will now echo the result of LatestBestseller::getContents().
$bookReader->read();

// Format:
//    '<class/interface>' => '<concrete class to instantiate>',
Injector::STANDARD_ALIASES => [
    'BrightNucleus\Config\ConfigInterface' => 'BrightNucleus\Config\Config',
]

// Format:
//    '<class/interface>' => '<concrete class to instantiate>',
Injector::SHARED_ALIASES => [
    'ShortcodeManager' => 'BrightNucleus\Shortcode\ShortcodeManager',
]

// Format:
//
// '<alias to provide argument for>' => [
//    '<argument>' => '<callable or scalar that returns the value>',
// ],
Injector::ARGUMENT_DEFINITIONS => [
	'PDO' => [
		'dsn'      => $dsn,
		'username' => $username,
		'passwd'   => $password,
	]
]

Injector::ARGUMENT_DEFINITIONS => [
	'config' => [
		'config' => new Injection( 'My\Custom\ConfigClass' ),
	]
]

// Format:
// '<argument>' => [
//    'interface' => '<interface/class that the argument accepts>',
//    'mappings'  => [
//        '<alias to provide argument for>' => <callable that returns a matching object>,
//    ],
// ],
Injector::ARGUMENT_PROVIDERS => [
    'config' => [
        'interface' => ConfigInterface::class,
        'mappings'  => [
            ShortcodeManager::class => function ($alias, $interface) {
                return ConfigFactory::createSubConfig(
                    __DIR__ . '/config/defaults.php',
                    $alias
                );
            },
        ],
    ],
]

// Format:
//    '<alias>' => <callable to use as factory>
Injector::DELEGATIONS => [
	'Example\Namespace\ExampleDependency' => function ( InjectionChain $injectionChain ) {
		$parent = $injectionChain->getByIndex(-2);
		$factory = new \Example\Namespace\ExampleFactory();
		return $factory->createFor( $parent );
	},
]

// Format:
//    '<alias>' => <callable to execute after instantiation>
Injector::PREPARATIONS => [
	'PDO' => function ( $instance, $injector ) {
		/** @var $instance PDO */
		$instance->setAttribute(
			PDO::ATTR_DEFAULT_FETCH_MODE,
			PDO::FETCH_OBJ
		);
	},
]

$config = ConfigFactory::create([
    Injector::STANDARD_ALIASES => [
        'ExampleInterface' => 'ConcreteExample'
    ]
]);
$injector->registerMappings($config);
// Here, `$object` will be an instance of `ConcreteExample`.
$object = $injector->make('ExampleInterface');

[0] => 'Example\Namespace\ExampleClass'
[1] => 'Example\Namespace\ExampleDependency'