Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions php-transformer/src/ArtifactCompiler/ArtifactCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function compile(array $artifact): TransformerResult
$sourceReports['compiled_site'] = $this->compiledSiteReport($normalized, $entryPath, $documents['documents'], $assets, $blockTypes, $serializedBlocks);
$sourceReports['materialization_plan'] = ( new MaterializationPlanBuilder() )->fromCompiledSite($sourceReports['compiled_site']);
$sourceReports['runtime_dependency_parity'] = ( new RuntimeDependencyParityReport() )->fromArtifact($normalized['files'], $html, $serializedBlocks, $entryPath);
if ( array() !== $entryBlocks['runtime_islands'] ) {
$sourceReports['runtime_islands'] = $entryBlocks['runtime_islands'];
}
$provenance = array(
array(
'source_format' => 'artifact',
Expand Down Expand Up @@ -126,7 +129,7 @@ public function compileFragment(string $content, string $source = 'fragment', st

/**
* @param array<int, array<string, mixed>> $files
* @return array{blocks: array<int, array<string, mixed>>, serialized_blocks: string, diagnostics: array<int, array<string, mixed>>, fallbacks: array<int, array<string, mixed>>, assets: array<int, array<string, mixed>>}
* @return array{blocks: array<int, array<string, mixed>>, serialized_blocks: string, diagnostics: array<int, array<string, mixed>>, fallbacks: array<int, array<string, mixed>>, assets: array<int, array<string, mixed>>, runtime_islands: array<int, array<string, mixed>>}
*/
private function compileEntryBlocks(string $html, string $entryPath, array $files): array
{
Expand All @@ -138,12 +141,13 @@ private function compileEntryBlocks(string $html, string $entryPath, array $file
'diagnostics' => $this->entryTransformDiagnostics($result['diagnostics']),
'fallbacks' => $result['fallbacks'],
'assets' => $result['assets'],
'runtime_islands' => $result['runtime_islands'],
);
}

/**
* @param array<int, array<string, mixed>> $files
* @return array{blocks: array<int, array<string, mixed>>, serialized_blocks: string, diagnostics: array<int, array<string, mixed>>, fallbacks: array<int, array<string, mixed>>, assets: array<int, array<string, mixed>>}
* @return array{blocks: array<int, array<string, mixed>>, serialized_blocks: string, diagnostics: array<int, array<string, mixed>>, fallbacks: array<int, array<string, mixed>>, assets: array<int, array<string, mixed>>, runtime_islands: array<int, array<string, mixed>>}
*/
private function compileHtmlDocumentBlocks(string $html, string $sourcePath, array $files, string $sourceScope): array
{
Expand All @@ -154,6 +158,7 @@ private function compileHtmlDocumentBlocks(string $html, string $sourcePath, arr
'diagnostics' => array(),
'fallbacks' => array(),
'assets' => array(),
'runtime_islands' => array(),
);
}

Expand All @@ -164,6 +169,7 @@ private function compileHtmlDocumentBlocks(string $html, string $sourcePath, arr
'diagnostics' => array(),
'fallbacks' => array(),
'assets' => array(),
'runtime_islands' => array(),
);
}

Expand All @@ -172,6 +178,7 @@ private function compileHtmlDocumentBlocks(string $html, string $sourcePath, arr
'source_scope' => $sourceScope,
'static_css' => $this->linkedStylesheetCss($html, $sourcePath, $files),
'asset_metadata' => $this->assetMetadataForSource($sourcePath, $files),
'runtime_script_metadata' => $this->runtimeScriptMetadataForSource($html, $sourcePath, $files),
'runtime_dom_selectors' => $this->runtimeDomSelectors($html, $sourcePath, $files),
'runtime_canvas_selectors' => $this->runtimeCanvasSelectors($html, $sourcePath, $files),
))->toArray();
Expand All @@ -182,6 +189,7 @@ private function compileHtmlDocumentBlocks(string $html, string $sourcePath, arr
'diagnostics' => is_array($result['diagnostics'] ?? null) ? $result['diagnostics'] : array(),
'fallbacks' => is_array($result['fallbacks'] ?? null) ? $result['fallbacks'] : array(),
'assets' => is_array($result['assets'] ?? null) ? $result['assets'] : array(),
'runtime_islands' => is_array($result['source_reports']['runtime_islands'] ?? null) ? $result['source_reports']['runtime_islands'] : array(),
);
}

