phplibrary/src/Controller/Service.php

102 lines
2.3 KiB
PHP

<?php
/**
* Класс сервиса = Упрощенный компонент
*/
namespace ctiso\Controller;
use ctiso\Path;
use ctiso\Model\BaseMapper;
use ctiso\File;
use ctiso\Registry;
use ctiso\Database\PDOStatement;
use ctiso\Database;
class Service
{
/** @var array */
public $viewPath = [];
/** @var array */
public $webPath = [];
/** @var Registry */
public $config;
/** @var string */
public $template;
/** @var string */
public $templatePath;
/** @var string */
public $COMPONENTS_WEB;
/** @var Database */
public $db;
/**
* Возвращает путь к шаблонам
* @param string $name Имя шаблона
* @return string
*/
public function getTemplatePath($name)
{
return Path::join($this->viewPath[0], 'templates', 'modern', $name);
}
/**
* Возвращает путь к шаблонам
* @return string
*/
public function getTemplateWebPath()
{
return Path::join($this->webPath[0], strtolower(get_class($this)), 'templates', 'modern');
}
/**
* Создает модель
* @param class-string $modelName
* @return BaseMapper
*/
public function getModel($modelName)
{
$model = new $modelName();
$model->db = $this->db;
return $model;
}
/**
* @param string $key
* @param string $val
* @param PDOStatement $res
* @return array
*/
public function options($key, $val, $res) {
$result = [];
while($res->next()) {
$result[] = ['value' => $res->getInt($key), 'name' => $res->getString($val)];
}
return $result;
}
/**
* @param array $list
* @param bool $selected
* @return array
*/
public function optionsPair($list, $selected = false) {
$result = [];
foreach ($list as $key => $value) {
$result [] = ['value' => $key, 'name' => $value, 'selected' => $key == $selected];
}
return $result;
}
/**
* @return array
*/
function getInfo() {
$filename = Path::join($this->viewPath[0], 'install.json');
if (file_exists($filename)) {
$settings = json_decode(File::getContents($filename), true);
return $settings;
}
return [];
}
}