Skip to content

Commit 05477f7

Browse files
author
Marc Aschmann
committed
Intial commit
0 parents  commit 05477f7

File tree

10 files changed

+367
-0
lines changed

10 files changed

+367
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Marc Aschmann
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Whitespace-only changes.

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "phpflo/phpflo-core",
3+
"description": "Collection of base components for usage with phpflo",
4+
"type": "library",
5+
"keywords": ["fbp", "flow"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Henri Bergius",
10+
"email": "henri.bergius@iki.fi",
11+
"homepage": "http://bergie.iki.fi/"
12+
},
13+
{
14+
"name": "Marc Aschmann",
15+
"email": "maschmann@gmail.com"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.4.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "<5.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"": "lib/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": { "Tests\\": "tests/" }
31+
}
32+
}

lib/PhpFlo/Component/Counter.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/phpflo package.
4+
*
5+
* (c) Henri Bergius <henri.bergius@iki.fi>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace PhpFlo\Component;
12+
13+
use PhpFlo\Component;
14+
15+
/**
16+
* Class Counter
17+
*
18+
* @package PhpFlo\Component
19+
* @author Henri Bergius <henri.bergius@iki.fi>
20+
*/
21+
class Counter extends Component
22+
{
23+
/**
24+
* @var null
25+
*/
26+
private $count;
27+
28+
public function __construct()
29+
{
30+
$this->inPorts()->add('in', ['datatype' => 'bang']);
31+
$this->outPorts()->add('count', ['datatype' => 'int']);
32+
33+
$this->inPorts()->in->on('data', [$this, 'appendCount']);
34+
$this->inPorts()->in->on('disconnect', [$this, 'sendCount']);
35+
36+
$this->count = null;
37+
}
38+
39+
/**
40+
* @param int|null $data
41+
*/
42+
public function appendCount($data)
43+
{
44+
if (is_null($this->count)) {
45+
$this->count = 0;
46+
}
47+
$this->count++;
48+
}
49+
50+
public function sendCount()
51+
{
52+
$this->outPorts()->count->send($this->count);
53+
$this->outPorts()->count->disconnect();
54+
$this->count = null;
55+
}
56+
}

lib/PhpFlo/Component/Output.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/phpflo package.
4+
*
5+
* (c) Henri Bergius <henri.bergius@iki.fi>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace PhpFlo\Component;
12+
13+
use PhpFlo\Component;
14+
15+
/**
16+
* Class Output
17+
*
18+
* @package PhpFlo\Component
19+
* @author Henri Bergius <henri.bergius@iki.fi>
20+
*/
21+
class Output extends Component
22+
{
23+
public function __construct()
24+
{
25+
$this->inPorts()->add('in', ['datatype' => 'all', 'addressable' => true]);
26+
$this->inPorts()->in->on('data', [$this, 'displayData']);
27+
}
28+
29+
/**
30+
* @param mixed $data
31+
*/
32+
public function displayData($data)
33+
{
34+
echo "{$data}\n";
35+
}
36+
}

lib/PhpFlo/Component/Queue.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/phpflo package.
4+
*
5+
* (c) Henri Bergius <henri.bergius@iki.fi>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace PhpFlo\Component;
12+
13+
use PhpFlo\Component;
14+
15+
/**
16+
* Component for creating a simple queue of messages.
17+
*
18+
* All incomming messages are kept in memory. As soon as the queue reaches the
19+
* pre-configured size of 100 messages, the current list of messages is send.
20+
*
21+
* You can reconfigure the queue size of the component by sending a message to
22+
* the `size` input port of this component. If the incomming data is not a
23+
* positive integer, an error message will be send to the `err` out port of this
24+
* component.
25+
*
26+
* @author Marijn Huizendveld <marijn@pink-tie.com>
27+
*/
28+
class Queue extends Component
29+
{
30+
/**
31+
* @var integer
32+
*/
33+
private $size;
34+
35+
/**
36+
* @var array<mixed>
37+
*/
38+
private $messages;
39+
40+
public function __construct()
41+
{
42+
$this->size = 100;
43+
$this->messages = [];
44+
45+
$this->inPorts()->add('in', ['datatype' => 'all']);
46+
$this->inPorts()->add('size', ['datatype' => 'all']);
47+
48+
$this->outPorts()->add('error', ['datatype' => 'all']);
49+
$this->outPorts()->add('messages', ['datatype' => 'all']);
50+
51+
$this->inPorts()->in->on('data', [$this, 'onAppendQueue']);
52+
$this->inPorts()->in->on('detach', [$this, 'onStreamEnded']);
53+
54+
$this->inPorts()->size->on('data', [$this, 'onResize']);
55+
}
56+
57+
/**
58+
* @param mixed $data
59+
*/
60+
public function onAppendQueue($data)
61+
{
62+
$this->messages[] = $data;
63+
64+
$this->sendQueue();
65+
}
66+
67+
public function onStreamEnded()
68+
{
69+
$this->flushQueue();
70+
}
71+
72+
/**
73+
* @param mixed $data
74+
*/
75+
public function onResize($data)
76+
{
77+
if (!is_int($data) || 0 > $data) {
78+
$dumped = var_dump($data);
79+
80+
$this->outPorts()->error->send(
81+
"Invalid queue size: '{$dumped}'. Queue resize operation expects a positive integer value."
82+
);
83+
}
84+
85+
$this->size = $data;
86+
87+
$this->sendQueue();
88+
}
89+
90+
private function sendQueue()
91+
{
92+
if ($this->size <= count($this->messages)) {
93+
$this->flushQueue();
94+
}
95+
}
96+
97+
private function flushQueue()
98+
{
99+
$this->outPorts()->messages->send($this->messages);
100+
$this->outPorts()->messages->disconnect();
101+
102+
$this->messages = [];
103+
}
104+
}

