Skip to content

Commit

Permalink
update some ini parser logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 8, 2021
1 parent 720c68d commit c9c70d6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 31 deletions.
6 changes: 4 additions & 2 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Stream\StringsStream;
use InvalidArgumentException;
use Throwable;
use Toolkit\FsUtil\File;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;
Expand All @@ -32,7 +33,6 @@
use function strlen;
use function substr;
use function trim;
use function vdump;

/**
* Class StringController
Expand Down Expand Up @@ -76,7 +76,7 @@ protected function init(): void
}

/**
* @throws JsonException
* @throws Throwable
*/
private function loadDumpContents(): void
{
Expand All @@ -96,6 +96,8 @@ private function loadDumpContents(): void
*
* @param FlagsParser $fs
* @param Output $output
*
* @throws Throwable
*/
public function loadCommand(FlagsParser $fs, Output $output): void
{
Expand Down
43 changes: 25 additions & 18 deletions app/Lib/Parser/IniParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
class IniParser
{
// public bool $parseBool = false;

/**
* current parsed section name.
*
Expand Down Expand Up @@ -99,6 +97,7 @@ public static function decode(string $str): array
*/
public static function encode(array $data): string
{
// TODO
return '';
}

Expand Down Expand Up @@ -178,6 +177,7 @@ public function parse(): array
$key = rtrim($tmp[0]);
$val = ltrim($tmp[1]);

// empty value
if ($val === '') {
$this->collectValue($key, $val);
continue;
Expand Down Expand Up @@ -227,40 +227,47 @@ protected function collectValue(string $key, mixed $val): void
}

// in section. eg: [arrayName] -> $sectionName='arrayName'
$sectionName = $this->sectionName;
$section = $this->sectionName;

// is list array value. eg `[] = "arr_elem_one"`
if ($key === '[]') {
if (!isset($this->data[$section]) || !is_array($this->data[$section])) {
$this->data[$section] = [];
}

// is array sub key.
$this->data[$section][] = $val;
return;
}

// is map array sub key.
// eg:
// [] = "arr_elem_one"
// val_arr[] = "arr_elem_one"
// val_arr_two[some_key] = "some_key_value"
// val_arr_two[sub_key] = "some_key_value"
// [sub_key] = "some_key_value"
$ok = preg_match("/[\w-]{0,64}\[(.*?)]$/", $key, $matches);
if ($ok === 1 && isset($matches[0])) {
[$arrName, $subKey] = explode('[', trim($key, ']'));

if ($arrName !== '') {
if (!isset($this->data[$sectionName][$arrName]) || !is_array($this->data[$sectionName][$arrName])) {
$this->data[$sectionName][$arrName] = [];
if (!isset($this->data[$section][$arrName]) || !is_array($this->data[$section][$arrName])) {
$this->data[$section][$arrName] = [];
}

if ($subKey !== '') { // eg: val_arr[subKey] = "arr_elem_one"
$this->data[$sectionName][$arrName][$subKey] = $val;
$this->data[$section][$arrName][$subKey] = $val;
} else { // eg: val_arr[] = "arr_elem_one"
$this->data[$sectionName][$arrName][] = $val;
$this->data[$section][$arrName][] = $val;
}
} else {
if (!isset($this->data[$sectionName]) || !is_array($this->data[$sectionName])) {
$this->data[$sectionName] = [];
if (!isset($this->data[$section]) || !is_array($this->data[$section])) {
$this->data[$section] = [];
}

if ($subKey !== '') { // eg: [subKey] = "arr_elem_one"
$this->data[$sectionName][$subKey] = $val;
} else { // eg: [] = "arr_elem_one"
$this->data[$sectionName][] = $val;
}
// eg: [sub_key] = "arr_elem_one"
$this->data[$section][$subKey] = $val;
}
} else {
$this->data[$sectionName][$key] = $val;
$this->data[$section][$key] = $val;
}
}

Expand Down
22 changes: 22 additions & 0 deletions app/Lib/Template/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,25 @@ public function configThis(callable $fn): self
return $this;
}

/**
* @param string $tplFile
* @param array $tplVars
*/
public function render(string $tplFile, array $tplVars = []): void
{
$tplFile = $this->findTplFile($tplFile);

echo $this->renderFile($tplFile, $tplVars);
}

/**
* @param string $tplName
*
* @return string
*/
protected function findTplFile(string $tplName): string
{
$tplName = $this->resolvePath($tplName);
if (is_file($tplName)) {
return $tplName;
}
Expand Down Expand Up @@ -183,6 +195,16 @@ public function getAllowExt(): array
return $this->allowExt;
}

/**
* @param string[] $allowExt
*/
public function addAllowExt(array $allowExt): void
{
foreach ($allowExt as $ext) {
$this->allowExt[] = $ext;
}
}

/**
* @param string[] $allowExt
*/
Expand Down
14 changes: 14 additions & 0 deletions app/Lib/Template/Contract/TemplateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
interface TemplateInterface
{
/**
* Render template file and output
*
* @param string $tplFile
* @param array $tplVars
*
* @return void
*/
public function render(string $tplFile, array $tplVars): void;

/**
* Render template file to string
*
* @param string $tplFile
* @param array $tplVars
*
Expand All @@ -19,6 +31,8 @@ interface TemplateInterface
public function renderFile(string $tplFile, array $tplVars): string;

/**
* Render template text to string
*
* @param string $tplCode
* @param array $tplVars
*
Expand Down
11 changes: 0 additions & 11 deletions app/Lib/Template/HtmlTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ class HtmlTemplate extends TextTemplate
*/
protected array $allowExt = ['.html', '.phtml', '.php'];

/**
* @param string $viewPath
* @param array $vars
*/
public function render(string $viewPath, array $vars = []): void
{
$viewFile = $this->findTplFile($viewPath);

echo $this->renderFile($viewFile, $vars);
}

/**
* @param string $viewPath
* @param array $vars
Expand Down

0 comments on commit c9c70d6

Please sign in to comment.