2014-02-14 17:28:11 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
namespace Icinga\User\Preferences;
|
|
|
|
|
2014-11-07 13:53:03 +01:00
|
|
|
use Icinga\Application\Config;
|
2014-02-14 17:28:11 +01:00
|
|
|
use Icinga\User;
|
|
|
|
use Icinga\User\Preferences;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-04-08 13:28:45 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-04-07 15:13:28 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-06-06 08:43:13 +02:00
|
|
|
use Icinga\Data\Db\DbConnection;
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preferences store factory
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* use Icinga\Data\ConfigObject;
|
2014-02-14 17:28:11 +01:00
|
|
|
* use Icinga\User\Preferences;
|
|
|
|
* use Icinga\User\Preferences\PreferencesStore;
|
|
|
|
*
|
|
|
|
* // Create a INI store
|
2014-04-07 15:13:28 +02:00
|
|
|
* $store = PreferencesStore::create(
|
2014-11-18 13:11:52 +01:00
|
|
|
* new ConfigObject(
|
2015-01-23 16:03:29 +01:00
|
|
|
* 'store' => 'ini',
|
2014-06-02 12:09:16 +02:00
|
|
|
* 'config_path' => '/path/to/preferences'
|
2014-02-14 17:28:11 +01:00
|
|
|
* ),
|
2014-02-17 14:11:55 +01:00
|
|
|
* $user // Instance of \Icinga\User
|
2014-02-14 17:28:11 +01:00
|
|
|
* );
|
|
|
|
*
|
|
|
|
* $preferences = new Preferences($store->load());
|
2014-02-17 14:11:55 +01:00
|
|
|
* $preferences->aPreference = 'value';
|
2014-02-14 17:28:11 +01:00
|
|
|
* $store->save($preferences);
|
2014-04-07 15:13:28 +02:00
|
|
|
* </code>
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
|
|
|
abstract class PreferencesStore
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Store config
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @var ConfigObject
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
protected $config;
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given user
|
|
|
|
*
|
|
|
|
* @var User
|
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
protected $user;
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new store
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config The config for this adapter
|
|
|
|
* @param User $user The user to which these preferences belong
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public function __construct(ConfigObject $config, User $user)
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for the store config
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @return ConfigObject
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
public function getStoreConfig()
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for the user
|
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @return User
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
public function getUser()
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-07 15:13:28 +02:00
|
|
|
* Initialize the store
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
abstract protected function init();
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-07 15:13:28 +02:00
|
|
|
* Load preferences from source
|
2014-02-14 17:28:11 +01:00
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @return array
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
abstract public function load();
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-07 15:13:28 +02:00
|
|
|
* Save the given preferences
|
2014-02-14 17:28:11 +01:00
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @param Preferences $preferences The preferences to save
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
abstract public function save(Preferences $preferences);
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create preferences storage adapter from config
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config The config for the adapter
|
|
|
|
* @param User $user The user to which these preferences belong
|
2014-02-14 17:28:11 +01:00
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @return self
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError When the configuration defines an invalid storage type
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public static function create(ConfigObject $config, User $user)
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
2015-01-23 16:07:38 +01:00
|
|
|
$type = ucfirst(strtolower($config->get('store', 'ini')));
|
2014-04-08 13:28:45 +02:00
|
|
|
$storeClass = 'Icinga\\User\\Preferences\\Store\\' . $type . 'Store';
|
2014-02-14 17:28:11 +01:00
|
|
|
if (!class_exists($storeClass)) {
|
|
|
|
throw new ConfigurationError(
|
2014-08-22 11:46:11 +02:00
|
|
|
'Preferences configuration defines an invalid storage type. Storage type %s not found',
|
|
|
|
$type
|
2014-02-14 17:28:11 +01:00
|
|
|
);
|
|
|
|
}
|
2014-04-07 15:13:28 +02:00
|
|
|
|
2014-04-08 13:28:45 +02:00
|
|
|
if ($type === 'Ini') {
|
2014-11-07 13:53:03 +01:00
|
|
|
$config->location = Config::resolvePath('preferences');
|
2014-04-08 13:28:45 +02:00
|
|
|
} elseif ($type === 'Db') {
|
|
|
|
$config->connection = new DbConnection(ResourceFactory::getResourceConfig($config->resource));
|
|
|
|
}
|
|
|
|
|
2014-02-14 17:28:11 +01:00
|
|
|
return new $storeClass($config, $user);
|
|
|
|
}
|
|
|
|
}
|