<commit_before><?php

namespace Bone\Mvc;

use Bone\Db\Adapter\MySQL;
use Bone\Mvc\View\ViewEngine;
use Bone\Mvc\View\PlatesEngine;
use PDO;
use Psr\Http\Message\ServerRequestInterface;
use stdClass;

class Controller
{
    /** @var ServerRequestInterface */
    protected $request;

    /** @var ViewEngine $plates */
    protected $viewEngine;

    /** @var string $controller */
    protected $controller;

    /** @var string $action  */
    protected $action;

    /** @var stdClass $view */
    public $view;

    /** @var string $body */
    private $body;

    /** @var bool */
    private $layoutEnabled;

    /** @var bool */
    private $viewEnabled;

    /** @var array $headers */
    private $headers;

    /** @var int $statusCode */
    private $statusCode = 200;

    /** @var object $params */
    public $params;

    /** @var array $post */
    protected $post = [];

    /** @var MySQL */
    protected $_db;

    /**
     * Controller constructor.
     * @param ServerRequestInterface $request
     */
    public function __construct(ServerRequestInterface $request)
    {
        $this->request = $request;
        $this->headers = [];
        $this->params = (object) $this->request->getQueryParams();

        if ($this->request->getMethod() == 'POST') {
            $this->post = $this->request->getParsedBody();
        }

        $this->initViewEngine();
        $this->view = new stdClass();
        $this->layoutEnabled = true;
        $this->viewEnabled = true;
    }

    /**
     * @return void
     */
    protected function setDB()
    {
        $config = Registry::ahoy()->get('db');
        $this->_db = new MySQL($config);
    }

    /**
     * @return void
     */
    protected function initViewEngine()
    {
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
        $engine = new PlatesEngine($viewPath);
        $this->viewEngine = $engine;
    }

    /**
     * @return PDO
     */
    public function getDbAdapter()
    {
        if(!$this->_db)
        {
            $this->setDB();
        }
        return $this->_db->getConnection();
    }

    /**
     * @return ViewEngine
     */
    public function getViewEngine()
    {
        return $this->viewEngine;
    }


    /**
     *  runs before th' controller action
     */
    public function init()
    {
        // extend this t' initialise th' controller
    }

    public function getParams()
    {
        return array_merge((array) $this->params, $this->post);
    }

    /**
     * @param $param
     * @return mixed
     */
    public function getParam($param, $default = null)
    {
        $params = $this->getParams();
        return isset($params->$param) ? urldecode($params->$param) : $default;
    }

    /**
     * @param $key
     * @param $val
     * @return $this
     */
    public function setParam($key, $val)
    {
        $this->params->$key = $val;
        return $this;
    }

    /**
     *  runs after yer work is done
     */
    public function postDispatch()
    {
        // extend this t' run code after yer controller is finished
    }

    /**
     *  For loadin' th' cannon, so t' speak
     *
     * @return array
     */
    public function getHeaders()<commit_msg>Update Controller.php<commit_after><?php

namespace Bone\Mvc;

use Bone\Db\Adapter\MySQL;
use Bone\Mvc\View\ViewEngine;
use Bone\Mvc\View\PlatesEngine;
use PDO;
use Psr\Http\Message\ServerRequestInterface;
use stdClass;

class Controller
{
    /** @var ServerRequestInterface */
    protected $request;

    /** @var ViewEngine $plates */
    protected $viewEngine;

    /** @var string $controller */
    protected $controller;

    /** @var string $action  */
    protected $action;

    /** @var stdClass $view */
    public $view;

    /** @var string $body */
    private $body;

    /** @var bool */
    private $layoutEnabled;

    /** @var bool */
    private $viewEnabled;

    /** @var array $headers */
    private $headers;

    /** @var int $statusCode */
    private $statusCode = 200;

    /** @var object $params */
    public $params;

    /** @var array $post */
    protected $post = [];

    /** @var MySQL */
    protected $_db;

    /**
     * Controller constructor.
     * @param ServerRequestInterface $request
     */
    public function __construct(ServerRequestInterface $request)
    {
        $this->request = $request;
        $this->headers = [];
        $this->params = (object) $this->request->getQueryParams();

        if ($this->request->getMethod() == 'POST') {
            $this->post = $this->request->getParsedBody();
        }

        $this->initViewEngine();
        $this->view = new stdClass();
        $this->layoutEnabled = true;
        $this->viewEnabled = true;
    }

    /**
     * @return void
     */
    protected function setDB()
    {
        $config = Registry::ahoy()->get('db');
        $this->_db = new MySQL($config);
    }

    /**
     * @return void
     */
    protected function initViewEngine()
    {
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
        $engine = new PlatesEngine($viewPath);
        $this->viewEngine = $engine;
    }

    /**
     * @return PDO
     */
    public function getDbAdapter()
    {
        if(!$this->_db)
        {
            $this->setDB();
        }
        return $this->_db->getConnection();
    }

    /**
     * @return ViewEngine
     */
    public function getViewEngine()
    {
        return $this->viewEngine;
    }


    /**
     *  runs before th' controller action
     */
    public function init()
    {
        // extend this t' initialise th' controller
    }

    public function getParams()
    {
        return array_merge((array) $this->params, $this->post);
    }

    /**
     * @param $param
     * @return mixed
     */
    public function getParam($param, $default = null)
    {
        return isset($this->params->$param) ? urldecode($this->params->$param) : $default;
    }

    /**
     * @param $key
     * @param $val
     * @return $this
     */
    public function setParam($key, $val)
    {
        $this->params->$key = $val;
        return $this;
    }

    /**
     *  runs after yer work is done
     */
    public function postDispatch()
    {
        // extend this t' run code after yer controller is finished
    }

    /**
     *  For loadin' th' cannon, so t' speak
     *
     * @return array
     */
    public function getHeaders()
{'fraction_non_alphanumeric': 0.15156633906633907, 'fraction_numerical': 0.0009213759213759214, 'mean_word_length': 1.9483929379809868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4589041', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1938, 'n_words': 452}