Kelas PHP untuk bekerja dengan file INI

Kelas pembantu untuk bekerja dengan file INI. Kelas menerapkan pola desain Builder. Kelas yang disajikan adalah implementasi fungsionalitas untuk bekerja dengan file INI. Fungsionalitas dasar termasuk membuat, menambah dan membaca bagian, menambah dan menghapus kunci dan fungsionalitas lainnya.


Pertama-tama, Anda perlu mendeklarasikan variabel yang bertanggung jawab atas struktur file, jalur file, dan jenis pemindaian:

/**
 * @var array  ini 
*/
private array $structure = [];
/**
 * @var int  
*/
private int $scannerMode;
/**
 * @var string   
*/
private string $path;

Dalam konstruktor metode, tentukan jalur ke file (untuk pembuatan) dan jenis pemindaian, dan juga panggil metode untuk mendapatkan struktur file:

public function __construct(string $path, int $scannerMode = INI_SCANNER_TYPED) {
	$this->path = $path;
	file_put_contents($path, null, LOCK_EX);
	$this->scannerMode = $scannerMode;

	$this->setInitStructure();
}

Metode untuk mendapatkan struktur file terlihat seperti ini:

private function setInitStructure(): void {
	$this->structure = parse_ini_file($this->path, true, $this->scannerMode);
}

Metode tersebut parse_ini_filemenerima argumen jalur ke file, tanda untuk menerima array multidimensi dan tipe pemindaian. Kami menetapkan bendera array multidimensi secara default true, karena lebih mudah bagi kami untuk bekerja dengan array tersebut. Jenis pemindaian dapat berupa:

  • INI_SCANNER_NORMAL (default dalam PHP)

  • INI_SCANNER_RAW

INI_SCANNER_RAW ( => ) . PHP 5.6.1 INI_SCANNER_TYPED. boolean, null integer , , .  "true""on"  "yes"   TRUE"false""off""no"  "none"  FALSE"null"   NULL. , , , . , , .

:

public function getStructure(): array {
	return $this->structure;
}

- . :

public function getSection(string $section): array {
	return $this->structure[$section];
}

public function getValue(string $section, string $key) {
	return $this->getSection($section)[$key];
}

, , . , . :

public function addSection(string $section): Ini {
	if (array_key_exists($section, $this->structure)) {
		throw new Exception(" {$section}  ");
	}

	$this->structure[$section] = [];

	return $this;
}

exception, .

, :

public function addValues(string $section, array $values): Ini {
	$this->structure[$section] = array_merge($values, $this->structure[$section]);

	return $this;
}

, . :

public function setValues(string $section, array $values): Ini {
  foreach ($values as $key => $value) {
  	$this->structure[$section][$key] = $value;
  }

  return $this;
}

, , :

public function removeSection(string $section): Ini {
  unset($this->structure[$section]);

  return $this;
}

public function removeKeys(string $section, array $keys): Ini {
  foreach ($keys as $key) {
  	unset($this->structure[$section][$key]);
  }

  return $this;
}

, "".

- :

public function write(): void {
  $iniContent = null;

  foreach ($this->structure as $section => $data) {
  	$iniContent .= "[{$section}]\n";

  foreach ($data as $key => $value) {
  	$iniContent .= "{$key}={$value}\n";
  }

  $iniContent .= "\n";
  }

	file_put_contents($this->path, $iniContent, LOCK_EX);
  $this->setInitStructure();
}

Saya harap asisten file PHP INI ini bermanfaat. Jika ada saran untuk perbaikan, saya akan senang mendengarnya.




All Articles