Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created March 11, 2019 21:34
Show Gist options
  • Save PJZ9n/e13613b15986dc8df2c3884a26700ab9 to your computer and use it in GitHub Desktop.
Save PJZ9n/e13613b15986dc8df2c3884a26700ab9 to your computer and use it in GitHub Desktop.
<?php
/*******************************************************************************
* Copyright © 2019 PJSoft All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
namespace PJSoft\provider;
use pocketmine\scheduler\FileWriteTask;
use pocketmine\Server;
class YamlProvider {
/** @var string File Path */
private $path;
/** @var mixed[] Data */
private $data;
/**
* YamlProvider constructor.
*
* @param string $path File Path
*/
public function __construct(string $path) {
$this->path = $path;
if (file_exists($this->path)) {
$file = file_get_contents($this->path);
$this->data = yaml_parse($file);
} else {
file_get_contents($this->path);
$this->data = array();
}
}
/**
* file save
*
* @param bool $async
*/
public function save(bool $async = true): void {
$yaml = yaml_emit($this->data);
if (!$async) {
file_put_contents($this->path, $yaml);
} else {
Server::getInstance()->getAsyncPool()->submitTask(new FileWriteTask($this->path, $this->data));
}
}
/**
* set value
*
* @param string $key
* @param mixed $value
*/
protected function set(string $key, $value): void {
$this->data[$key] = $value;
}
/**
* get value
*
* @param string $key
*
* @return mixed|null
*/
protected function get(string $key) {
if (!isset($this->data[$key])) {
return null;
}
return $this->data[$key];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment