66 lines
2.4 KiB
PHP
66 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use DOMElement;
|
|
use DOMText;
|
|
use DOMXPath;
|
|
use LogicException;
|
|
|
|
/** Mechanical console-to-GUI executable correction; no scheduling or business-policy change. */
|
|
final class DeadlineTaskGuiAction
|
|
{
|
|
public static function replacement(string $xml, string $old, string $new): string
|
|
{
|
|
$normal = str_replace('\\', '/', $old);
|
|
if (! str_ends_with($normal, '/php.exe') || $new !== substr($normal, 0, -7).'php-win.exe') {
|
|
throw new LogicException('DEADLINE_TASK_GUI_EXECUTABLE_PAIR');
|
|
}
|
|
$document = WindowsTaskXml::parse($xml);
|
|
$xpath = new DOMXPath($document);
|
|
$xpath->registerNamespace('t', 'http://schemas.microsoft.com/windows/2004/02/mit/task');
|
|
$nodes = $xpath->query('/t:Task/t:Actions/t:Exec/t:Command');
|
|
$actions = $xpath->query('/t:Task/t:Actions/*');
|
|
$command = $nodes === false ? null : $nodes->item(0);
|
|
if ($actions === false || $nodes === false || $actions->length !== 1 || $nodes->length !== 1
|
|
|| ! $command instanceof DOMElement || $command->textContent !== $old) {
|
|
throw new LogicException('DEADLINE_TASK_GUI_ACTION_MISMATCH');
|
|
}
|
|
$command->nodeValue = $new;
|
|
$output = $document->saveXML();
|
|
if (! is_string($output)) {
|
|
throw new LogicException('DEADLINE_TASK_GUI_XML');
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
public static function assertConserved(string $before, string $after, string $old, string $new): void
|
|
{
|
|
if (self::canonical(self::replacement($before, $old, $new)) !== self::canonical($after)) {
|
|
throw new LogicException('DEADLINE_TASK_GUI_UNRELATED_DEFINITION_CHANGE');
|
|
}
|
|
}
|
|
|
|
public static function canonical(string $xml): string
|
|
{
|
|
$document = WindowsTaskXml::parse($xml);
|
|
$xpath = new DOMXPath($document);
|
|
$blanks = $xpath->query('//text()[normalize-space(.)=""]');
|
|
if ($blanks === false) {
|
|
throw new LogicException('DEADLINE_TASK_GUI_XML');
|
|
}
|
|
foreach ($blanks as $blank) {
|
|
if ($blank instanceof DOMText && $blank->parentNode !== null) {
|
|
$blank->parentNode->removeChild($blank);
|
|
}
|
|
}
|
|
$canonical = $document->documentElement->C14N();
|
|
if (! is_string($canonical)) {
|
|
throw new LogicException('DEADLINE_TASK_GUI_XML');
|
|
}
|
|
|
|
return $canonical;
|
|
}
|
|
}
|