From c692190390588b0934404e07d9ead4155af63e25 Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Wed, 20 Sep 2023 23:51:36 +0200 Subject: [PATCH] remove dependencies `Nette\Finder` and `Nette\Utils --- composer.json | 4 +- src/Utilities/ManifestRecord.php | 11 ++-- src/Utilities/ViteManifest.php | 94 ++++++++++++++------------- src/View/Helper/ViteScriptsHelper.php | 14 ++-- 4 files changed, 66 insertions(+), 57 deletions(-) diff --git a/composer.json b/composer.json index 1dc0b56..cf39bd7 100644 --- a/composer.json +++ b/composer.json @@ -5,9 +5,7 @@ "license": "MIT", "require": { "php": ">=8.0", - "cakephp/cakephp": "^4.2.0", - "nette/finder": "^2.5", - "nette/utils": "^v3.0.0" + "cakephp/cakephp": "^4.2.0" }, "require-dev": { "phpunit/phpunit": "^8.5 || ^9.3", diff --git a/src/Utilities/ManifestRecord.php b/src/Utilities/ManifestRecord.php index 5f73dc2..0e7aee4 100644 --- a/src/Utilities/ManifestRecord.php +++ b/src/Utilities/ManifestRecord.php @@ -3,7 +3,6 @@ namespace ViteHelper\Utilities; -use Nette\Utils\Strings; use stdClass; class ManifestRecord @@ -75,7 +74,7 @@ public function match(string $needle, string $property = 'src'): bool { $field = $this->getChunk($property); - return is_string($field) && Strings::contains($field, $needle); + return is_string($field) && str_contains($field, $needle); } /** @@ -121,7 +120,7 @@ public function matchAll(array $names, string $property = 'src'): bool */ public function isLegacy(): bool { - return Strings::contains($this->chunk->file, 'legacy'); + return str_contains($this->chunk->file, 'legacy'); } /** @@ -131,7 +130,7 @@ public function isLegacy(): bool */ public function isPolyfill(): bool { - return Strings::contains($this->chunk->file, 'polyfills'); + return str_contains($this->chunk->file, 'polyfills'); } /** @@ -141,7 +140,7 @@ public function isPolyfill(): bool */ public function isJavascript(): bool { - return Strings::endsWith($this->chunk->file, '.js'); + return str_ends_with($this->chunk->file, '.js'); } /** @@ -151,7 +150,7 @@ public function isJavascript(): bool */ public function isStylesheet(): bool { - return Strings::endsWith((string)$this->chunk->file, '.css'); + return str_ends_with((string)$this->chunk->file, '.css'); } /** diff --git a/src/Utilities/ViteManifest.php b/src/Utilities/ViteManifest.php index 62f65f1..66be3c9 100644 --- a/src/Utilities/ViteManifest.php +++ b/src/Utilities/ViteManifest.php @@ -3,8 +3,6 @@ namespace ViteHelper\Utilities; -use Nette\Utils\FileSystem; -use Nette\Utils\Json; use ViteHelper\Exception\ManifestNotFoundException; /** @@ -12,53 +10,61 @@ */ class ViteManifest { - /** - * Returns the manifest records as a Collection - * - * @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance - * @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[] - * @throws \ViteHelper\Exception\ManifestNotFoundException - * @internal - */ - public static function getRecords(ViteHelperConfig $config): ManifestRecords - { - $manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST); + /** + * Returns the manifest records as a Collection + * + * @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance + * @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[] + * @throws \ViteHelper\Exception\ManifestNotFoundException + * @internal + */ + public static function getRecords(ViteHelperConfig $config): ManifestRecords + { + $manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST); - try { - $json = FileSystem::read($manifestPath); + if (!is_readable($manifestPath)) { + throw new ManifestNotFoundException( + "No valid manifest.json found at path {$manifestPath}. Did you build your js?", + ); + } - $json = str_replace([ - "\u0000", - ], '', $json); + $json = @file_get_contents($manifestPath); - $manifest = Json::decode($json); - } catch (\Exception $e) { - throw new ManifestNotFoundException( - "No valid manifest.json found at path {$manifestPath}. Did you build your js? Error: {$e->getMessage()}" - ); - } + if ($json === false) { + throw new ManifestNotFoundException('Could not parse manifest.json'); + } - $manifestArray = []; - foreach (get_object_vars($manifest) as $property => $value) { - $manifestArray[$property] = new ManifestRecord($property, $value, $config); - } + $json = str_replace( + [ + "\u0000", + ], + '', + $json + ); - /** - * Legacy Polyfills must come first. - */ - usort($manifestArray, function ($file) { - /** @var \ViteHelper\Utilities\ManifestRecord $file */ - return $file->isPolyfill() ? 0 : 1; - }); + $manifest = json_decode($json, false, 512, JSON_THROW_ON_ERROR); - /** - * ES-module scripts must come last. - */ - usort($manifestArray, function ($file) { - /** @var \ViteHelper\Utilities\ManifestRecord $file */ - return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0; - }); + $manifestArray = []; + foreach (get_object_vars($manifest) as $property => $value) { + $manifestArray[$property] = new ManifestRecord($property, $value, $config); + } - return new ManifestRecords($manifestArray, $manifestPath); - } + /** + * Legacy Polyfills must come first. + */ + usort($manifestArray, function ($file) { + /** @var \ViteHelper\Utilities\ManifestRecord $file */ + return $file->isPolyfill() ? 0 : 1; + }); + + /** + * ES-module scripts must come last. + */ + usort($manifestArray, function ($file) { + /** @var \ViteHelper\Utilities\ManifestRecord $file */ + return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0; + }); + + return new ManifestRecords($manifestArray, $manifestPath); + } } diff --git a/src/View/Helper/ViteScriptsHelper.php b/src/View/Helper/ViteScriptsHelper.php index 561bb00..36327a5 100644 --- a/src/View/Helper/ViteScriptsHelper.php +++ b/src/View/Helper/ViteScriptsHelper.php @@ -6,8 +6,6 @@ use Cake\Collection\CollectionInterface; use Cake\Utility\Text; use Cake\View\Helper; -use Nette\Utils\Arrays; -use Nette\Utils\Strings; use ViteHelper\Exception\ConfigurationException; use ViteHelper\Exception\InvalidArgumentException; use ViteHelper\Utilities\ConfigDefaults; @@ -52,7 +50,7 @@ public function isDev(?ViteHelperConfig $config = null): bool $needles = $config->read('development.hostNeedles', ConfigDefaults::DEVELOPMENT_HOST_NEEDLES); foreach ($needles as $needle) { - if (Strings::contains((string)$this->getView()->getRequest()->host(), $needle)) { + if (str_contains((string)$this->getView()->getRequest()->host(), $needle)) { return true; } } @@ -241,7 +239,15 @@ private function getFilesForDevelopment(array $options, ViteHelperConfig $config . 'Be sure to set the ViteHelper.development.' . $configOption . ' config or pass entries to the helper.' ); } - if (!Arrays::isList($files)) { + + $arrayIsList = static function (mixed $value) : bool { + return is_array($value) && (PHP_VERSION_ID < 80100 + ? !$value || array_keys($value) === range(0, count($value) - 1) + : array_is_list($value) + ); + }; + + if (!$arrayIsList($files)) { throw new ConfigurationException(sprintf( 'Expected entryPoints to be a List (array with int-keys) with at least one entry, but got %s.', gettype($files) === 'array' ? 'a relational array' : gettype($files),