1<?php
2/**
3 * DocBlox
4 *
5 * PHP Version 5
6 *
7 * @category  DocBlox
8 * @package   Parallel
9 * @author    Mike van Riel <mike.vanriel@naenius.com>
10 * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
11 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12 * @link      http://docblox-project.org
13 */
14
15/**
16 * Class that represents a named pipe for a Worker.
17 *
18 * This class manages the named pipe for a worker and is able to push and pull
19 * specific data to facilitate IPC (interprocess communication).
20 *
21 * @category DocBlox
22 * @package  Parallel
23 * @author   Mike van Riel <mike.vanriel@naenius.com>
24 * @license  http://www.opensource.org/licenses/mit-license.php MIT
25 * @link     http://docblox-project.org
26 */
27class DocBlox_Parallel_WorkerPipe
28{
29    /** @var DocBlox_Parallel_Worker worker class that is associated */
30    protected $worker;
31
32    /** @var string Path to the pipe */
33    protected $path;
34
35    /**
36     * Initializes the named pipe.
37     *
38     * @param DocBlox_Parallel_Worker $worker Associated worker.
39     */
40    public function __construct(DocBlox_Parallel_Worker $worker)
41    {
42        $this->worker = $worker;
43
44        $this->path = tempnam(sys_get_temp_dir(), 'dpm_');
45        posix_mkfifo($this->path, 0750);
46    }
47
48    /**
49     * If the named pipe was not cleaned up, do so now.
50     */
51    public function __destruct()
52    {
53        if (file_exists($this->path)) {
54            $this->release();
55        }
56    }
57
58    /**
59     * Pull the worker data into the named pipe.
60     *
61     * @return void
62     */
63    public function pull()
64    {
65        $this->writePipeContents();
66    }
67
68    /**
69     * Push the worker data back onto the worker and release the pipe.
70     *
71     * @return void
72     */
73    public function push()
74    {
75        list($result, $error, $return_code) = $this->readPipeContents();
76        $this->release();
77
78        $this->worker->setResult($result);
79        $this->worker->setError($error);
80        $this->worker->setReturnCode($return_code);
81    }
82
83    /**
84     * Convenience method to show relation to readPipeContents.
85     *
86     * @return void
87     */
88    protected function writePipeContents()
89    {
90        // push the gathered data onto a name pipe
91        $pipe = fopen($this->path, 'w');
92        fwrite(
93            $pipe, serialize(
94                array(
95                    $this->worker->getResult(),
96                    $this->worker->getError(),
97                    $this->worker->getReturnCode()
98                )
99            )
100        );
101        fclose($pipe);
102    }
103
104    /**
105     * Returns the unserialized contents of the pipe.
106     *
107     * @return array
108     */
109    protected function readPipeContents()
110    {
111        $pipe = fopen($this->path, 'r+');
112        $result = unserialize(fread($pipe, filesize($this->path)));
113        fclose($pipe);
114
115        return $result;
116    }
117
118    /**
119     * Releases the pipe.
120     *
121     * @return void
122     */
123    protected function release()
124    {
125        unlink($this->path);
126    }
127}