classes/bfw/core/Controller.php
author Markus Bröker <broeker.markus@googlemail.com>
Fri, 13 Nov 2015 14:53:10 +0100
changeset 9 8c2f3735a9bd
parent 8 54ea0099329c
child 10 b441d3c0f572
permissions -rw-r--r--
Weitere Korrekturen nach dem Aufwachen

<?php

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

namespace bfw\core;

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

/**
 *
 */
abstract class Controller {
    protected $logger;
    /**
     * @var Model
     */
    private $model;
    /**
     * @var Request
     */
    private $request;
    /**
     * @var Response
     */
    private $response;
    /**
     * @var View
     */
    private $view;

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

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

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

    /**
     * @param $controller
     * @return string
     */
    public static function mapControllerName($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();

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

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