PHP code example of intraworlds / service-container

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

    

intraworlds / service-container example snippets


namespace Acme\Cache;

class MemoryAdapter {
  function get(string $key): string {}
  function set(string $key, string $string): void {}
}

namespace Acme\Cache;

class Client {
  private $adapter;

  function __construct(MemoryAdapter $adapter) {
    $this->adapter = $adapter;
  }

  function get(string $key) {
    $string = $this->adapter->get($key);
    return unserialize($string);
  }

  function set(string $key, $value): void {
    $string = serialize($value);
    $this->adapter->set($key, $string);
  }
}

namespace Acme;

use IW\ServiceContainer;

$container = new ServiceContainer;
$client = $container->get(Cache\Client::class);

namespace Acme\Cache;

class PhpSerializer {
  function serialize($value): string {}
  function unserialize(string $string) {}
}

namespace Acme\Cache;

class Client {
  private $adapter;

  function __construct(MemoryAdapter $adapter, PhpSerializer $serializer) {
    $this->adapter = $adapter;
    $this->serializer = $serializer;
  }

  function get(string $key) {
    $string = $this->adapter->get($key);
    return $this->serializer->unserialize($string);
  }

  function set(string $key, $value): void {
    $string = $this->serializer->serialize($value);
    $this->adapter->set($key, $string);
  }
}

$client = $container->get(Cache\Client::class);

abstract class Parent {
  function __construct(Dependency $dependency, ServiceContainer $container) {
    $this->dependency = $dependency;
    $container->resolve([$this, 'init']);
  }
}

class Child extends Parent {
  function init(AhotherDependency $another) {
    // ...
  }
}

interface OrderCommand
{
  function execute();
}

class OrderInvoker
{
  function __construct(private OrderCommand ...$commands) {}

  function execute() : void {
    array_walk($this->commands, fn($command) => $command->execute());
  }
}

// an alias but that's no good for multiple commands
$container->alias('OrderInvoker', 'ReserveItems');

// external factory
$container->bind('OrderInvoker', function (IW\ServiceContainer $container) {
  return new OrderInvoker($container->get('ReserveItems'), $container->get('SendInvoice'));
});

// internal factory
$container->bind('OrderInvoker', 'OrderInvoker::create');

class OrderInvoker
{
  static function create(ReserveItems $reserveItems, SendInvoice $sendInvoice) : OrderInvoker {
    return new OrderInvoker($reserveItems, $sendInvoice);
  }
}

// wiring factory
$container->wire('OrderInvoker', 'ReserveItems', 'SendInvoice');

// using annotations (TBD PHP 8.0), used as a fallback (can be overridden by defining factory directly (eg. in tests)
class OrderInvoker
{
  #[IW\ServiceContainer\Bind('create')]
  function __construct(private OrderCommand ...$commands) {}

  static function create(ReserveItems $reserveItems, SendInvoice $sendInvoice) : OrderInvoker {
    return new OrderInvoker($reserveItems, $sendInvoice);
  } 
}

$exception->getOrigin(); // returns first exception outside the framework (useful for avoiding of tracing)