Mini Shell
<?php
/**
* Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
*
* Licensed under CLOUD LINUX LICENSE AGREEMENT
* http://cloudlinux.com/docs/LICENSE.TXT
*/
namespace App\Base;
/**
* Class Controller
*
* @package selector\base
*/
class Controller {
/**
* Default controller directory
*/
const CONTROLLER_DIRECTORY = 'controller';
/**
* @var string
*/
public $pageTitle;
/**
* @var string
*/
private $controllerDirectory;
/**
* View object
*
* @var View
*/
private $_view;
/**
* Controller object
*
* @var Controller
*/
protected $controller;
/**
* @var string
*/
protected $controllerName;
/**
* @var string
*/
protected $action;
/**
* @var string
*/
public $defaultAction = 'index';
/**
*
*/
public function __construct()
{
$this->controllerDirectory = Base::load()->getBilling();
$this->init();
}
/**
* Initialize method
*/
public function init()
{
}
/**
* Try to resolve current controller by given params
*
* @param $controller
* @param $action
* @return mixed
* @throws \Exception
*/
public function getController($controller, $action)
{
$this->action = empty($action) ? $this->defaultAction : $action;
$this->controllerName = lcfirst($controller);
try {
$partsOfController = explode('-', $controller);
$controllerClass = '';
foreach ($partsOfController as $part) {
$controllerClass .= ucfirst($part);
}
$controllerClass .= 'Controller';
$this->controller = '\\App\\Controller\\'.$this->controllerDirectory.'\\'.$controllerClass;
if (!class_exists($this->controller)) {
throw new Exception('Undefined page');
}
$model = new $this->controller;
$model->controller = $this->controller;
$model->controllerName = $this->controllerName;
$model->action = $this->action;
$model->setView();
return $model;
} catch(\Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Set view object
*/
public function setView()
{
$this->_view = new View($this->controllerName);
}
/**
* Get View object
*
* @return View
*/
public function getView()
{
return $this->_view;
}
/**
* Run controller action
*
* @throws \Exception
*/
public function runAction()
{
$actionMethod = lcfirst($this->action) . 'Action';
if(!method_exists($this, $actionMethod)) {
throw new Exception('Undefined controller action "'.$this->action.'"');
}
$method = new \ReflectionMethod($this->controller, $actionMethod);
$method->invoke($this);
}
/**
* Render view file with layout
*
* @param string $view
* @param array $values
* @param bool $output
* @return string
*/
public function render($view = 'index', $values = array(), $output = true)
{
return $this->_view->render($view, $values, $output);
}
/**
* Render view file
*
* @param string $view
* @param array $values
* @param bool $output
* @return string
*/
public function renderPartial($view, $values, $output = true)
{
return $this->_view->renderPartial($view, $values, $output);
}
}
Zerion Mini Shell 1.0