PythonRunner::run()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Rakshitbharat\Pythoninphp;
4
5
class PythonRunner
6
{
7
    public $file_path = '';
8
9
    public $out_put = '';
10
11
    public function pythonGrabber($pythonFilePathLaravelFormat)
12
    {
13
        $this->file_path = base_path() . DIRECTORY_SEPARATOR . $pythonFilePathLaravelFormat;
14
    }
15
16
    public function run()
17
    {
18
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
19
            $pythonPath = exec("where python");
20
        } else {
21
            $pythonPath = exec("which python");
22
        }
23
        $command = $pythonPath . " " . $this->file_path . " 2>&1";
24
        $pid = popen($command, "r");
25
        while (!feof($pid)) {
26
            $this->out_put .= fread($pid, 256);
27
            flush();
28
            ob_flush();
29
            usleep(100000);
30
        }
31
        pclose($pid);
32
        return $this->out_put;
33
    }
34
}
35