Skip to content

Commit

Permalink
✨ feat: add global std instance, extend the loadFile method
Browse files Browse the repository at this point in the history
- load file support check exists
- add ci test on php8.3
  • Loading branch information
inhere committed Mar 15, 2024
1 parent 9cd897b commit 1580742
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.0, 8.1, 8.2]
php: [8.1, 8.2, 8.3]
os: [ubuntu-latest, macOS-latest] # windows-latest,
# include: # will not testing on php 7.2
# - os: 'ubuntu-latest'
Expand Down
8 changes: 3 additions & 5 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Collection extends \Toolkit\Stdlib\Std\Collection
*
* @return mixed
*/
public function set(string $key, mixed $value): self
public function set(string $key, mixed $value): static
{
if ($this->keyPathSep && strpos($key, $this->keyPathSep) > 0) {
Arr::setByPath($this->data, $key, $value, $this->keyPathSep);
Expand Down Expand Up @@ -126,10 +126,9 @@ public function setKeyPathSep(string $keyPathSep): void
*
* @return $this
*/
public function load(iterable $data): self
public function load(iterable $data): static
{
$this->bindData($this->data, $data);

return $this;
}

Expand All @@ -138,10 +137,9 @@ public function load(iterable $data): self
*
* @return $this
*/
public function loadData(iterable $data): self
public function loadData(iterable $data): static
{
$this->bindData($this->data, $data);

return $this;
}

Expand Down
66 changes: 56 additions & 10 deletions src/ConfigBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,60 @@ class ConfigBox extends Collection
self::FORMAT_JSON5 => JSON_PRETTY_PRINT,
];

//
// ============================== Global instance ===============================
//

/**
* @var ConfigBox|null
*/
private static ?self $std;

/**
* @return ConfigBox
*/
public static function std(): ConfigBox
{
if (!self::$std) {
self::$std = new ConfigBox();
}
return self::$std;
}

/**
* reset global instance
*/
public static function resetStd(): void
{
self::$std = null;
}

//
// ============================== Quickly create ===============================
//

/**
* @param string $filepath
* @param string $format
* @param bool $onExists
*
* @return static
*/
public static function newFromFile(string $filepath, string $format = ''): self
public static function newFromFile(string $filepath, string $format = '', bool $onExists = false): self
{
return (new static())->loadFromFile($filepath, $format);
return (new static())->loadFromFile($filepath, $format, $onExists);
}

/**
* @param string[] $filePaths
* @param string $format
* @param string $format
* @param bool $onExists
*
* @return static
*/
public static function newFromFiles(array $filePaths, string $format = ''): self
public static function newFromFiles(array $filePaths, string $format = '', bool $onExists = false): self
{
return (new static())->loadFromFiles($filePaths, $format);
return (new static())->loadFromFiles($filePaths, $format, $onExists);
}

/**
Expand Down Expand Up @@ -95,6 +129,10 @@ public static function newFromStrings(string $format, string ...$strings): self
return (new static())->loadFromStrings($format, ...$strings);
}

//
// ============================== load config data ===============================
//

/**
* @param string $format
* @param resource $stream
Expand Down Expand Up @@ -129,28 +167,32 @@ public function loadFromStrings(string $format, string ...$strings): self
/**
* @param string[] $filePaths
* @param string $format If is empty, will parse from filepath.
* @param bool $onExists Only load exists files, otherwise will load all files.
*
* @return $this
*/
public function loadFromFiles(array $filePaths, string $format = ''): self
public function loadFromFiles(array $filePaths, string $format = '', bool $onExists = false): self
{
foreach ($filePaths as $filePath) {
$this->loadFromFile($filePath, $format);
$this->loadFromFile($filePath, $format, $onExists);
}

return $this;
}

/**
* @param string $filepath
* @param string $format If is empty, will parse from filepath.
* @param bool $onExists Only load exists files, otherwise will direct read file.
*
* @return $this
*/
public function loadFromFile(string $filepath, string $format = ''): self
public function loadFromFile(string $filepath, string $format = '', bool $onExists = false): self
{
$data = ConfigUtil::readFromFile($filepath, $format);
if ($onExists && !file_exists($filepath)) {
return $this;
}

$data = ConfigUtil::readFromFile($filepath, $format);
return $this->load($data);
}

Expand Down Expand Up @@ -224,6 +266,10 @@ public function loadTomlFile(string $filepath): self
return $this->load(ConfigUtil::parseTomlFile($filepath));
}

//
// ============================== helper methods ===============================
//

/**
* Export config data to file
*
Expand Down

0 comments on commit 1580742

Please sign in to comment.