classes/bfw/core/Controller.php
author Markus Bröker<broeker.markus@googlemail.com>
Tue, 19 Sep 2017 13:04:18 +0200
changeset 51 bfcfa759003a
parent 39 8b4f9c6136f4
permissions -rw-r--r--
GSV ignorieren

<?php

/**
 * Copyright(C) 2015 Markus Bröker<broeker.markus@googlemail.com>
 *
 */

namespace bfw\core;

use bfw\Request;
use bfw\Response;
use Logger;

/**
 * Class Controller
 */
abstract class Controller {

    protected $logger;

    /**
     * @var Model
     */
    private $model;

    /**
     * @var Request
     */
    private $request;

    /**
     * @var Response
     */
    private $response;

    /**
     * @var View
     */
    private $view;

    /**
     * Controller constructor.
     *
     */
    public function __construct() {
        $this->request = new Request();
        $this->response = new Response();

        $this->model = $this->getDataModelInstance();
        $this->view = new View($this, $this->model);

        $this->logger = Logger::getLogger(get_class($this));
    }

    private function getDataModelInstance() {
        $modelName = $this->getModelName();

        return new $modelName();
    }

    private function getModelName() {
        $modelName = str_replace('controller', 'model', get_class($this));
        $modelName = strtolower($modelName);

        return str_replace('controller', '\DataModel', $modelName);
    }

    /**
     * @param $controller
     * @return string
     */
    public static function findControllerInNameSpace($controller) {
        return sprintf('bfw\mvc\controller\%sController', ucfirst($controller));
    }

    /**
     * @return View
     */
    public function getView() {
        return $this->view;
    }

    /**
     * @param $view
     * @return $this
     */
    public function setView($view) {
        $this->view = $view;

        return $this;
    }

    /**
     * @return Response
     */
    public function getResponse() {
        return $this->response;
    }

    /**
     * @param $response
     * @return $this
     */
    public function setResponse($response) {
        $this->response = $response;

        return $this;
    }

    /**
     * @return Model
     */
    public function getModel() {
        return $this->model;
    }

    /**
     * @param $model
     * @return $this
     */
    public function setModel($model) {
        $this->model = $model;

        return $this;
    }

    /**
     * @return Request
     */
    public function getRequest() {
        return $this->request;
    }

    /**
     * @return mixed
     */
    abstract public function index();
}