Skip to content

Commit

Permalink
add value types on arrays when content don t come from json or yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
lhapaipai committed Feb 14, 2024
1 parent d59e657 commit 68b0bd4
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 19 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"php-cs-fixer.executablePath": "${workspaceFolder}/bin/php-cs-fixer",
"phpstan.rootDir": "src/vite-bundle"
}
8 changes: 4 additions & 4 deletions src/vite-bundle/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameters:
level: max
ignoreErrors:
- "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ has parameter \\$[a-zA-Z0-9_]+ with no value type specified in iterable type array#"
- "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ return type has no value type specified in iterable type array#"
- "#Property [a-zA-Z0-9\\$\\_\\\\:\\(\\)]+ type has no value type specified in iterable type array#"
# ignoreErrors:
# - "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ has parameter \\$[a-zA-Z0-9_]+ with no value type specified in iterable type array#"
# - "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ return type has no value type specified in iterable type array#"
# - "#Property [a-zA-Z0-9\\$\\_\\\\:\\(\\)]+ type has no value type specified in iterable type array#"
paths:
- src

Expand Down
3 changes: 3 additions & 0 deletions src/vite-bundle/src/CacheWarmer/EntrypointsCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class EntrypointsCacheWarmer extends AbstractPhpFileCacheWarmer
{
/**
* @param array<string, array<mixed>> $configs
*/
public function __construct(
private string $publicPath,
private array $configs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ private function entrypointsLookupFactory(
return new Reference($id);
}

/**
* @param array<string, bool|string|null> $defaultAttributes
* @param array<string, mixed> $config
*/
private function tagRendererFactory(
ContainerBuilder $container,
array $defaultAttributes,
Expand All @@ -158,6 +162,11 @@ private function getServiceId(string $prefix, string $configName): string
return sprintf('pentatrion_vite.%s[%s]', $prefix, $configName);
}

/**
* @param array<string, mixed> $config
*
* @return array<string, mixed>
*/
public static function prepareConfig(array $config): array
{
$base = '/'.trim($config['build_directory'], '/').'/';
Expand Down
10 changes: 6 additions & 4 deletions src/vite-bundle/src/EventListener/PreloadAssetsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public function onKernelResponse(ResponseEvent $event): void
$linkProvider = $linkProvider->withLink($link);
}

foreach ($this->entrypointRenderer->getRenderedStyles() as $href) {
$link = $this->createLink('preload', $href)->withAttribute('as', 'style');

$linkProvider = $linkProvider->withLink($link);
foreach ($this->entrypointRenderer->getRenderedStyles() as $filePath => $tag) {
$href = $tag->getAttribute('href');
if (is_string($href)) {
$link = $this->createLink('preload', $href)->withAttribute('as', 'style');
$linkProvider = $linkProvider->withLink($link);
}
}

$request->attributes->set('_links', $linkProvider);
Expand Down
11 changes: 10 additions & 1 deletion src/vite-bundle/src/Model/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ class Tag
public const SCRIPT_TAG = 'script';
public const LINK_TAG = 'link';

/**
* @param array<string, bool|string> $attributes
*/
public function __construct(
private string $tagName,
private array $attributes,
private array $attributes = [],
private string $content = '',
private bool $internal = false
) {
Expand Down Expand Up @@ -44,11 +47,17 @@ public function isModulePreload(): bool
&& 'modulepreload' === $this->attributes['rel'];
}

/**
* @return array<string, bool|string|null>
*/
public function getAttributes(): array
{
return $this->attributes;
}

/**
* @return bool|string|null
*/
public function getAttribute(string $key): mixed
{
return key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
Expand Down
18 changes: 15 additions & 3 deletions src/vite-bundle/src/Service/EntrypointRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@

class EntrypointRenderer implements ResetInterface
{
/** @var array<string, bool> */
private array $returnedViteClients = [];

/** @var array<string, bool> */
private array $returnedReactRefresh = [];

/** @var array<string, bool> */
private array $returnedViteLegacyScripts = [];

/** @var array<'scripts'|'styles', array<string, Tag>> */
private array $renderedFiles = [
'scripts' => [],
'styles' => [],
Expand Down Expand Up @@ -86,6 +92,9 @@ public function getRenderedScripts(): array
return $this->renderedFiles['scripts'];
}

/**
* @return array<string, Tag>
*/
public function getRenderedStyles(): array
{
return $this->renderedFiles['styles'];
Expand Down Expand Up @@ -223,12 +232,15 @@ public function renderLinks(
$tags = [];

foreach ($entrypointsLookup->getCSSFiles($entryName) as $filePath) {
if (false === \in_array($filePath, $this->renderedFiles['styles'], true)) {
$tags[] = $tagRenderer->createLinkStylesheetTag(
if (!isset($this->renderedFiles['styles'][$filePath])) {
$tag = $tagRenderer->createLinkStylesheetTag(
$this->completeURL($filePath, $useAbsoluteUrl),
array_merge(['integrity' => $entrypointsLookup->getFileHash($filePath)], $options['attr'] ?? [])
);
$this->renderedFiles['styles'][] = $filePath;

$tags[] = $tag;

$this->renderedFiles['styles'][$filePath] = $tag;
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/vite-bundle/src/Service/EntrypointsLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class EntrypointsLookup
{
/** @var array<mixed>|null */
private ?array $fileContent = null;

public function __construct(
Expand All @@ -21,6 +22,9 @@ public function hasFile(): bool
return $this->fileAccessor->hasFile($this->configName, 'entrypoints');
}

/**
* @return array<mixed>
*/
private function getFileContent(): array
{
if (is_null($this->fileContent)) {
Expand Down Expand Up @@ -70,27 +74,39 @@ public function getBase(): string
return $this->getFileContent()['base'];
}

/**
* @return array<string>
*/
public function getJSFiles(string $entryName): array
{
$this->throwIfEntrypointIsMissing($entryName);

return $this->getFileContent()['entryPoints'][$entryName]['js'] ?? [];
}

/**
* @return array<string>
*/
public function getCSSFiles(string $entryName): array
{
$this->throwIfEntrypointIsMissing($entryName);

return $this->getFileContent()['entryPoints'][$entryName]['css'] ?? [];
}

/**
* @return array<string>
*/
public function getJavascriptDependencies(string $entryName): array
{
$this->throwIfEntrypointIsMissing($entryName);

return $this->getFileContent()['entryPoints'][$entryName]['preload'] ?? [];
}

/**
* @return array<string>
*/
public function getJavascriptDynamicDependencies(string $entryName): array
{
$this->throwIfEntrypointIsMissing($entryName);
Expand Down
4 changes: 1 addition & 3 deletions src/vite-bundle/src/Service/EntrypointsLookupCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

class EntrypointsLookupCollection
{
/**
* @param ServiceLocator<EntrypointsLookup> $entrypointsLookupLocator
*/
/** @param ServiceLocator<EntrypointsLookup> $entrypointsLookupLocator */
public function __construct(
private ServiceLocator $entrypointsLookupLocator,
private string $defaultConfigName
Expand Down
11 changes: 10 additions & 1 deletion src/vite-bundle/src/Service/FileAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class FileAccessor
self::MANIFEST => 'manifest.json',
];

/** @var array<string, array<key-of<FileAccessor::FILES>,array<mixed>>> */
private array $content;

/** @param array<string, array<mixed>> $configs */
public function __construct(
private string $publicPath,
private array $configs,
Expand All @@ -33,6 +35,11 @@ public function hasFile(string $configName, string $fileType): bool
return file_exists($basePath.'.vite/'.self::FILES[$fileType]) || file_exists($basePath.self::FILES[$fileType]);
}

/**
* @param key-of<FileAccessor::FILES> $fileType
*
* @return array<mixed>
*/
public function getData(string $configName, string $fileType): array
{
$cacheItem = null;
Expand All @@ -41,7 +48,9 @@ public function getData(string $configName, string $fileType): array
$cacheItem = $this->cache->getItem("$configName.$fileType");

if ($cacheItem->isHit()) {
$this->content[$configName][$fileType] = $cacheItem->get();
/** @var array<mixed> $data */
$data = $cacheItem->get();
$this->content[$configName][$fileType] = $data;
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/vite-bundle/src/Service/TagRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class TagRenderer
{
/**
* @param array<string, bool|string|null> $globalDefaultAttributes
* @param array<string, bool|string|null> $globalScriptAttributes
* @param array<string, bool|string|null> $globalLinkAttributes
* @param array<string, bool|string|null> $globalPreloadAttributes
*/
public function __construct(
private array $globalDefaultAttributes = [],
private array $globalScriptAttributes = [],
Expand Down Expand Up @@ -57,6 +63,7 @@ public function createDetectModernBrowserScript(): Tag
);
}

/** @param array<string, bool|string|null> $attributes */
public function createInternalScriptTag(array $attributes = [], string $content = ''): Tag
{
$tag = new Tag(
Expand All @@ -69,6 +76,7 @@ public function createInternalScriptTag(array $attributes = [], string $content
return $tag;
}

/** @param array<string, bool|string|null> $attributes */
public function createScriptTag(array $attributes = [], string $content = ''): Tag
{
$tag = new Tag(
Expand All @@ -84,6 +92,7 @@ public function createScriptTag(array $attributes = [], string $content = ''): T
return $tag;
}

/** @param array<string, bool|string|null> $extraAttributes */
public function createLinkStylesheetTag(string $fileName, array $extraAttributes = []): Tag
{
$attributes = [
Expand All @@ -104,6 +113,7 @@ public function createLinkStylesheetTag(string $fileName, array $extraAttributes
return $tag;
}

/** @param array<string, bool|string|null> $extraAttributes */
public function createModulePreloadLinkTag(string $fileName, array $extraAttributes = []): Tag
{
$attributes = [
Expand Down Expand Up @@ -139,6 +149,7 @@ public static function generateTag(Tag $tag): string
);
}

/** @param array<string, bool|string|null> $attributes */
private static function convertArrayToAttributes(array $attributes): string
{
$nonNullAttributes = array_filter(
Expand Down
4 changes: 1 addition & 3 deletions src/vite-bundle/src/Service/TagRendererCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

class TagRendererCollection
{
/**
* @param ServiceLocator<TagRenderer> $tagRendererLocator
*/
/** @param ServiceLocator<TagRenderer> $tagRendererLocator */
public function __construct(
private ServiceLocator $tagRendererLocator,
private string $defaultConfigName
Expand Down

0 comments on commit 68b0bd4

Please sign in to comment.