blue-twilight/app/Helpers/ConfigHelper.php

92 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Helpers;
use App\Configuration;
class ConfigHelper
{
public function allowedDateFormats()
{
return [
'Y-m-d - H:i',
'd/m/Y - H:i',
'm/d/Y - H:i',
'jS F Y - H:i',
];
}
public function allowedThemeNames()
{
$results = [];
$directoryIterator = new \FilesystemIterator(dirname(dirname(__DIR__)) . '/resources/views/themes', \FilesystemIterator::SKIP_DOTS);
/** @var \SplFileInfo $fileInfo */
foreach ($directoryIterator as $fileInfo)
{
if (!$fileInfo->isDir() || strtolower($fileInfo->getFilename()) == 'base')
{
continue;
}
$themeName = strtolower($fileInfo->getFilename());
$themeDisplayName = $themeName;
$themeInfoFilename = sprintf('%s/theme.json', $fileInfo->getPathname());
if (file_exists($themeInfoFilename))
{
$themeInfo = json_decode(file_get_contents($themeInfoFilename));
if ($themeInfo !== false && isset($themeInfo->name))
{
$themeDisplayName = $themeInfo->name;
}
}
$results[$themeName] = $themeDisplayName;
}
return $results;
}
public function defaults()
{
return array(
'app_name' => trans('global.app_name'),
'date_format' => $this->allowedDateFormats()[0]
);
}
public function get($key, $defaultIfUnset = true)
{
$config = Configuration::where('key', $key)->first();
return (is_null($config) ? $this->defaults()[$key] : $config->value);
}
public function getAll()
{
$results = array();
foreach (Configuration::all() as $config)
{
$results[$config->key] = $config->value;
}
return $results;
}
public function getOrCreateModel($key)
{
$config = Configuration::where('key', $key)->first();
if (is_null($config) || $config === false)
{
$config = new Configuration();
$config->key = $key;
$config->value = '';
$config->save();
}
return $config;
}
}