Skip to content

Commit c692190

Browse files
committed
remove dependencies Nette\Finder and `Nette\Utils
1 parent 6028e07 commit c692190

4 files changed

Lines changed: 66 additions & 57 deletions

File tree

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"license": "MIT",
66
"require": {
77
"php": ">=8.0",
8-
"cakephp/cakephp": "^4.2.0",
9-
"nette/finder": "^2.5",
10-
"nette/utils": "^v3.0.0"
8+
"cakephp/cakephp": "^4.2.0"
119
},
1210
"require-dev": {
1311
"phpunit/phpunit": "^8.5 || ^9.3",

src/Utilities/ManifestRecord.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace ViteHelper\Utilities;
55

6-
use Nette\Utils\Strings;
76
use stdClass;
87

98
class ManifestRecord
@@ -75,7 +74,7 @@ public function match(string $needle, string $property = 'src'): bool
7574
{
7675
$field = $this->getChunk($property);
7776

78-
return is_string($field) && Strings::contains($field, $needle);
77+
return is_string($field) && str_contains($field, $needle);
7978
}
8079

8180
/**
@@ -121,7 +120,7 @@ public function matchAll(array $names, string $property = 'src'): bool
121120
*/
122121
public function isLegacy(): bool
123122
{
124-
return Strings::contains($this->chunk->file, 'legacy');
123+
return str_contains($this->chunk->file, 'legacy');
125124
}
126125

127126
/**
@@ -131,7 +130,7 @@ public function isLegacy(): bool
131130
*/
132131
public function isPolyfill(): bool
133132
{
134-
return Strings::contains($this->chunk->file, 'polyfills');
133+
return str_contains($this->chunk->file, 'polyfills');
135134
}
136135

137136
/**
@@ -141,7 +140,7 @@ public function isPolyfill(): bool
141140
*/
142141
public function isJavascript(): bool
143142
{
144-
return Strings::endsWith($this->chunk->file, '.js');
143+
return str_ends_with($this->chunk->file, '.js');
145144
}
146145

147146
/**
@@ -151,7 +150,7 @@ public function isJavascript(): bool
151150
*/
152151
public function isStylesheet(): bool
153152
{
154-
return Strings::endsWith((string)$this->chunk->file, '.css');
153+
return str_ends_with((string)$this->chunk->file, '.css');
155154
}
156155

157156
/**

src/Utilities/ViteManifest.php

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,68 @@
33

44
namespace ViteHelper\Utilities;
55

6-
use Nette\Utils\FileSystem;
7-
use Nette\Utils\Json;
86
use ViteHelper\Exception\ManifestNotFoundException;
97

108
/**
119
* Reads the information in the manifest.json file provided by ViteJs after running 'vite build'
1210
*/
1311
class ViteManifest
1412
{
15-
/**
16-
* Returns the manifest records as a Collection
17-
*
18-
* @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance
19-
* @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[]
20-
* @throws \ViteHelper\Exception\ManifestNotFoundException
21-
* @internal
22-
*/
23-
public static function getRecords(ViteHelperConfig $config): ManifestRecords
24-
{
25-
$manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST);
13+
/**
14+
* Returns the manifest records as a Collection
15+
*
16+
* @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance
17+
* @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[]
18+
* @throws \ViteHelper\Exception\ManifestNotFoundException
19+
* @internal
20+
*/
21+
public static function getRecords(ViteHelperConfig $config): ManifestRecords
22+
{
23+
$manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST);
2624

27-
try {
28-
$json = FileSystem::read($manifestPath);
25+
if (!is_readable($manifestPath)) {
26+
throw new ManifestNotFoundException(
27+
"No valid manifest.json found at path {$manifestPath}. Did you build your js?",
28+
);
29+
}
2930

30-
$json = str_replace([
31-
"\u0000",
32-
], '', $json);
31+
$json = @file_get_contents($manifestPath);
3332

34-
$manifest = Json::decode($json);
35-
} catch (\Exception $e) {
36-
throw new ManifestNotFoundException(
37-
"No valid manifest.json found at path {$manifestPath}. Did you build your js? Error: {$e->getMessage()}"
38-
);
39-
}
33+
if ($json === false) {
34+
throw new ManifestNotFoundException('Could not parse manifest.json');
35+
}
4036

41-
$manifestArray = [];
42-
foreach (get_object_vars($manifest) as $property => $value) {
43-
$manifestArray[$property] = new ManifestRecord($property, $value, $config);
44-
}
37+
$json = str_replace(
38+
[
39+
"\u0000",
40+
],
41+
'',
42+
$json
43+
);
4544

46-
/**
47-
* Legacy Polyfills must come first.
48-
*/
49-
usort($manifestArray, function ($file) {
50-
/** @var \ViteHelper\Utilities\ManifestRecord $file */
51-
return $file->isPolyfill() ? 0 : 1;
52-
});
45+
$manifest = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
5346

54-
/**
55-
* ES-module scripts must come last.
56-
*/
57-
usort($manifestArray, function ($file) {
58-
/** @var \ViteHelper\Utilities\ManifestRecord $file */
59-
return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0;
60-
});
47+
$manifestArray = [];
48+
foreach (get_object_vars($manifest) as $property => $value) {
49+
$manifestArray[$property] = new ManifestRecord($property, $value, $config);
50+
}
6151

62-
return new ManifestRecords($manifestArray, $manifestPath);
63-
}
52+
/**
53+
* Legacy Polyfills must come first.
54+
*/
55+
usort($manifestArray, function ($file) {
56+
/** @var \ViteHelper\Utilities\ManifestRecord $file */
57+
return $file->isPolyfill() ? 0 : 1;
58+
});
59+
60+
/**
61+
* ES-module scripts must come last.
62+
*/
63+
usort($manifestArray, function ($file) {
64+
/** @var \ViteHelper\Utilities\ManifestRecord $file */
65+
return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0;
66+
});
67+
68+
return new ManifestRecords($manifestArray, $manifestPath);
69+
}
6470
}

src/View/Helper/ViteScriptsHelper.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Cake\Collection\CollectionInterface;
77
use Cake\Utility\Text;
88
use Cake\View\Helper;
9-
use Nette\Utils\Arrays;
10-
use Nette\Utils\Strings;
119
use ViteHelper\Exception\ConfigurationException;
1210
use ViteHelper\Exception\InvalidArgumentException;
1311
use ViteHelper\Utilities\ConfigDefaults;
@@ -52,7 +50,7 @@ public function isDev(?ViteHelperConfig $config = null): bool
5250

5351
$needles = $config->read('development.hostNeedles', ConfigDefaults::DEVELOPMENT_HOST_NEEDLES);
5452
foreach ($needles as $needle) {
55-
if (Strings::contains((string)$this->getView()->getRequest()->host(), $needle)) {
53+
if (str_contains((string)$this->getView()->getRequest()->host(), $needle)) {
5654
return true;
5755
}
5856
}
@@ -241,7 +239,15 @@ private function getFilesForDevelopment(array $options, ViteHelperConfig $config
241239
. 'Be sure to set the ViteHelper.development.' . $configOption . ' config or pass entries to the helper.'
242240
);
243241
}
244-
if (!Arrays::isList($files)) {
242+
243+
$arrayIsList = static function (mixed $value) : bool {
244+
return is_array($value) && (PHP_VERSION_ID < 80100
245+
? !$value || array_keys($value) === range(0, count($value) - 1)
246+
: array_is_list($value)
247+
);
248+
};
249+
250+
if (!$arrayIsList($files)) {
245251
throw new ConfigurationException(sprintf(
246252
'Expected entryPoints to be a List (array with int-keys) with at least one entry, but got %s.',
247253
gettype($files) === 'array' ? 'a relational array' : gettype($files),

0 commit comments

Comments
 (0)