Added unfinished environment configuration manager
This commit is contained in:
parent
b93fe00474
commit
21bfd8797a
3 changed files with 211 additions and 0 deletions
|
@ -14,6 +14,7 @@
|
|||
use ncc\Exceptions\InvalidScopeException;
|
||||
use ncc\Exceptions\InvalidVersionNumberException;
|
||||
use ncc\Exceptions\MalformedJsonException;
|
||||
use ncc\Exceptions\MethodNotAvailableException;
|
||||
use ncc\Exceptions\ProjectAlreadyExistsException;
|
||||
use ncc\Exceptions\RuntimeException;
|
||||
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\Classes\EnvironmentConfiguration;
|
||||
|
||||
use ncc\Objects\PhpConfiguration;
|
||||
|
||||
class EnvironmentConfiguration
|
||||
{
|
||||
/**
|
||||
* Returns an array of all the current configuration values set in this environment
|
||||
*
|
||||
* @return PhpConfiguration[]
|
||||
*/
|
||||
public static function getCurrentConfiguration(): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach(ini_get_all() as $name => $config)
|
||||
{
|
||||
$results[$name] = PhpConfiguration::fromArray($config);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of only the changed configuration values
|
||||
*
|
||||
* @return PhpConfiguration[]
|
||||
*/
|
||||
public static function getChangedValues(): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach(ini_get_all() as $name => $config)
|
||||
{
|
||||
$config = PhpConfiguration::fromArray($config);
|
||||
if($config->LocalValue !== $config->GlobalValue)
|
||||
{
|
||||
$results[$name] = $config;
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file_path
|
||||
* @return void
|
||||
*/
|
||||
public static function export(string $file_path)
|
||||
{
|
||||
$configuration = [];
|
||||
foreach(self::getChangedValues() as $changedValue)
|
||||
{
|
||||
$configuration[$changedValue->getName()] = $changedValue->getValue();
|
||||
}
|
||||
|
||||
// TODO: Implement ini writing process here
|
||||
}
|
||||
|
||||
public static function import(string $file_path)
|
||||
{
|
||||
// TODO: Implement ini reading process here
|
||||
$configuration = [];
|
||||
foreach($configuration as $item => $value)
|
||||
{
|
||||
ini_set($item, $value);
|
||||
}
|
||||
}
|
||||
}
|
139
src/ncc/Objects/PhpConfiguration.php
Normal file
139
src/ncc/Objects/PhpConfiguration.php
Normal file
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace ncc\Objects;
|
||||
|
||||
class PhpConfiguration
|
||||
{
|
||||
/**
|
||||
* The Configuration name
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private $Name;
|
||||
|
||||
/**
|
||||
* The default value that's globally set
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $GlobalValue;
|
||||
|
||||
/**
|
||||
* The local value that has been modified by the program
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $LocalValue;
|
||||
|
||||
/**
|
||||
* The access level for modifying this configuration value
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $Access;
|
||||
|
||||
/**
|
||||
* Sets a value to this Php Configuration
|
||||
*
|
||||
* @param string $value
|
||||
* @return bool
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function setValue(string $value): bool
|
||||
{
|
||||
if($this->Name == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ini_set($this->Name, $value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value set for this configuration
|
||||
*
|
||||
* @return string
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->LocalValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the configuration value to its default state
|
||||
*
|
||||
* @return bool
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function resetValue(): bool
|
||||
{
|
||||
if($this->Name == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ini_restore($this->Name);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the object
|
||||
*
|
||||
* @return array
|
||||
* @noinspection PhpArrayShapeAttributeCanBeAddedInspection
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'global_value' => $this->GlobalValue,
|
||||
'local_value' => $this->LocalValue,
|
||||
'access' => $this->Access
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the object from an array representation
|
||||
*
|
||||
* @param array $data
|
||||
* @param string|null $name
|
||||
* @return PhpConfiguration
|
||||
*/
|
||||
public static function fromArray(array $data, ?string $name=null): PhpConfiguration
|
||||
{
|
||||
$PhpConfiguratoinObject = new PhpConfiguration();
|
||||
|
||||
if($name !== null)
|
||||
{
|
||||
$PhpConfiguratoinObject->Name = $name;
|
||||
}
|
||||
|
||||
if(isset($data['global_value']))
|
||||
{
|
||||
$PhpConfiguratoinObject->GlobalValue = $data['global_value'];
|
||||
}
|
||||
|
||||
if(isset($data['local_value']))
|
||||
{
|
||||
$PhpConfiguratoinObject->LocalValue = $data['local_value'];
|
||||
}
|
||||
|
||||
if(isset($data['access']))
|
||||
{
|
||||
$PhpConfiguratoinObject->Access = $data['access'];
|
||||
}
|
||||
|
||||
return $PhpConfiguratoinObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->Name;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue