Crafting Unique and Customizeable Websites is my Passion

Crafting Unique and Customizeable Websites is my Passion

hero frame

Container Class

The container class creates an array $recipes with functions for creating new instances of every controller and repository class.

<?php
namespace app\core;

use PDO;
use PDOException;

use app\blog\postrepo;
use app\blog\postmodel;
use app\blog\postscontroller;

use app\blog\admin\adminpostscontroller;

use app\blog\catrepo;
use app\blog\catcontroller;

use app\blog\hashrepo;
use app\blog\hashcontroller;

use app\blog\paginationrepo;
use app\blog\paginationcontroller;

use app\blog\commentrepo;

use app\user\userrepo;
use app\user\logincontroller;
use app\user\loginservice;


class container
{   
    private $recipes = [];
    private $instances = [];

    public function __construct()
    {
        $this->recipes = [
            "postscontroller" => function() {
                return new postscontroller(
                    $this->make("postrepo"),
                    $this->make("postmodel"),
                    $this->make("commentrepo")                    
                );
            },
            "adminpostscontroller" => function() {
                return new adminpostscontroller(
                    $this->make("postrepo"),
                    $this->make("postmodel"),
                    $this->make("hashrepo")                    
                );
            },
            "paginationcontroller" => function() {
                return new paginationcontroller(
                    $this->make("paginationrepo")
                );
            },
            "catcontroller" => function() {
                return new catcontroller(
                    $this->make("catrepo"),
                    $this->make("postrepo")
                );
            },
            "hashcontroller" => function() {
                return new hashcontroller(
                    $this->make("hashrepo"),
                    $this->make("postrepo")
                );
            },
            "postrepo" => function() {
               return new postrepo(
                   $this->make("pdo")
               );
            },
            "postmodel" => function() {
                return new postmodel();
             },
            "catrepo" => function() {
                return new catrepo(
                    $this->make("pdo")
and is initialised by the init.php
                );
             },
             "hashrepo" => function() {
                return new hashrepo(
                    $this->make("pdo")
                );
             },
             "paginationrepo" => function() {
                return new paginationrepo(
                    $this->make("postsproseite")
                );
             },
             "commentrepo" => function() {
                return new commentrepo(
                    $this->make("pdo")
                );
             },
             "userrepo" => function() {
                return new userrepo(
                    $this->make("pdo")
                );
             },
             "logincontroller" => function() {
                return new logincontroller(
                    $this->make("loginservice")          
                );
            },
            "loginservice" => function() {
                return new loginservice(
                    $this->make("userrepo")          
                );
            },
            "pdo" => function(){
                try
                {
                    $pdo = new PDO("mysql:dbname=microblog;host=localhost;charset=utf8",
                            "testadmin",
                            "MPDqTMjWyWlGX0FD") ;
                }catch(PDOException $e){
        
                    echo "Verbindung fehlgeschlagen";
                    die();
                }
        
                $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                return $pdo;
            },
            "postsproseite" => function(){
                $postsproseite = 2;
                return $postsproseite;
            }
        ];
    }
    
    public function make($name)
    {
        if(!empty($this->instances[$name]))
        {
            return $this->instances[$name];
        }

        if(isset($this->recipes[$name])) {
            $this->instances[$name] = $this->recipes[$name]();
        }

        // ERZEUGE: $this->instances[$name]

        return $this->instances[$name];

    }
    /*
    private $pdo;
    private $getPostRepo;
    private $getCatRepo;
    private $getHashRepo;
    private $getPaginationRepo;


    public function getPdo()
    {   
        if(!empty($this->pdo)){
            return  $this->pdo;
        }

        try
        {
            $this->pdo = new PDO("mysql:dbname=soztot;host=localhost;charset=utf8",
                    "admintest",
                    "MPDqTMjWyWlGX0FD") ;
        }catch(PDOException $e){

            //bei Nichtzustandekommen
            die("Keine Verbindung!");
        }

        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        return $this->pdo;
    }

    public function getPostRepo()
    {
        if(!empty($this->getPostRepo)){
            return  $this->getPostRepo;
        }

        $this->getPostRepo = new postrepo(
            $this->getPdo()
        );
        return $this->getPostRepo;
    }

    public function getCatRepo()
    {
        if(!empty($this->getCatRepo)){
            return  $this->getCatRepo;
        }
        
        $this->getCatRepo = new catrepo(
            $this->getPdo()
        );
        return $this->getCatRepo;
    }

    public function getHashRepo()
    {
        if(!empty($this->getHashRepo)){
            return  $this->getHashRepo;
        }
        
        $this->getHashRepo = new hashrepo(
            $this->getPdo()
        );
        return $this->getHashRepo;
    }

    public function getPaginationRepo()
    {
        if(!empty($this->getPaginationRepo)){
            return  $this->getPaginationRepo;
        }

        $this->getPaginationRepo = new paginationrepo(2);
        return $this->getPaginationRepo;
    }*/

}

?>

Object-Oriented Programming in PHP | figure 1.2: container.php

The container class is initialised by the init.php

<?php
require("autoload.php");
require("database.php");

function e($str)
{
    return htmlentities($str, ENT_QUOTES, "UTF-8");
}

$container = new app\core\container();

$userrepo = $container->make("userrepo");

?>

be the first to share shared this post

Leave a Reply

Your email address will not be published. Required fields are marked *

top frame
freebie picture
STAY TUNED &
SUBSCRIBE

Sign up to my mailing list and receive occasional updates, new bogposts, freebies, giveaways and more



    Terms of privacy and data protection



    Get in Touch

    Feel free to send me a message. I'll gladly respond to all your requests.