Skip to content

Commit 21515bd

Browse files
author
Marc Aschmann
committed
Avoid having pre-instantiated objects with connected/attached ports/sockets on second run
1 parent 633d8c2 commit 21515bd

5 files changed

Lines changed: 160 additions & 47 deletions

File tree

Flow/AbstractNetworkBuilder.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/*
3+
* This file is part of the phpflo\phpflo-bundle package.
4+
*
5+
* (c) Marc Aschmann <maschmann@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PhpFlo\PhpFloBundle\Flow;
11+
12+
use PhpFlo\Graph;
13+
use PhpFlo\Network;
14+
15+
/**
16+
* Class AbstractNetworkBuilder
17+
*
18+
* @package PhpFlo\PhpFloBundle\Flow
19+
* @author Marc Aschmann <maschmann@gmail.com>
20+
*/
21+
abstract class AbstractNetworkBuilder
22+
{
23+
/**
24+
* @param string $fileName
25+
* @return Network
26+
*/
27+
public function fromFile($fileName)
28+
{
29+
$fileUri = $this->root . '/' . ltrim($fileName, '/');
30+
31+
if (file_exists($fileUri)) {
32+
$graph = file_get_contents($fileUri);
33+
} else {
34+
throw new \InvalidArgumentException('Could not find file ' . $fileUri);
35+
}
36+
37+
return $this->fromString($graph);
38+
}
39+
40+
/**
41+
* @param string $graph
42+
* @return Network
43+
*/
44+
public function fromString($graph)
45+
{
46+
$graph = Graph::loadString($graph);
47+
48+
return $this->fromGraph($graph);
49+
}
50+
51+
/**
52+
* @param Graph $graph
53+
* @return Network
54+
*/
55+
public function fromGraph(Graph $graph)
56+
{
57+
return Network::create($graph, $this->builder);
58+
}
59+
}

Flow/DiNetworkBuilder.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-bundle package.
4+
*
5+
* (c) Marc Aschmann <maschmann@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PhpFlo\PhpFloBundle\Flow;
11+
12+
use PhpFlo\Builder\ComponentDiFinder;
13+
use PhpFlo\PhpFloBundle\Common\BuilderInterface;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
16+
17+
/**
18+
* Class DiNetworkBuilder
19+
*
20+
* @package PhpFlo\PhpFloBundle\Flow
21+
* @author Marc Aschmann <maschmann@gmail.com>
22+
*/
23+
class DiNetworkBuilder extends AbstractNetworkBuilder implements BuilderInterface
24+
{
25+
/**
26+
* @var string
27+
*/
28+
protected $root;
29+
30+
/**
31+
* @var ComponentBuilderInterface
32+
*/
33+
protected $builder;
34+
35+
/**
36+
* Builder constructor.
37+
*
38+
* @param ComponentRegistryInterface $registry
39+
* @param string $rootDir
40+
*/
41+
public function __construct(ContainerInterface $container, $rootDir)
42+
{
43+
$this->builder = new ComponentDiFinder($container);
44+
$this->root = $rootDir . '/../app/config';
45+
}
46+
}

