diff options
Diffstat (limited to 'modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php')
-rw-r--r-- | modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php | 127 |
1 files changed, 74 insertions, 53 deletions
diff --git a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php index 614f5ee4..5812c0cd 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php @@ -12,17 +12,28 @@ abstract class BootEntry /** Supports both via distinct entry */ const BOTH = 'PCBIOS-EFI'; - public abstract function supportsMode($mode); - /** - * @param ScriptBuilderBase $builder - * @return string + * @var string Internal ID - set to your liking, e.g. the MiniLinux version identifier */ - public abstract function toScript($builder); + protected $internalId; + + public function __construct(string $internalId) + { + $this->internalId = $internalId; + } + + public abstract function supportsMode(string $mode): bool; - public abstract function toArray(); + public abstract function toScript(ScriptBuilderBase $builder): string; - public abstract function addFormFields(&$array); + public abstract function toArray(): array; + + public abstract function addFormFields(array &$array): void; + + public function internalId(): string + { + return $this->internalId; + } /* * @@ -32,15 +43,15 @@ abstract class BootEntry * Return a BootEntry instance from the serialized data. * * @param string $module module this entry belongs to, or special values .script/.exec - * @param string $jsonString serialized entry data - * @return BootEntry|null instance representing boot entry, null on error + * @param string $data serialized entry data + * @return ?BootEntry instance representing boot entry, null on error */ - public static function fromJson($module, $data) + public static function fromJson(string $module, string $data): ?BootEntry { - if ($module{0} !== '.') { + if ($module[0] !== '.') { // Hook from other module $hook = Hook::loadSingle($module, 'ipxe-bootentry'); - if ($hook === false) { + if ($hook === null) { error_log('Module ' . $module . ' doesnt have an ipxe-bootentry hook'); return null; } @@ -49,9 +60,9 @@ abstract class BootEntry return null; return $ret->getBootEntry($data); } - if (is_string($data)) { - $data = json_decode($data, true); - } + $data = json_decode($data, true); + if (!is_array($data)) + return null; if ($module === '.script') { return new CustomBootEntry($data); } @@ -64,14 +75,14 @@ abstract class BootEntry return null; } - public static function forMenu($menuId) + public static function forMenu(int $menuId): MenuBootEntry { return new MenuBootEntry($menuId); } - public static function newStandardBootEntry($initData, $efi = false, $arch = false) + public static function newStandardBootEntry($initData, $efi = false, $arch = false, string $internalId = ''): ?StandardBootEntry { - $ret = new StandardBootEntry($initData, $efi, $arch); + $ret = new StandardBootEntry($initData, $efi, $arch, $internalId); $list = []; if ($ret->arch() !== self::EFI) { $list[] = self::BIOS; @@ -89,7 +100,7 @@ abstract class BootEntry return $ret; } - public static function newCustomBootEntry($initData) + public static function newCustomBootEntry($initData): ?CustomBootEntry { if (!is_array($initData) || empty($initData)) return null; @@ -99,15 +110,14 @@ abstract class BootEntry /** * Return a BootEntry instance from database with the given id. * - * @param string $id - * @return BootEntry|null|false false == unknown id, null = unknown entry type, BootEntry instance on success + * @return ?BootEntry null = unknown entry type, BootEntry instance on success */ - public static function fromDatabaseId($id) + public static function fromDatabaseId(string $id): ?BootEntry { $row = Database::queryFirst("SELECT module, data FROM serversetup_bootentry WHERE entryid = :id LIMIT 1", ['id' => $id]); if ($row === false) - return false; + return null; return self::fromJson($row['module'], $row['data']); } @@ -117,11 +127,11 @@ abstract class BootEntry * * @return BootEntry[] all existing BootEntries */ - public static function getAll() + public static function getAll(): array { - $res = Database::simpleQuery("SELECT entryid, data FROM serversetup_bootentry"); + $res = Database::simpleQuery("SELECT entryid, module, data FROM serversetup_bootentry"); $ret = []; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $tmp = self::fromJson($row['module'], $row['data']); if ($tmp === null) continue; @@ -143,14 +153,15 @@ class StandardBootEntry extends BootEntry */ protected $efi; /** - * @var string BootEntry Constants above + * @var ?string BootEntry Constants above */ protected $arch; const KEYS = ['executable', 'initRd', 'commandLine', 'replace', 'imageFree', 'autoUnload', 'resetConsole', 'dhcpOptions']; - public function __construct($data, $efi = false, $arch = false) + public function __construct($data, $efi = false, ?string $arch = null, string $internalId = '') { + parent::__construct($internalId); $this->pcbios = new ExecData(); $this->efi = new ExecData(); if ($data instanceof PxeSection) { @@ -223,10 +234,7 @@ class StandardBootEntry extends BootEntry } } - /** - * @param PxeSection $data - */ - private function fromPxeMenu($data) + private function fromPxeMenu(PxeSection $data): void { $bios = $this->pcbios; $bios->executable = $data->kernel; @@ -250,12 +258,12 @@ class StandardBootEntry extends BootEntry $bios->commandLine = trim(preg_replace('/\s+/', ' ', $bios->commandLine)); } - public function arch() + public function arch(): ?string { return $this->arch; } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { if ($mode === $this->arch || $this->arch === BootEntry::AGNOSTIC) return true; @@ -266,7 +274,7 @@ class StandardBootEntry extends BootEntry return false; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { if ($this->arch === BootEntry::AGNOSTIC) // Same as below, could construct fall-through but this is more clear return $builder->execDataToScript($this->pcbios, null, null); @@ -275,7 +283,7 @@ class StandardBootEntry extends BootEntry $this->supportsMode(BootEntry::EFI) ? $this->efi : null); } - public function addFormFields(&$array) + public function addFormFields(array &$array): void { $array[$this->arch . '_selected'] = 'selected'; $array['entries'][] = $this->pcbios->toFormFields(BootEntry::BIOS); @@ -283,7 +291,10 @@ class StandardBootEntry extends BootEntry $array['exec_checked'] = 'checked'; } - public function toArray() + /** + * @return array{PCBIOS: array, EFI: array, arch: string} + */ + public function toArray(): array { return [ BootEntry::BIOS => $this->pcbios->toArray(), @@ -298,7 +309,7 @@ class CustomBootEntry extends BootEntry /** * @var string iPXE */ - protected $ipxe; + protected $ipxe = ''; protected $bash; @@ -306,6 +317,7 @@ class CustomBootEntry extends BootEntry public function __construct($data) { + parent::__construct('custom'); if (is_array($data)) { $this->ipxe = $data['script'] ?? ''; // LEGACY foreach (['bash', 'grub'] as $key) { @@ -314,21 +326,24 @@ class CustomBootEntry extends BootEntry } } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { return true; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { + // TODO: A (very) simple translator for oneliners like "poweroff || goto fail" maybe? if ($builder instanceof ScriptBuilderIpxe) return $this->ipxe; if ($builder instanceof ScriptBuilderBash) return $this->bash; + if ($builder instanceof ScriptBuilderGrub) + return $this->grub; return ''; } - public function addFormFields(&$array) + public function addFormFields(array &$array): void { $array['entry'] = [ 'script' => $this->ipxe, @@ -336,7 +351,10 @@ class CustomBootEntry extends BootEntry $array['script_checked'] = 'checked'; } - public function toArray() + /** + * @return array{script: string} + */ + public function toArray(): array { return ['script' => $this->ipxe]; } @@ -344,30 +362,32 @@ class CustomBootEntry extends BootEntry class MenuBootEntry extends BootEntry { + /** @var int */ protected $menuId; - public function __construct($menuId) + public function __construct(int $menuId) { + parent::__construct('menu-' . $menuId); $this->menuId = $menuId; } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { return true; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { - $menu = IPxeMenu::get($this->menuId); + $menu = IPxeMenu::get($this->menuId, true); return $builder->menuToScript($menu); } - public function toArray() + public function toArray(): array { return []; } - public function addFormFields(&$array) + public function addFormFields(array &$array): void { } } @@ -380,23 +400,24 @@ class SpecialBootEntry extends BootEntry public function __construct($type) { $this->type = $type['type'] ?? $type; + parent::__construct('special-' . $this->type); } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { return true; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { return $builder->getSpecial($this->type); } - public function toArray() + public function toArray(): array { return []; } - public function addFormFields(&$array) { } + public function addFormFields(array &$array): void { } -}
\ No newline at end of file +} |