Skip to content

Commit 01e69dc

Browse files
committed
added factory
1 parent d41293b commit 01e69dc

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/Spork/Factory.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Spork, an OpenSky project.
5+
*
6+
* (c) OpenSky Project Inc
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spork;
13+
14+
use Spork\Batch\BatchJob;
15+
use Spork\Batch\Strategy\StrategyInterface;
16+
17+
class Factory
18+
{
19+
/**
20+
* Creates a new batch job instance.
21+
*
22+
* @param ProcessManager $manager The process manager
23+
* @param null $data Data for the batch job
24+
* @param StrategyInterface $strategy The strategy
25+
*
26+
* @return BatchJob A new batch job instance
27+
*/
28+
public function createBatchJob(ProcessManager $manager, $data = null, StrategyInterface $strategy = null)
29+
{
30+
return new BatchJob($manager, $data, $strategy);
31+
}
32+
33+
/**
34+
* Creates a new shared memory instance.
35+
*
36+
* @param integer $pid The child process id or null if this is the child
37+
* @param integer $signal The signal to send after writing to shared memory
38+
*
39+
* @return SharedMemory A new shared memory instance
40+
*/
41+
public function createSharedMemory($pid = null, $signal = null)
42+
{
43+
return new SharedMemory($pid, $signal);
44+
}
45+
46+
/**
47+
* Creates a new fork instance.
48+
*
49+
* @param int $pid Process id
50+
* @param SharedMemory $shm Shared memory
51+
* @param bool $debug Debug mode
52+
*
53+
* @return Fork A new fork instance
54+
*/
55+
public function createFork($pid, SharedMemory $shm, $debug = false)
56+
{
57+
return new Fork($pid, $shm, $debug);
58+
}
59+
}

src/Spork/ProcessManager.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Spork;
1313

14-
use Spork\Batch\BatchJob;
1514
use Spork\Batch\Strategy\StrategyInterface;
1615
use Spork\EventDispatcher\EventDispatcher;
1716
use Spork\EventDispatcher\EventDispatcherInterface;
@@ -24,16 +23,18 @@
2423
class ProcessManager
2524
{
2625
private $dispatcher;
26+
private $factory;
2727
private $debug;
2828
private $zombieOkay;
2929
private $signal;
3030

3131
/** @var Fork[] */
3232
private $forks;
3333

34-
public function __construct(EventDispatcherInterface $dispatcher = null, $debug = false)
34+
public function __construct(EventDispatcherInterface $dispatcher = null, Factory $factory = null, $debug = false)
3535
{
3636
$this->dispatcher = $dispatcher ?: new EventDispatcher();
37+
$this->factory = $factory ?: new Factory();
3738
$this->debug = $debug;
3839
$this->zombieOkay = false;
3940
$this->forks = array();
@@ -72,7 +73,7 @@ public function zombieOkay($zombieOkay = true)
7273

7374
public function createBatchJob($data = null, StrategyInterface $strategy = null)
7475
{
75-
return new BatchJob($this, $data, $strategy);
76+
return $this->factory->createBatchJob($this, $data, $strategy);
7677
}
7778

7879
public function process($data, $callable, StrategyInterface $strategy = null)
@@ -101,7 +102,7 @@ public function fork($callable)
101102
$this->forks = array();
102103

103104
// setup the shared memory
104-
$shm = new SharedMemory(null, $this->signal);
105+
$shm = $this->factory->createSharedMemory(null, $this->signal);
105106
$message = new ExitMessage();
106107

107108
// phone home on shutdown
@@ -146,9 +147,9 @@ public function fork($callable)
146147
}
147148

148149
// connect to shared memory
149-
$shm = new SharedMemory($pid);
150+
$shm = $this->factory->createSharedMemory($pid);
150151

151-
return $this->forks[$pid] = new Fork($pid, $shm, $this->debug);
152+
return $this->forks[$pid] = $this->factory->createFork($pid, $shm, $this->debug);
152153
}
153154

154155
public function monitor($signal = SIGUSR1)

0 commit comments

Comments
 (0)