Skip to content

Commit

Permalink
copy cacert.pem to temp directory when running from phar archive
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed Jul 11, 2015
1 parent 8153032 commit eeb4477
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/GW2Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class GW2Api {
function __construct( array $options = [] ) {
$this->options = $this->getOptions( $options );

$this->extractCacertFile();

$this->client = new Client( $this->options );
$this->options = $options;

Expand All @@ -48,11 +50,46 @@ protected function getOptions( array $options = [] ) {
return [
'base_url' => $this->apiUrl,
'defaults' => [
'verify' => __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem'
'verify' => $this->getCacertFilePath()
]
] + $options;
}

/**
* Returns the path to cacert.pem.
*
* @return string
*/
protected function getCacertFilePath() {
if( $this->isIncludedAsPhar() ) {
return sys_get_temp_dir() . '/gw2api-cacert.pem';
} else {
return __DIR__ . '/cacert.pem';
}
}

/**
* Copies the phar cacert from the phar into the temp directory.
*/
protected function extractCacertFile() {
if( $this->isIncludedAsPhar() ) {
$cacertFilePath = $this->getCacertFilePath();

if( !file_exists( $cacertFilePath )) {
copy( __DIR__ . '/cacert.pem', $cacertFilePath );
}
}
}

/**
* Checks if the library is included as phar file.
*
* @return bool
*/
protected function isIncludedAsPhar() {
return strpos( __FILE__, 'phar://' ) === 0;
}

/**
* @param string $handler
*/
Expand Down

0 comments on commit eeb4477

Please sign in to comment.