Expand Down Expand Up @@ -625,6 +633,65 @@ private function assetMetadataForSource(string $sourcePath, array $files): array
return $metadata;
}

/**
* @param array<int, array<string, mixed>> $files
* @return array<int, array<string, mixed>>
*/
private function runtimeScriptMetadataForSource(string $html, string $sourcePath, array $files): array
{
if ( ! preg_match_all('/<script\b[^>]*>/i', $html, $matches) ) {
return array();
}

$metadata = array();
foreach ( $matches[0] as $tag ) {
$src = $this->htmlAttribute((string) $tag, 'src');
if ( '' === $src ) {
continue;
}

$asset = $this->findAssetByHtmlReference($src, $sourcePath, $files);
if ( ! is_array($asset) || ! $this->isMaterializedScriptAsset($asset) ) {
continue;
}

$metadata[] = array_filter(array(
'path' => (string) ($asset['path'] ?? ''),
'selector' => 'script[src="' . $src . '"]',
'attributes' => array_filter(array(
'src' => $src,
'type' => $this->htmlAttribute((string) $tag, 'type'),
'async' => $this->htmlAttribute((string) $tag, 'async'),
'defer' => $this->htmlAttribute((string) $tag, 'defer'),
), static fn (string $value): bool => '' !== $value),
'script_role' => 'runtime',
'script_source_kind' => 'external',
), static fn (mixed $value): bool => '' !== $value && array() !== $value);
}

return $this->dedupeRows($metadata);
}

/**
* @param array<int, array<string, mixed>> $rows
* @return array<int, array<string, mixed>>
*/
private function dedupeRows(array $rows): array
{
$seen = array();
$deduped = array();
foreach ( $rows as $row ) {
$key = json_encode($row, JSON_UNESCAPED_SLASHES);
if ( ! is_string($key) || isset($seen[$key]) ) {
continue;
}
$seen[$key] = true;
$deduped[] = $row;
}

return $deduped;
}

/**
* @return array<int, string>
*/
Expand Down
18 changes: 18 additions & 0 deletions php-transformer/src/Contract/ConversionReportProjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static function fromResultParts(string $sourceFormat, array $blocks, arra
'navigation_candidates' => self::navigationCandidates($blocks, $sourceReports),
'semantic_parity' => self::semanticParity($sourceReports),
'runtime_dependency_parity' => self::runtimeDependencyParity($sourceReports),
'runtime_islands' => self::runtimeIslands($sourceReports),
'interaction_candidates' => self::interactionCandidates($sourceReports),
'presentation_gaps' => self::presentationGaps($sourceReports),
'metrics' => $metrics,
Expand Down Expand Up @@ -313,6 +314,23 @@ private static function interactionCandidates(array $sourceReports): array
return self::dedupeRows(array_values(array_filter($candidates, static fn (mixed $candidate): bool => is_array($candidate))));
}

/**
* @param array<string, mixed> $sourceReports
* @return array<int, array<string, mixed>>
*/
private static function runtimeIslands(array $sourceReports): array
{
$islands = $sourceReports['runtime_islands'] ?? array();
if ( ! is_array($islands) ) {
$islands = array();
}

$html = is_array($sourceReports['html'] ?? null) ? $sourceReports['html'] : array();
$htmlIslands = is_array($html['runtime_islands'] ?? null) ? $html['runtime_islands'] : array();

return self::dedupeRows(array_values(array_filter(array_merge($islands, $htmlIslands), static fn (mixed $island): bool => is_array($island))));
}

/**
* @param array<string, mixed> $sourceReports
* @return array<string, mixed>
Expand Down
2 changes: 1 addition & 1 deletion php-transformer/src/Contract/TransformerResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function assertCanonicalEnvelope(array $result, bool $requireMater
throw new InvalidArgumentException('Canonical transformer result conversion report is missing source_format.');
}

foreach ( array( 'source_summary', 'selector_summary', 'fallback_diagnostics', 'asset_refs', 'navigation_candidates', 'interaction_candidates', 'presentation_gaps', 'metrics' ) as $key ) {
foreach ( array( 'source_summary', 'selector_summary', 'fallback_diagnostics', 'asset_refs', 'navigation_candidates', 'interaction_candidates', 'runtime_islands', 'presentation_gaps', 'metrics' ) as $key ) {
if ( array_key_exists($key, $conversionReport) && ! is_array($conversionReport[$key]) ) {
throw new InvalidArgumentException(sprintf('Canonical transformer result conversion report %s must be an array.', $key));
}
Expand Down
Loading
Loading