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.
|
|
|
|
*
|
2015-01-18 11:15:45 +00:00
|
|
|
* @param string $name
|
2015-01-14 17:19:41 -06:00
|
|
|
* @param bool $checkEnv
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2015-01-18 11:15:45 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2015-01-18 11:15:45 +00:00
|
|
|
if (array_key_exists($name, $this->settings)) {
|
|
|
|
return $this->settings[$name];
|
2015-01-14 17:19:41 -06:00
|
|
|
}
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
if ($checkEnv) {
|
2015-01-18 11:15:45 +00:00
|
|
|
$env = getenv(strtoupper($name));
|
2015-01-14 17:19:41 -06:00
|
|
|
if (!$env) {
|
|
|
|
return $env;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $setting;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $setting;
|
|
|
|
}
|
2015-01-18 11:15:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates or updates a setting value.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*
|
2015-01-18 11:33:03 +00:00
|
|
|
* @return void
|
2015-01-18 11:15:45 +00:00
|
|
|
*/
|
|
|
|
public function set($name, $value)
|
|
|
|
{
|
2015-01-18 11:33:03 +00:00
|
|
|
$this->model->updateOrCreate(compact('name'), compact('value'));
|
2015-01-18 11:15:45 +00:00
|
|
|
}
|
2015-01-14 17:19:41 -06:00
|
|
|
}
|