Flow/NetworkBuilder.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
* @package PhpFlo\PhpFloBundle\Flow
2424
* @author Marc Aschmann <maschmann@gmail.com>
2525
*/
26-
class NetworkBuilder implements BuilderInterface
26+
class NetworkBuilder extends AbstractNetworkBuilder implements BuilderInterface
2727
{
2828
/**
2929
* @var string
3030
*/
31-
private $root;
31+
protected $root;
3232

3333
/**
3434
* @var ComponentBuilderInterface
3535
*/
36-
private $builder;
36+
protected $builder;
3737

3838
/**
3939
* Builder constructor.
@@ -46,44 +46,4 @@ public function __construct(ComponentRegistryInterface $registry, $rootDir)
4646
$this->builder = new ComponentDiFinder($registry);
4747
$this->root = $rootDir . '/../app/config';
4848
}
49-
50-
/**
51-
* @Todo maybe find a cached solution for that?
52-
*
53-
* @param string $fileName
54-
* @return Network
55-
* @throws \InvalidArgumentException
56-
*/
57-
public function fromFile($fileName)
58-
{
59-
$fileUri = $this->root . '/' . ltrim($fileName, '/');
60-
61-
if (file_exists($fileUri)) {
62-
$graph = file_get_contents($fileUri);
63-
} else {
64-
throw new \InvalidArgumentException('Could not find file ' . $fileUri);
65-
}
66-
67-
return $this->fromString($graph);
68-
}
69-
70-
/**
71-
* @param string $graph
72-
* @return Network
73-
*/
74-
public function fromString($graph)
75-
{
76-
$graph = Graph::loadString($graph);
77-
78-
return $this->fromGraph($graph);
79-
}
80-
81-
/**
82-
* @param Graph $graph
83-
* @return Network
84-
*/
85-
public function fromGraph(Graph $graph)
86-
{
87-
return Network::create($graph, $this->builder);
88-
}
8949
}

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ Configuration
3131
];
3232
// ...
3333
```
34-
After adding the bundle, you have a new factory service **phpflo** which uses a registry of all your accordingly tagged services.
34+
After adding the bundle, you have a new factory service **phpflo.network** which uses a registry of all your accordingly tagged services.
35+
Another possibility is to use **phpflo.network_di** service.
36+
The difference between these services is: network uses a registry and components are registered **only once** and collected during compile time. This is useful when using the network within on request cycle and build it only once.
37+
If you are working e.g. with a long running process, you should us the network_di where every component will be fetched as a fresh object from DIC for every network build.
3538
You can add components using following steps:
3639

3740
**Component class**
@@ -61,9 +64,9 @@ class ReadFile extends Component
6164
}
6265
}
6366
```
64-
Keep in mind to exend the PhpFlo\Component or implement PhpFlo\ComponentInterface!
67+
Keep in mind to extend the PhpFlo\Component or implement PhpFlo\ComponentInterface!
6568

66-
**service definition**
69+
**service definition (phpflo.network)**
6770
```yaml
6871
# app/config/services.yml
6972

@@ -77,6 +80,21 @@ services:
7780
```
7881
You can name the service whatever you want: The only two important things are the tags. It needs name "phpflo.component" to be found in compiler pass and the alias will be used as component name for the graph file.
7982

83+
**service definition (phpflo.network_di)**
84+
85+
```yaml
86+
# app/config/services.yml
87+
88+
services:
89+
app.read_file:
90+
lazy: true
91+
shared: false # force instantiation
92+
class: AppBundle\Component\ReadFile
93+
tags: # optional
94+
- {name: phpflo.component, alias: read_file}
95+
96+
```
97+
8098
**graph file** (app/config/my_graph.json)
8199
```json
82100
{
@@ -143,6 +161,29 @@ You can name the service whatever you want: The only two important things are th
143161
```
144162
The builder will automatically search for the provided graph file in '''app/config''' - if you need subdirectories, you can provide the filename in the formate '''subdir/graph.json'''
145163

164+
**processes definition (network_di)**
165+
```json
166+
{
167+
...
168+
"processes": {
169+
"ReadFile": {
170+
"component": "app.read_file"
171+
},
172+
"SplitbyLines": {
173+
"component": "split_str"
174+
},
175+
"CountLines": {
176+
"component": "counter"
177+
},
178+
"Display": {
179+
"component": "output"
180+
}
181+
},
182+
...
183+
}
184+
```
185+
Add the service name as component name!
186+
146187
Example
147188
----
148189
Within a symfony controller with access to the DIC, you can simply create a graph like this:

Resources/config/services.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
parameters:
22

33
services:
4-
phpflo:
4+
phpflo.network_di:
5+
class: PhpFlo\PhpFloBundle\Flow\DiNetworkBuilder
6+
lazy: true
7+
arguments:
8+
- '@service_container' # not nice, but necessary
9+
- '%kernel.root_dir%'
10+
11+
phpflo.network:
512
class: PhpFlo\PhpFloBundle\Flow\NetworkBuilder
613
lazy: true
714
arguments:

0 commit comments

Comments
 (0)