forked from overtrue/pinyin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
更新生成器类型加载器,减少重复代码,优化代码结构,更新文档。 (overtrue#78)
* 更新生成器类型加载器,减少重复代码,优化代码结构,更新文档。 * 删除没必要存在的变量~
- Loading branch information
Showing
2 changed files
with
42 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use Closure; | ||
use Exception; | ||
use SplFileObject; | ||
use Generator; | ||
|
||
/** | ||
* Generator syntax(yield) Dict File loader. | ||
|
@@ -36,7 +37,14 @@ class GeneratorFileDictLoader implements DictLoaderInterface | |
* | ||
* @var array | ||
*/ | ||
protected $handles = []; | ||
protected static $handles = []; | ||
|
||
/** | ||
* surnames. | ||
* | ||
* @var SplFileObject | ||
*/ | ||
protected static $surnamesHandle; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -51,7 +59,7 @@ public function __construct($path) | |
$segment = $this->path.'/'.sprintf($this->segmentName, $i); | ||
|
||
if (file_exists($segment) && is_file($segment)) { | ||
array_push($this->handles, $this->openFile($segment)); | ||
array_push(static::$handles, $this->openFile($segment)); | ||
} | ||
} | ||
} | ||
|
@@ -91,28 +99,40 @@ protected function getGenerator(array $handles) | |
} | ||
|
||
/** | ||
* Load dict. | ||
* Traverse the stream. | ||
* | ||
* @param Generator $generator | ||
* @param Closure $callback | ||
* @author Seven Du <[email protected]> | ||
*/ | ||
public function map(Closure $callback) | ||
protected function traversing(Generator $generator, Closure $callback) | ||
{ | ||
foreach ($this->getGenerator($this->handles) as $string => $pinyin) { | ||
foreach ($generator as $string => $pinyin) { | ||
$callback([$string => $pinyin]); | ||
} | ||
} | ||
|
||
/** | ||
* Load dict. | ||
* | ||
* @param Closure $callback | ||
*/ | ||
public function map(Closure $callback) | ||
{ | ||
$this->traversing($this->getGenerator(static::$handles), $callback); | ||
} | ||
|
||
/** | ||
* Load surname dict. | ||
* | ||
* @param Closure $callback | ||
*/ | ||
public function mapSurname(Closure $callback) | ||
{ | ||
$surnames = $this->path.'/surnames'; | ||
|
||
foreach ($this->getGenerator([ $this->openFile($surnames) ]) as $string => $pinyin) { | ||
$callback([$string => $pinyin]); | ||
if (!static::$surnamesHandle instanceof SplFileObject) { | ||
static::$surnamesHandle = $this->openFile($this->path.'/surnames'); | ||
} | ||
|
||
$this->traversing($this->getGenerator([static::$surnamesHandle]), $callback); | ||
} | ||
} |