Cachet/app/Config/Repository.php

92 lines
2.1 KiB
PHP
Raw Normal View History

2015-01-14 17:19:41 -06:00
<?php
/*
* This file is part of Cachet.
*
* (c) James Brooks <james@cachethq.io>
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
* (c) Graham Campbell <graham@mineuk.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2015-01-16 00:04:14 -06:00
namespace CachetHQ\Cachet\Config;
2015-01-14 17:19:41 -06:00
2015-01-18 11:55:32 +00:00
use CachetHQ\Cachet\Models\Setting;
2015-01-14 17:19:41 -06:00
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.
*
2015-01-18 11:55:32 +00:00
* @var array|null
2015-01-14 17:19:41 -06:00
*/
2015-01-18 11:55:32 +00:00
protected $settings;
2015-01-14 17:19:41 -06:00
/**
* Create a new settings service instance.
*
* @param \CachetHQ\Cachet\Models\Setting $model
*/
2015-01-18 11:55:32 +00:00
public function __construct(Setting $model)
2015-01-14 17:19:41 -06:00
{
$this->model = $model;
}
/**
* Returns a setting from the database.
*
* @param string $name
* @param string|null $default
* @param bool $checkEnv
2015-01-14 17:19:41 -06:00
*
* @return string|null
*/
2015-05-20 12:56:59 -05:00
public function get($name, $default = null, $checkEnv = true)
2015-01-14 17:19:41 -06:00
{
2015-01-18 11:55:32 +00:00
// if we've not loaded the settings, load them now
if (!$this->settings) {
$this->settings = $this->model->all()->lists('value', 'name');
}
2015-01-14 17:19:41 -06:00
2015-01-18 11:55:32 +00:00
// if the setting exists, return it
if (isset($this->settings[$name])) {
return $this->settings[$name];
2015-01-14 17:19:41 -06:00
}
2015-01-18 11:55:32 +00:00
// fallback to getenv if allowed to
if ($checkEnv) {
if ($this->settings[$name] = env(strtoupper($name))) {
return $this->settings[$name];
}
2015-01-18 11:55:32 +00:00
}
2015-05-20 12:56:59 -05:00
return $default;
2015-01-14 17:19:41 -06:00
}
/**
* Creates or updates a setting value.
*
* @param string $name
* @param string $value
*/
public function set($name, $value)
{
2015-01-18 11:55:32 +00:00
// save the change to the db
$this->model->updateOrCreate(compact('name'), compact('value'));
2015-01-18 11:55:32 +00:00
// if we've loaded the settings, persist this change
if ($this->settings) {
$this->settings[$name] = $value;
}
}
2015-01-14 17:19:41 -06:00
}