-
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.
* reimplemented DataStream to DataPipeline
- Loading branch information
Showing
14 changed files
with
203 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\DataPipeline; | ||
|
||
/** | ||
* Represents place where data can be write like file, database or socket | ||
*/ | ||
abstract class DataSink implements Filter { | ||
|
||
public function pipe(Filter $output) { | ||
// noop for sink | ||
} | ||
|
||
public function unpipe(Filter $output = null) { | ||
// noop for sink | ||
} | ||
|
||
/** | ||
* @param $data | ||
*/ | ||
public abstract function write($data); | ||
|
||
public function onPipe(DataSource $source) { | ||
|
||
} | ||
|
||
public function onUnpipe(DataSource $source) { | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\DataPipeline; | ||
|
||
/** | ||
* Represents place where data come from like file, database or socket | ||
*/ | ||
interface DataSource { | ||
|
||
/** | ||
* @param Filter $output | ||
* @return Filter | ||
*/ | ||
public function pipe(Filter $output); | ||
|
||
/** | ||
* @param Filter|null $output | ||
* @return Filter | ||
*/ | ||
public function unpipe(Filter $output = null); | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\DataPipeline; | ||
|
||
/** | ||
* Transfrom, process, filter data and can output it to next Filter. | ||
*/ | ||
interface Filter extends DataSource { | ||
|
||
/** | ||
* @param $data | ||
*/ | ||
public function write($data); | ||
|
||
/** | ||
* @param DataSource $source | ||
* @return | ||
*/ | ||
public function onPipe(DataSource $source); | ||
|
||
public function onUnpipe(DataSource $source); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\DataPipeline; | ||
|
||
|
||
class Filters { | ||
|
||
public static function transform(callable $transfromer) { | ||
return new Transformer($transfromer); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\DataPipeline; | ||
|
||
/** | ||
* Represents linux dev/null All written data will be lost. | ||
*/ | ||
class NullSink extends DataSink { | ||
|
||
public function write($data) { | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\DataPipeline; | ||
|
||
class Transformer implements Filter { | ||
|
||
protected $transformer; | ||
protected $output; | ||
|
||
public function __construct(callable $transfromer) { | ||
$this->transformer = $transfromer; | ||
$this->output = new NullSink(); | ||
} | ||
|
||
public function write($data) { | ||
$transfromer = $this->transformer; | ||
$this->output->write($transfromer($data)); | ||
} | ||
|
||
public function pipe(Filter $output) { | ||
$this->unpipe(); | ||
$this->output = $output; | ||
$this->output->onPipe($this); | ||
return $this->output; | ||
} | ||
|
||
public function unpipe(Filter $output = null) { | ||
$this->output->onUnpipe($this); | ||
$this->output = new NullSink(); | ||
} | ||
|
||
public function onPipe(DataSource $source) { | ||
|
||
} | ||
|
||
public function onUnpipe(DataSource $source) { | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
46 changes: 46 additions & 0 deletions
46
src/SAREhub/Commons/Protobuf/FileProtobufMessageSource.php
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\Protobuf; | ||
|
||
use Protobuf\Stream; | ||
use SAREhub\Commons\DataPipeline\DataSource; | ||
use SAREhub\Commons\DataPipeline\Filter; | ||
use SAREhub\Commons\DataPipeline\NullSink; | ||
|
||
class FileProtobufMessageSource implements DataSource { | ||
|
||
/** @var \SplFileObject */ | ||
protected $file; | ||
|
||
protected $sizeInfoBytes; | ||
|
||
/** @var string */ | ||
protected $sizeInfoPackFormat; | ||
|
||
/** @var Filter */ | ||
protected $output; | ||
|
||
public function __construct(\SplFileObject $file, ProtobufMessagesFileHeader $header) { | ||
$this->file = $file; | ||
$this->sizeInfoPackFormat = $header->getMessageSizeInfoPackFormat(); | ||
$this->sizeInfoBytes = $header->getMessageSizeInfoBytes(); | ||
$this->output = new NullSink(); | ||
} | ||
|
||
public function flow() { | ||
$size = unpack($this->sizeInfoPackFormat, $this->file->fread($this->sizeInfoBytes))[1]; | ||
$data = $this->file->fread($size); | ||
$this->output->write(Stream::fromString($data, $size)); | ||
} | ||
|
||
public function pipe(Filter $output) { | ||
$this->unpipe(); | ||
$this->output = $output; | ||
$this->output->onPipe($this); | ||
} | ||
|
||
public function unpipe(Filter $output = null) { | ||
$this->output->onUnpipe($this); | ||
$this->output = new NullSink(); | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
src/SAREhub/Commons/Protobuf/FileProtobufMessageStreamSource.php
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.