2022-04-25 02:17:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Octoleo CMS
|
|
|
|
*
|
|
|
|
* @created 9th April 2022
|
|
|
|
* @author Llewellyn van der Merwe <https://git.vdm.dev/Llewellyn>
|
|
|
|
* @git WEBD-325-45 <https://git.vdm.dev/Llewellyn/WEBD-325-45>
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Octoleo\CMS\Service;
|
|
|
|
|
|
|
|
use Joomla\DI\Container;
|
|
|
|
use Joomla\DI\ServiceProviderInterface;
|
|
|
|
use Joomla\Registry\Registry;
|
|
|
|
|
2022-04-25 02:19:51 +00:00
|
|
|
/**
|
|
|
|
* Configuration service provider
|
|
|
|
*/
|
2022-04-25 02:17:01 +00:00
|
|
|
class ConfigurationProvider implements ServiceProviderInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Configuration instance
|
|
|
|
*
|
|
|
|
* @var Registry
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $file Path to the config file.
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException
|
|
|
|
*/
|
|
|
|
public function __construct(string $file)
|
|
|
|
{
|
|
|
|
// Verify the configuration exists and is readable.
|
|
|
|
if (!is_readable($file))
|
|
|
|
{
|
|
|
|
throw new \RuntimeException('Configuration file does not exist or is unreadable.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the class
|
|
|
|
include_once $file;
|
|
|
|
$this->config = new Registry(new \LConfig());
|
|
|
|
|
|
|
|
// Set database values based on config values
|
|
|
|
$this->config->loadObject( (object) [
|
|
|
|
'database' => [
|
|
|
|
'driver' => $this->config->get('dbtype'),
|
|
|
|
'host' => $this->config->get('host'),
|
|
|
|
'port' => $this->config->get('port', ''),
|
|
|
|
'user' => $this->config->get('user'),
|
|
|
|
'password' => $this->config->get('password'),
|
|
|
|
'database' => $this->config->get('db'),
|
|
|
|
'prefix' => $this->config->get('dbprefix')
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the service provider with a DI container.
|
|
|
|
*
|
|
|
|
* @param Container $container The DI container.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register(Container $container): void
|
|
|
|
{
|
|
|
|
$container->share(
|
|
|
|
'config',
|
|
|
|
function (): Registry
|
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|