PHP code example of marcel-strahl / container

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

    

marcel-strahl / container example snippets




use MarcelStrahl\Container\FileLoader\AdapterBuilder;
use MarcelStrahl\Container\FileLoader\PHPArrayAdapter;
use MarcelStrahl\Container\ObjectBuilder\ObjectBuilderFactory;
use MarcelStrahl\Container\Delegator\ObjectDelegator;
use MarcelStrahl\Container\ClassContainer;
use MarcelStrahl\Container\ObjectContainer;
use MarcelStrahl\Container\AppContainer;
use MarcelStrahl\Container\Dto\ObjectStore;
use MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummy;

$adapter = (new AdapterBuilder())->build(PHPArrayAdapter::class);

$path = sprintf('%s/../Unit/FileLoader/php_array_config.php', __DIR__);
Assert::stringNotEmpty($path);

$builderFactory = new ObjectBuilderFactory();
$delegator = new ObjectDelegator($builderFactory);

$classStore = $adapter->loadFileFromPath($path, ClassStore::create());

$classContainer = ClassContainer::create($classStore);
$classContainer->compile();

$objectContainer = new ObjectContainer(ObjectStore::create(), $delegator);

$app = AppContainer::initialize($classContainer, $objectContainer);
$builderFactory->setContainer($app);

// Insert `$app` into your application context



declare(strict_types=1);

return [
    // class => [factory?: class-string, alias?: class-string, id?: non-empty-string]
    \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummy::class => [],
    \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummyWithFactory::class => [
        'factory' => \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummyWithFactory\Factory::class,
    ],
    \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummyWithServiceId::class => [
        'id' => 'serviceId',
    ],
    \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummyWithAlias::class => [
        'alias' => \MarcelStrahl\Tests\Unit\FileLoader\data\AliasInterface::class,
    ],
    \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummyWithFactory::class => [
        'alias' => \MarcelStrahl\Tests\Unit\FileLoader\data\AliasInterface::class,
        'id' => 'serviceId',
        'factory' => \MarcelStrahl\Tests\Unit\FileLoader\data\PhpArrayLoaderClassDummyWithFactory\Factory::class,
    ],
];