lib/PhpFlo/Component/ReadFile.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/phpflo package.
4+
*
5+
* (c) Henri Bergius <henri.bergius@iki.fi>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace PhpFlo\Component;
12+
13+
use PhpFlo\Component;
14+
15+
/**
16+
* Class ReadFile
17+
*
18+
* @package PhpFlo\Component
19+
* @author Henri Bergius <henri.bergius@iki.fi>
20+
*/
21+
class ReadFile extends Component
22+
{
23+
public function __construct()
24+
{
25+
$this->inPorts()->add('source', ['datatype' => 'string']);
26+
$this->outPorts()->add('out', ['datatype' => 'string']);
27+
$this->outPorts()->add('error', []); // use defaults, datatype = all
28+
29+
$this->inPorts()->source->on('data', [$this, 'readFile']);
30+
}
31+
32+
/**
33+
* @param string $data
34+
*/
35+
public function readFile($data)
36+
{
37+
if (!file_exists($data)) {
38+
$this->outPorts()->error->send("File {$data} doesn't exist");
39+
40+
return;
41+
}
42+
43+
$this->outPorts()->out->send(file_get_contents($data));
44+
$this->outPorts()->out->disconnect();
45+
}
46+
}

lib/PhpFlo/Component/SplitStr.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo/phpflo package.
4+
*
5+
* (c) Henri Bergius <henri.bergius@iki.fi>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace PhpFlo\Component;
12+
13+
use PhpFlo\Component;
14+
15+
/**
16+
* Class SplitStr
17+
*
18+
* @package PhpFlo\Component
19+
* @author Henri Bergius <henri.bergius@iki.fi>
20+
*/
21+
class SplitStr extends Component
22+
{
23+
/**
24+
* @var string
25+
*/
26+
private $delimiterString;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $string;
32+
33+
public function __construct()
34+
{
35+
$this->inPorts()->add('in', ['datatype' => 'string']);
36+
$this->inPorts()->add('delimiter', ['datatype' => 'string']);
37+
$this->outPorts()->add('out', ['datatype' => 'string']);
38+
39+
$this->inPorts()->delimiter->on('data', [$this, 'setDelimiter']);
40+
$this->inPorts()->in->on('data', [$this, 'appendString']);
41+
$this->inPorts()->in->on('disconnect', [$this, 'splitString']);
42+
43+
$this->delimiterString = "\n";
44+
$this->string = "";
45+
}
46+
47+
/**
48+
* @param string $data
49+
*/
50+
public function setDelimiter($data)
51+
{
52+
$this->delimiterString = $data;
53+
}
54+
55+
/**
56+
* @param string $data
57+
*/
58+
public function appendString($data)
59+
{
60+
$this->string .= $data;
61+
}
62+
63+
public function splitString()
64+
{
65+
$parts = explode($this->delimiterString, $this->string);
66+
foreach ($parts as $part) {
67+
$this->outPorts()->out->send($part);
68+
}
69+
$this->outPorts()->out->disconnect();
70+
$this->string = "";
71+
}
72+
}

tests/PhpFlo/Component/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)