misuzu/src/Apps/AppsContext.php

71 lines
2 KiB
PHP

<?php
namespace Misuzu\Apps;
use RuntimeException;
use Index\Db\DbConnection;
class AppsContext {
public private(set) AppsData $apps;
public private(set) ScopesData $scopes;
public function __construct(DbConnection $dbConn) {
$this->apps = new AppsData($dbConn);
$this->scopes = new ScopesData($dbConn);
}
/**
* @return array<string, string>
*/
public function handleScopeString(
AppInfo|string $appInfo,
string $scope,
bool $allowDeprecated = false,
bool $sort = true,
bool $breakOnFail = true
): array {
return $this->handleScopes($appInfo, explode(' ', $scope), $allowDeprecated, $sort, $breakOnFail);
}
/**
* @param string[] $strings
* @return array<string, string|ScopeInfo>
*/
public function handleScopes(
AppInfo|string $appInfo,
array $strings,
bool $allowDeprecated = false,
bool $sort = true,
bool $breakOnFail = true
): array {
if(is_string($appInfo))
$appInfo = $this->apps->getAppInfo(appId: $appInfo, deleted: false);
$infos = [];
foreach($strings as $string) {
try {
$scopeInfo = $this->scopes->getScopeInfo($string, ScopeInfoGetField::String);
if(!$allowDeprecated && $scopeInfo->deprecated) {
$infos[$string] = 'deprecated';
if($breakOnFail) break; else continue;
}
if(!$this->apps->isAppScopeAllowed($appInfo, $scopeInfo)) {
$infos[$string] = 'restricted';
if($breakOnFail) break; else continue;
}
$infos[$string] = $scopeInfo;
} catch(RuntimeException $ex) {
$infos[$string] = 'unknown';
if($breakOnFail) break; else continue;
}
}
if($sort)
ksort($infos, SORT_STRING);
return $infos;
}
}