Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ncjoes/office-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
ncjoes committed Mar 2, 2019
2 parents 7d7cbb8 + 869e456 commit a35f168
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 6 deletions.
152 changes: 152 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/OfficeConverter/OfficeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ class OfficeConverter
private $tempPath;
private $extension;
private $basename;
private $prefixExecWithExportHome;

/**
* OfficeConverter constructor.
*
* @param $filename
* @param null $tempPath
* @param string $bin
* @param bool $prefixExecWithExportHome
*/
public function __construct($filename, $tempPath = null, $bin = 'libreoffice')
public function __construct($filename, $tempPath = null, $bin = 'libreoffice', $prefixExecWithExportHome = true)
{
if ($this->open($filename)) {
$this->setup($tempPath, $bin);
$this->setup($tempPath, $bin, $prefixExecWithExportHome);
}
}

Expand Down Expand Up @@ -72,10 +74,11 @@ protected function open($filename)
/**
* @param $tempPath
* @param $bin
* @param $prefixExecWithExportHome
*
* @throws OfficeConverterException
*/
protected function setup($tempPath, $bin)
protected function setup($tempPath, $bin, $prefixExecWithExportHome)
{
//basename
$this->basename = pathinfo($this->file, PATHINFO_BASENAME);
Expand All @@ -97,6 +100,9 @@ protected function setup($tempPath, $bin)

//binary location
$this->bin = $bin;

//use prefix export home or not
$this->prefixExecWithExportHome = $prefixExecWithExportHome;
}

/**
Expand Down Expand Up @@ -221,9 +227,11 @@ private function exec($cmd, $input = '')
{
// Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
// getenv('HOME') isn't set on Windows and generates a Notice.
$home = getenv('HOME');
if (!is_writable($home)) {
$cmd = 'export HOME=/tmp && ' . $cmd;
if ($this->prefixExecWithExportHome) {
$home = getenv('HOME');
if (!is_writable($home)) {
$cmd = 'export HOME=/tmp && ' . $cmd;
}
}
$process = proc_open($cmd, [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
fwrite($pipes[0], $input);
Expand Down

0 comments on commit a35f168

Please sign in to comment.