diff --git a/src/ncc/Managers/RemoteSourcesManager.php b/src/ncc/Managers/RemoteSourcesManager.php new file mode 100644 index 0000000..95f156b --- /dev/null +++ b/src/ncc/Managers/RemoteSourcesManager.php @@ -0,0 +1,152 @@ +DefinedSourcesPath = PathFinder::getRemouteSources(Scopes::System); + + $this->load(); + } + + /** + * Loads an existing remote sources file, or creates a new one if it doesn't exist + * + * @return void + */ + public function load(): void + { + $this->Sources = []; + + try + { + + if(file_exists($this->DefinedSourcesPath)) + { + $sources = ZiProto::decode(IO::fread($this->DefinedSourcesPath)); + $this->Sources = []; + foreach($sources as $source) + $this->Sources[] = DefinedRemoteSource::fromArray($source); + } + } + catch(Exception $e) + { + unset($e); + } + } + + /** + * Saves the remote sources file to disk + * + * @return void + * @throws IOException + */ + public function save(): void + { + $sources = []; + foreach($this->Sources as $source) + $sources[] = $source->toArray(true); + + IO::fwrite($this->DefinedSourcesPath, ZiProto::encode($sources)); + } + + /** + * Adds a new remote source to the list + * + * @param DefinedRemoteSource $source + * @return bool + */ + public function addRemoteSource(DefinedRemoteSource $source): bool + { + foreach($this->Sources as $existingSource) + { + if($existingSource->Name === $source->Name) + return false; + } + + $this->Sources[] = $source; + return true; + } + + /** + * Gets a remote source by its name + * + * @param string $name + * @return DefinedRemoteSource|null + */ + public function getRemoteSource(string $name): ?DefinedRemoteSource + { + foreach($this->Sources as $source) + { + if($source->Name === $name) + return $source; + } + + return null; + } + + /** + * Deletes an existing remote source + * + * @param string $name + * @return bool + */ + public function deleteRemoteSource(string $name): bool + { + foreach($this->Sources as $index => $source) + { + if($source->Name === $name) + { + unset($this->Sources[$index]); + return true; + } + } + + return false; + } + + /** + * Returns an array of all the defined remote sources + * + * @return DefinedRemoteSource[] + */ + public function getSources(): array + { + if($this->Sources == null) + $this->load(); + return $this->Sources; + } + } \ No newline at end of file