From 615d47895f7b7ca54be2ca0d76f136eb76d9e55d Mon Sep 17 00:00:00 2001 From: Netkas Date: Thu, 23 Sep 2021 03:10:35 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 2 + .ppm_package | 1 + Makefile | 9 ++ .../ConfigurationFolderNotFoundException.php | 29 ++++ .../ConfigurationNotDefinedException.php | 30 ++++ src/acm2/Objects/Schema.php | 68 ++++++++ src/acm2/acm2.php | 151 ++++++++++++++++++ src/acm2/package.json | 37 +++++ 8 files changed, 327 insertions(+) create mode 100644 .gitignore create mode 100644 .ppm_package create mode 100644 Makefile create mode 100644 src/acm2/Exceptions/ConfigurationFolderNotFoundException.php create mode 100644 src/acm2/Exceptions/ConfigurationNotDefinedException.php create mode 100644 src/acm2/Objects/Schema.php create mode 100644 src/acm2/acm2.php create mode 100644 src/acm2/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f392550 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +*.ppm \ No newline at end of file diff --git a/.ppm_package b/.ppm_package new file mode 100644 index 0000000..a9122be --- /dev/null +++ b/.ppm_package @@ -0,0 +1 @@ +src/acm2 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7326093 --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/src/acm2/Exceptions/ConfigurationFolderNotFoundException.php b/src/acm2/Exceptions/ConfigurationFolderNotFoundException.php new file mode 100644 index 0000000..f3684fa --- /dev/null +++ b/src/acm2/Exceptions/ConfigurationFolderNotFoundException.php @@ -0,0 +1,29 @@ +message = $message; + $this->code = $code; + $this->previous = $previous; + } + } \ No newline at end of file diff --git a/src/acm2/Exceptions/ConfigurationNotDefinedException.php b/src/acm2/Exceptions/ConfigurationNotDefinedException.php new file mode 100644 index 0000000..3a9a492 --- /dev/null +++ b/src/acm2/Exceptions/ConfigurationNotDefinedException.php @@ -0,0 +1,30 @@ +message = $message; + $this->code = $code; + $this->previous = $previous; + } + } \ No newline at end of file diff --git a/src/acm2/Objects/Schema.php b/src/acm2/Objects/Schema.php new file mode 100644 index 0000000..9353092 --- /dev/null +++ b/src/acm2/Objects/Schema.php @@ -0,0 +1,68 @@ +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; + } + } \ No newline at end of file diff --git a/src/acm2/acm2.php b/src/acm2/acm2.php new file mode 100644 index 0000000..ab6fd11 --- /dev/null +++ b/src/acm2/acm2.php @@ -0,0 +1,151 @@ +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; + } + } \ No newline at end of file diff --git a/src/acm2/package.json b/src/acm2/package.json new file mode 100644 index 0000000..29b3238 --- /dev/null +++ b/src/acm2/package.json @@ -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": [] +} \ No newline at end of file