<?php
namespace App\Core;
use PDO;
use App\Person\PersonRepository;
use App\Person\RemarkRepository;
use App\Person\PartnerRepository;
use App\Person\WohnorteRepository;
use App\Person\EreignisseRepository;
use App\Person\AdressenRepository;
use App\Person\PersonController;
use App\User\UsersRepository;
use App\User\LoginController;
use App\User\LoginService;

class Container
{
  private $receips = [];           // Bauanleitung für verschiedene instances
  private $instances = [];

  public function __construct()
  {
    $this->receips = [
      'loginService' => function() {
        return new LoginService(
          $this->make('usersRepository')
        );
      },
      'loginController' => function() {
        return new LoginController(
          $this->make('loginService')
        );
      },
      'personController' => function() {
        return new PersonController(         //neuer PersonsController
           $this->make('personRepository'),
           $this->make('remarkRepository'),
           $this->make('partnerRepository'),
           $this->make('wohnorteRepository'),
           $this->make('ereignisseRepository'),
           $this->make('adressenRepository')
        );
      },
      'usersRepository' => function() {
        return new UsersRepository(
          $this->make("pdo")
        );
      },
      'personRepository' => function() {
        return new PersonRepository(
          $this->make("pdo")
        );
      },
      'remarkRepository' => function() {
        return new RemarkRepository(
          $this->make("pdo")
        );
      },
      'partnerRepository' => function() {
        return new PartnerRepository(
          $this->make("pdo")
        );
      },
      'wohnorteRepository' => function() {
        return new WohnorteRepository(
          $this->make("pdo")
        );
      },
      'ereignisseRepository' => function() {
        return new EreignisseRepository(
          $this->make("pdo")
        );
      },
      'adressenRepository' => function() {
        return new AdressenRepository(
          $this->make("pdo")
        );
      },
      'pdo' => function() {
        $servername = "mysqle8e6.netcup.net";
        $port = "3306";
        $username = "k46054_hbc";
        $password = "cXZm/E97dKvZy6Cg*";
        $dbname="k46054_hbc";
        
        $pdo = new PDO("mysql:host=$servername;dbname=$dbname;port=$port", $username, $password);
        
        //$pdo = new PDO('mysql:host=localhost;dbname=stammbaum;charset=utf8','root','');
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        return $pdo;
      }
    ];
  }

  public function make($name)
  {
    if (!empty($this->instances[$name]))
      {
      return $this->instances[$name]; // DB-Verbindung besteht
      }
    // sonst ERZEUGE return $this->instances[$name
    if (isset($this->receips[$name])) {
      $this->instances[$name] = $this->receips[$name]();
    }
      return $this->instances[$name];
  }
}

?>
