75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Hotfix\Hotfix;
|
|
|
|
use NoccyLabs\Hotfix\Exception\HotfixException;
|
|
use NoccyLabs\Hotfix\Service\ServiceManager;
|
|
use NoccyLabs\Hotfix\Hotfix\AliasManager;
|
|
|
|
class HotfixLoader
|
|
{
|
|
|
|
const PGP_HEADER_TOKEN = '-----BEGIN PGP SIGNATURE-----';
|
|
|
|
public static function load($uri)
|
|
{
|
|
// First, check if this is a valid URL or filename
|
|
if ((strpos($uri,"://")!==false) || (file_exists($uri))) {
|
|
return self::loadFromUri($uri);
|
|
}
|
|
|
|
if (strpos($uri,":")!==false) {
|
|
list($prefix,$id) = explode(":",$uri,2);
|
|
} else {
|
|
throw new HotfixException("Invalid hotfix specified {$uri}");
|
|
}
|
|
|
|
// Check services first, as aliases should for security reasons not
|
|
// be allowed to overload services.
|
|
if (($service = ServiceManager::getService($prefix))) {
|
|
return self::loadFromService($service,$id);
|
|
}
|
|
|
|
// Check aliases
|
|
if (($alias = AliasManager::getAlias($prefix))) {
|
|
$url = str_replace('{fix}', $id, $alias);
|
|
return self::loadFromUri($url);
|
|
}
|
|
|
|
throw new HotfixException("Could not read hotfix {$uri}");
|
|
}
|
|
|
|
private static function loadFromUri($url)
|
|
{
|
|
$source = file_get_contents($url);
|
|
return self::loadHotfix($source,$url);
|
|
}
|
|
|
|
private static function loadFromService(ReaderInterface $service, $id)
|
|
{
|
|
$source = $service->read($id);
|
|
return self::loadHotfix($source,$service->getName());
|
|
}
|
|
|
|
private static function loadHotfix($source,$origin)
|
|
{
|
|
|
|
// Check for a signature header
|
|
if (false === strpos($source, self::PGP_HEADER_TOKEN)) {
|
|
$body = $source;
|
|
$signature = new Signature($body,null);
|
|
} else {
|
|
list ($body, $sig) = explode(self::PGP_HEADER_TOKEN, $source);
|
|
$sig = self::PGP_HEADER_TOKEN.$sig;
|
|
$signature = new Signature($body,$sig);
|
|
}
|
|
|
|
// Extract header next
|
|
list ($header,$body) = explode("\n---\n", $body, 2);
|
|
$header = new Header($header);
|
|
|
|
$hotfix = new Hotfix($body, $header, $signature, $origin);
|
|
return $hotfix;
|
|
}
|
|
|
|
} |