blue-twilight/public/bootstrap/src/Bootstrapper.php

136 lines
4.1 KiB
PHP

<?php
namespace AppBootstrap;
use App\Services\GiteaService;
/**
* This class handles the downloading and extracting of the vendors directory.
* Because Laravel and other vendors are not yet available, it uses "raw" PHP and the odd few classes within Blue
* Twilight, such as GiteaService.
*
* @package AppBootstrap
*/
class Bootstrapper
{
private $configDir;
private $rootDir;
private $tempDir;
private $viewsDir;
public function __construct()
{
$this->rootDir = dirname(__DIR__);
$this->configDir = sprintf('%s/config', dirname(dirname($this->rootDir)));
$this->tempDir = sprintf('%s/temp', $this->rootDir);
$this->viewsDir = sprintf('%s/views', $this->rootDir);
}
public function handleRequest()
{
if (!isset($_GET['act']))
{
$this->view('index', ['appName' => 'Blue Twilight Bootstrapper']);
}
else
{
switch (trim($_GET['act']))
{
case 'download':
$this->download();
return;
default:
throw new \Exception(sprintf('ERROR: Action \'%s\' was not recognised.', $_GET['act']));
}
}
}
private function download()
{
$appConfig = require_once sprintf('%s/app.php', $this->configDir);
$servicesConfig = require_once sprintf('%s/services.php', $this->configDir);
$versionNumber = sprintf('v%s', $appConfig['version']);
$gitea = new GiteaService($servicesConfig['gitea'], $versionNumber);
$releaseInfo = $gitea->getSpecificRelease($versionNumber);
if (is_null($releaseInfo))
{
throw new \Exception(sprintf('No release info found in Gitea for Blue Twilight version \'%\'', $versionNumber));
}
else if (!isset($releaseInfo->assets))
{
throw new \Exception(sprintf('No assets found in Gitea for Blue Twilight version \'%\'', $versionNumber));
}
$vendorsPrefix = 'vendors';
$vendorsSuffix = '.tar.gz';
$selectedAsset = null;
foreach ($releaseInfo->assets as $asset)
{
/*
Ignore anything that is not "vendors<something>.tar.gz" were the <something> is also optional - e.g.
vendors_2.1.2.tar.gz
vendors.tar.gz
but NOT 2.1.2_vendors.zip
*/
if (!starts_with($asset->name, $vendorsPrefix) || !ends_with($asset->name, $vendorsSuffix))
{
continue;
}
$selectedAsset = $asset;
break;
}
if (is_null($selectedAsset))
{
throw new \Exception('No vendors.tar.gz found in Gitea for Blue Twilight version \'%\'', $versionNumber);
}
$targetFileName = sprintf('%s/vendors.tar.gz', $this->tempDir);
$this->downloadFile($selectedAsset->browser_download_url, $targetFileName);
var_dump('done');
}
private function downloadFile($sourceURL, $targetFilename)
{
$urlHandle = @fopen($sourceURL, 'r');
$tempFilename = @fopen($targetFilename, 'w');
if ($urlHandle === false)
{
throw new \Exception(sprintf('Failed downloading the file from %s', $sourceURL));
}
else if ($tempFilename === false)
{
throw new \Exception(sprintf('Failed opening the file \'%s\' for writing', $targetFilename));
}
while (!feof($urlHandle))
{
$buffer = fread($urlHandle, 8192);
fwrite($tempFilename, $buffer);
}
@fclose($urlHandle);
@fclose($tempFilename);
}
private function view($name, array $viewData = [])
{
$viewFile = sprintf('%s/%s.php', $this->viewsDir, $name);
if (!file_exists($viewFile) || !is_readable($viewFile))
{
throw new \Exception(sprintf('ERROR: View file \'%s\' does not exist.', $viewFile));
}
// Provide keys as variables - e.g. $viewData['something'] becomes accessible via $something
extract($viewData);
require_once $viewFile;
}
}