Cachet/src/Config/Repository.php

82 lines
1.6 KiB
PHP
Raw Normal View History

2015-01-14 17:19:41 -06:00
<?php
2015-01-16 00:04:14 -06:00
namespace CachetHQ\Cachet\Config;
2015-01-14 17:19:41 -06:00
use Illuminate\Database\Eloquent\Model;
2015-01-16 00:04:14 -06:00
class Repository
2015-01-14 17:19:41 -06:00
{
/**
* The eloquent model instance.
*
* @var \CachetHQ\Cachet\Models\Setting
*/
protected $model;
/**
* Cache of the settings.
*
* @var null|array
*/
protected $settings = null;
/**
* Create a new settings service instance.
*
* @param \CachetHQ\Cachet\Models\Setting $model
*
* @return void
*/
public function __construct(Model $model)
{
$this->model = $model;
}
/**
* Returns a setting from the database.
*
* @param string $name
2015-01-14 17:19:41 -06:00
* @param bool $checkEnv
*
* @return string|null
*/
public function get($name, $checkEnv = true)
2015-01-14 17:19:41 -06:00
{
$setting = null;
try {
if (! $this->settings) {
$this->settings = $this->model->all()->lists('value', 'name');
}
if (array_key_exists($name, $this->settings)) {
return $this->settings[$name];
2015-01-14 17:19:41 -06:00
}
} catch (ErrorException $e) {
if ($checkEnv) {
$env = getenv(strtoupper($name));
2015-01-14 17:19:41 -06:00
if (!$env) {
return $env;
}
}
return $setting;
}
return $setting;
}
/**
* Creates or updates a setting value.
*
* @param string $name
* @param string $value
*
* @return void
*/
public function set($name, $value)
{
$this->model->updateOrCreate(compact('name'), compact('value'));
}
2015-01-14 17:19:41 -06:00
}