PHP code example of dhii / containers

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

    

dhii / containers example snippets


    // Retrieve factories and extensions from respective files, and create a service provider with them
    $factories = / Perhaps retrieve service providers from other modules and aggregate them
    $provider = new CompositeCachingServiceProvider([$appProvider, $moduleProviderA, $moduleProviderB]);
    
    $proxyContainer = new ProxyContainer(); // A temporary parent container for lookup delegation
    $container = new DelegatingContainer($provider, $proxyContainer); // Container with application configuration
    $appContainer = new CompositeContainer([ // The application's container
        $dbContainer, // <-- Perhaps another container with configuration from DB
        $container, // <-- The main container with merged configuration from modules
    ]);
    $appContainer = new CachingContainer($appContainer); // Add caching, so that each service definition is only invoked once
    $proxyContainer->setInnerContainer($appContainer); // Switch lookup to the application's main container, making it available in service definitions
    
    // Retrieve cached configuration aggregated from various modules and other sources, sucha as the database or a remote API
    $appContainer->get('my-service');

[
    'serviceA' =>
        /** @tag letters */
        fn (): string => 'A',
    'serviceB' =>
        /**
         * @tag letters
         */
        function (): string {
            return 'B';
        },
    'serviceC' => function (ContainerInterface $c): string {
        var_dump($c->get('letters'));
    },
];

// App configuration, perhaps from a file
$config = [
  'dev' => [
    'db' => [
      'host' => 'localhost',
      'username' => 'root',
      'password' => '',
      'database' => 'my_app',
    ],
  'staging' => [
    'db' => [
      'host' => '123.abc.com',
      'username' => 'application123',
      'password' => '$*!@$T123SAfa',
      'database' => 'my_app',
    ],
  ],
];

// Can create container hierarchies of arbitrary depths from array hierarchies
$factory = new DataStructureBasedFactory(new DictionaryFactory());
// The new configuration interface
$config = $factory->createContainerFromArray($config);

// Output the DB host names for each environment
foreach ($config as $env => $envConfig) {
  echo $env . ': ' . $envConfig->get('db')->get('host') . PHP_EOL; // Print 'dev: localhost' then 'staging: 123.abc.com'
}

// Access configuration using a path
$config = new PathContainer($config, '/');
echo $config->get('staging/db/username'); // Print 'application123'

// Access dev env DB config with a 'local_' prefix
$localDbConfig = new PrefixingContainer($config->get('dev/db'), 'local_');
echo $localDbConfig->get('local_username'); // Print 'root'

// Effectively adds production DB configuration
$productionConfig = new Dictionary([
  'production' => [
    'db' => [
      'host' => 'db.myserver.com',
      'username' => 'D97rh1d0A&13',
      'password' => 'DN(Q(u3dgh3q87g3',
      'database' => 'my_app',
    ],
  ],
]);
$config = new CompositeContainer([$config, $productionConfig]);
echo $config->get('production/db/password'); // Print 'DN(Q(u3dgh3q87g3'
echo $config->get('dev/db/password'); // Print '': all of the old configuration is available on this new container

// Make production host also available as 'live_db_host' - maybe something