Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move away from PECL extension #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = lf
charset = utf-8
indent_size = tab
indent_style = tab
Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w00t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert_final_newline = true
max_line_length = off
trim_trailing_whitespace = true
tab_width = 4
21 changes: 21 additions & 0 deletions src/Exception/GpgBinaryNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace PharIo\GnuPG\Exception;

use RuntimeException;

class GpgBinaryNotFound extends RuntimeException
{
public static function inDefaultPath(): self
{
return new self('No gnupg binary found - please specify or install the pecl/gnupg extension.');
}

public static function inProvidedPath(string $path): self
{
return new self(sprintf(
'No gnupg binary found in %1$s - please check the location',
$path
));
}
}
19 changes: 19 additions & 0 deletions src/Exception/GpgExtensionNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace PharIo\GnuPG\Exception;

use RuntimeException;
use Throwable;

class GpgExtensionNotFound extends RuntimeException
{
public static function inPhp(): self
{
return new self('The pecl/GnuPG extension for PHP was not found. You might want to install it.');
}

public static function orBroken(Throwable $t): self
{
return new self('The pexl/GnuPG extension for PHP was found but an Exception was thrown when using it. You might want to check the installation.', null, $t);
}
}
69 changes: 23 additions & 46 deletions src/Factory.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php declare(strict_types = 1);

namespace PharIo\GnuPG;

use PharIo\Executor\Executor;
use PharIo\FileSystem\Directory;
use PharIo\FileSystem\Filename;
use PharIo\GnuPG\Exception\GpgBinaryNotFound;
use PharIo\GnuPG\Exception\GpgExtensionNotFound;
use PharIo\GnuPG\GnuPG as GPG;
use function extension_loaded;
use function putenv;
use function sys_get_temp_dir;

/** @noinspection PhpComposerExtensionStubsInspection */
class Factory {

/** @var Filename */
private $gpgBinary;

/**
* Factory constructor.
*
Expand All @@ -20,49 +22,24 @@ public function __construct(Filename $gpgBinary = null) {
$this->gpgBinary = $gpgBinary;
}

public function createGnuPG(Directory $homeDirectory): \Gnupg {
if (\extension_loaded('gnupg')) {
\putenv('GNUPGHOME=' . $homeDirectory->asString());
$gpg = new \Gnupg();
$gpg->seterrormode(\Gnupg::ERROR_SILENT);

return $gpg;
public function createGnuPG(Directory $homeDirectory): GnuPG
{
putenv('GNUPGHOME=' . $homeDirectory->asString());
try {
return new GPG\Extension();
} catch (GpgExtensionNotFound $e) {
// Do nothing on purpose
}

$gpg = new GnuPG(
new Executor(),
$this->getGPGBinaryPath(),
$this->getTempDirectory(),
$homeDirectory
);

if (!\class_exists('\Gnupg')) {
\class_alias(GnuPG::class, '\Gnupg');
}

/** @var \Gnupg $gpg */
return $gpg;
}

/**
* @throws Exception
*/
private function getGPGBinaryPath(): Filename {
if ($this->gpgBinary === null) {
$which = \stripos(\PHP_OS, 'WIN') === 0 ? 'where.exe' : 'which';
$result = \exec(\sprintf('%s %s', $which, 'gpg'), $output, $exitCode);

if ($exitCode !== 0) {
throw new Exception('No gnupg binary found - please specify or install the pecl/gnupg extension.');
}
$resultLines = \explode("\n", $result, 2);
$this->gpgBinary = new Filename($resultLines[0]);
try {
return GPG\Binary::createwithPaths(
$this->gpgBinary->asString(),
sys_get_temp_dir(),
$homeDirectory->asString()
);
} catch (GpgBinaryNotFound $e) {
// Do nothing on purpose
}

return $this->gpgBinary;
}

private function getTempDirectory(): Directory {
return new Directory(\sys_get_temp_dir());
return new GPG\NoGpgAvailable;
}
}
Loading