Initial Commit

This commit is contained in:
Netkas 2021-09-23 03:10:35 -04:00
commit 615d47895f
8 changed files with 327 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/
*.ppm

1
.ppm_package Normal file
View File

@ -0,0 +1 @@
src/acm2

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
clean:
rm -rf build
build:
mkdir build
ppm --compile="src/acm2" --directory="build"
install:
ppm --no-prompt --install="build/net.intellivoid.acm2.ppm" --fix-conflict

View File

@ -0,0 +1,29 @@
<?php
/** @noinspection PhpPropertyOnlyWrittenInspection */
namespace acm2\Exceptions;
use Exception;
use Throwable;
class ConfigurationFolderNotFoundException extends Exception
{
/**
* @var Throwable|null
*/
private ?Throwable $previous;
/**
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->message = $message;
$this->code = $code;
$this->previous = $previous;
}
}

View File

@ -0,0 +1,30 @@
<?php
/** @noinspection PhpUnused */
/** @noinspection PhpPropertyOnlyWrittenInspection */
namespace acm2\Exceptions;
use Exception;
use Throwable;
class ConfigurationNotDefinedException extends Exception
{
/**
* @var Throwable|null
*/
private ?Throwable $previous;
/**
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->message = $message;
$this->code = $code;
$this->previous = $previous;
}
}

View File

@ -0,0 +1,68 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace acm2\Objects;
class Schema
{
/**
* @var string
*/
private $name;
/**
* @var array
*/
private $structure;
public function __construct()
{
$this->name = 'default_schema';
$this->structure = [];
}
/**
* @param string $name
* @param mixed $default_value
*/
public function setDefinition(string $name, $default_value)
{
$this->structure[$name] = $default_value;
}
/**
* @param string $name
*/
public function removeDefinition(string $name)
{
if(isset($this->structure[$name]))
unset($this->structure[$name]);
}
/**
* Returns structure as array
*
* @return array
*/
public function toArray(): array
{
return $this->structure;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
}

151
src/acm2/acm2.php Normal file
View File

@ -0,0 +1,151 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace acm2;
use acm2\Exceptions\ConfigurationFolderNotFoundException;
use acm2\Exceptions\ConfigurationNotDefinedException;
use acm2\Objects\Schema;
class acm2
{
/**
* @var string
*/
private $VendorName;
/**
* @var string
*/
private $WorkingDirectory;
/**
* @var string
*/
private $MasterConfigurationPath;
/**
* @var array|null
*/
private $Configuration;
/**
* @param string $vendor
* @throws ConfigurationFolderNotFoundException
*/
public function __construct(string $vendor)
{
$vendor = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $vendor);
$vendor = mb_ereg_replace("([\.]{2,})", '', $vendor);
$vendor = str_ireplace(' ', '_', $vendor);
$vendor = strtolower($vendor);
$this->VendorName = $vendor;
$this->WorkingDirectory = DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'acm';
$this->MasterConfigurationPath = $this->WorkingDirectory . DIRECTORY_SEPARATOR . $this->VendorName . '.json';
if(file_exists($this->WorkingDirectory) == false)
throw new ConfigurationFolderNotFoundException('The configuration folder \'' . $this->WorkingDirectory . '\' was not found');
}
/**
* Reloads the configuration or creates it if it doesn't exist
*/
public function reloadConfiguration()
{
if($this->Configuration == null)
{
$this->Configuration = [
'file_type' => 'acm_json',
'file_version' => '2.0.0.0',
'configuration' => []
];
}
if(file_exists($this->MasterConfigurationPath) == false)
{
file_put_contents($this->MasterConfigurationPath, json_encode($this->Configuration, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
return;
}
$this->Configuration = json_decode(file_get_contents($this->MasterConfigurationPath));
}
/**
* Saves the updated configuration to disk
*/
public function updateConfiguration()
{
if($this->Configuration == null)
$this->reloadConfiguration();
file_put_contents($this->MasterConfigurationPath, json_encode($this->Configuration, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
/**
* Defines a new schema in the configuration
*
* @param Schema $schema
*/
public function defineSchema(Schema $schema)
{
if($this->Configuration == null)
$this->reloadConfiguration();
if(isset($this->Configuration['configuration'][$schema->getName()]) == false)
{
$this->Configuration['configuration'][$schema->getName()] = $schema->toArray();
}
else
{
// Add missing values
foreach($schema->toArray() as $name => $value)
{
if(isset($this->Configuration['configuration'][$schema->getName()][$name]) == false)
$this->Configuration['configuration'][$schema->getName()][$name] = $value;
}
}
$this->updateConfiguration();
}
/**
* Returns a configuration
*
* @param string $name
* @return mixed
* @throws ConfigurationNotDefinedException
*/
public function getConfiguration(string $name)
{
if(isset($this->Configuration['configuration'][$name]) == false)
throw new ConfigurationNotDefinedException($name . ' is not defined in the configuration');
return $this->Configuration['configuration'][$name];
}
/**
* @return string
*/
public function getMasterConfigurationPath(): string
{
return $this->MasterConfigurationPath;
}
/**
* @return string
*/
public function getWorkingDirectory(): string
{
return $this->WorkingDirectory;
}
/**
* @return string
*/
public function getVendorName(): string
{
return $this->VendorName;
}
}

37
src/acm2/package.json Normal file
View File

@ -0,0 +1,37 @@
{
"package": {
"package_name": "net.intellivoid.acm2",
"name": "Advanced Configuration Manager v2",
"version": "1.0.0.0",
"author": "Zi Xing Narrakas",
"organization": "Intellivoid Technologies",
"description": "Stores and manages configuration files on the system using ACM File Format",
"url": "https://github.com/Intellivoid/acm2",
"dependencies": [],
"configuration": {
"autoload_method": "generated_spl",
"main": null,
"post_installation": [],
"pre_installation": []
}
},
"components": [
{
"required": true,
"file": "acm2.php"
},
{
"required": true,
"file": "Exceptions/ConfigurationFolderNotFoundException.php"
},
{
"required": true,
"file": "Exceptions/ConfigurationNotDefinedException.php"
},
{
"required": true,
"file": "Objects/Schema.php"
}
],
"files": []
}