phplibrary/src/Layout/Manager.php

85 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Выбор макета страницы.
* Выбор оформления страницы осуществляется если было совпадение с каким либо условием
*/
namespace ctiso\Layout;
use ctiso\Filter\Filter,
ctiso\Functions,
ctiso\HttpRequest;
class Manager extends Filter
{
// Массив условий с их макетами
protected $condition = array();
/**
* Функция которая добавляет условие для проверки параметров $_GET
* @param $get array() | true Ассоциативный массив ключей и значений для $_GET
*
* @example
* addConditionGet(array('module' => 'personal'), 'personal')
* addConditionGet(array('module' => 'login'), 'login')
*/
public function addConditionGet($get, Filter $layout)
{
$this->addCondition(Functions::rcurry(array($this, 'checkGet'), $get), $layout);
}
/**
* Условие для аякс запросов. Тоже самое что и addConditionGet но еще проверяется является ли запрос ajax
*/
public function addConditionXHR($get, Filter $layout)
{
$this->addCondition(Functions::rcurry(array($this, 'checkXHR'), $get), $layout);
}
public function checkGet(/*.HttpRequest.*/$request, $get)
{
if (is_array($get)) {
foreach ($get as $key => $value) {
if ($request->get($key) != $value) {
return false;
}
}
}
return true;
}
public function checkXHR(/*.HttpRequest.*/$request, $get)
{
return $request->isAjax() && $this->checkGet($request, $get);
}
/**
* Добавляет условие в общем виде
* @parma $get function(HttpRequest) Функция
* @parma $layout Layout Макет
*/
public function addCondition($get, Filter $layout)
{
$this->condition [] = array($get, $layout);
}
/**
* Выбирает и применяет макет для страницы
*/
public function execute(HttpRequest $request)
{
// print_r($request->get('mode'));
foreach ($this->condition as $condition) {
if (call_user_func($condition[0], $request)) {
$layout = $condition[1];
$view = $layout->execute($request);
if (is_object($view)) {
echo $view->render();
} else {
echo $view;
}
return null;
}
}
}
}