for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace nebula\route\collection;
use Iterator;
use IteratorAggregate;
/**
* 路由集合文件
*/
class CollectionFile implements IteratorAggregate
{
* 数据源
*
* @var string
protected $path;
public static function fromFile(string $path)
$collection = new static;
$collection->path = $path;
return $collection;
}
* 保存到文件
* @param string $path
* @return boolean
public function save(string $path):bool
foreach ($this as $name => $value) {
if (\file_put_contents($path, $name .'|'. \serialize($value), FILE_APPEND) === false) {
return false;
return true;
* 获取迭代器
* @return Iterator
public function getIterator():Iterator
foreach (new ReadLineIterator($this->path) as $line) {
$data = \explode('|', $line);
yield $data[0] => \unserialize($data[1]);