| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Assets package. |
| 5 |
* |
| 6 |
* (c) Inpsyde GmbH |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Inpsyde\Assets\Loader; |
| 15 |
|
| 16 |
use Inpsyde\Assets\Asset; |
| 17 |
use Inpsyde\Assets\BaseAsset; |
| 18 |
use Inpsyde\Assets\ConfigureAutodiscoverVersionTrait; |
| 19 |
use Inpsyde\Assets\Exception\FileNotFoundException; |
| 20 |
use Inpsyde\Assets\Exception\InvalidResourceException; |
| 21 |
use Inpsyde\Assets\Script; |
| 22 |
use Inpsyde\Assets\Style; |
| 23 |
|
| 24 |
abstract class AbstractWebpackLoader implements LoaderInterface |
| 25 |
{ |
| 26 |
use ConfigureAutodiscoverVersionTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $directoryUrl = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param string $directoryUrl optional directory URL which will be used for the Asset |
| 35 |
* |
| 36 |
* @return static |
| 37 |
*/ |
| 38 |
public function withDirectoryUrl(string $directoryUrl): AbstractWebpackLoader |
| 39 |
{ |
| 40 |
$this->directoryUrl = $directoryUrl; |
| 41 |
|
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @param array<string, string> $data |
| 47 |
* @param string $resource |
| 48 |
* |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
abstract protected function parseData(array $data, string $resource): array; |
| 52 |
|
| 53 |
/** |
| 54 |
* @param mixed $resource |
| 55 |
* |
| 56 |
* @return array |
| 57 |
* |
| 58 |
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration |
| 59 |
* @psalm-suppress MixedArgument |
| 60 |
*/ |
| 61 |
public function load($resource): array |
| 62 |
{ |
| 63 |
if (!is_string($resource) || !is_readable($resource)) { |
| 64 |
throw new FileNotFoundException( |
| 65 |
sprintf( |
| 66 |
'The given file "%s" does not exists or is not readable.', |
| 67 |
(string) $resource |
| 68 |
) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
$data = @file_get_contents($resource) |
| 73 |
?: ''; // phpcs:ignore |
| 74 |
$data = json_decode($data, true); |
| 75 |
$errorCode = json_last_error(); |
| 76 |
if (0 < $errorCode) { |
| 77 |
throw new InvalidResourceException( |
| 78 |
sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return $this->parseData($data, $resource); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Translates JSON_ERROR_* constant into meaningful message. |
| 87 |
* |
| 88 |
* @param int $errorCode |
| 89 |
* |
| 90 |
* @return string Message string |
| 91 |
*/ |
| 92 |
private function getJSONErrorMessage(int $errorCode): string |
| 93 |
{ |
| 94 |
switch ($errorCode) { |
| 95 |
case JSON_ERROR_DEPTH: |
| 96 |
return 'Maximum stack depth exceeded'; |
| 97 |
case JSON_ERROR_STATE_MISMATCH: |
| 98 |
return 'Underflow or the modes mismatch'; |
| 99 |
case JSON_ERROR_CTRL_CHAR: |
| 100 |
return 'Unexpected control character found'; |
| 101 |
case JSON_ERROR_SYNTAX: |
| 102 |
return 'Syntax error, malformed JSON'; |
| 103 |
case JSON_ERROR_UTF8: |
| 104 |
return 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
| 105 |
default: |
| 106 |
return 'Unknown error'; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @param string $handle |
| 112 |
* @param string $fileUrl |
| 113 |
* @param string $filePath |
| 114 |
* |
| 115 |
* @return Asset|null |
| 116 |
*/ |
| 117 |
protected function buildAsset(string $handle, string $fileUrl, string $filePath): ?Asset |
| 118 |
{ |
| 119 |
$extensionsToClass = [ |
| 120 |
'css' => Style::class, |
| 121 |
'js' => Script::class, |
| 122 |
]; |
| 123 |
|
| 124 |
/** @var array{filename?:string, extension?:string} $pathInfo */ |
| 125 |
$pathInfo = pathinfo($filePath); |
| 126 |
$filename = $pathInfo['filename'] ?? ''; |
| 127 |
$extension = $pathInfo['extension'] ?? ''; |
| 128 |
|
| 129 |
if (!in_array($extension, array_keys($extensionsToClass), true)) { |
| 130 |
return null; |
| 131 |
} |
| 132 |
|
| 133 |
$class = $extensionsToClass[$extension]; |
| 134 |
|
| 135 |
/** @var Asset|BaseAsset $asset */ |
| 136 |
$asset = new $class($handle, $fileUrl, $this->resolveLocation($filename)); |
| 137 |
$asset->withFilePath($filePath); |
| 138 |
$asset->canEnqueue(true); |
| 139 |
|
| 140 |
if ($asset instanceof BaseAsset) { |
| 141 |
$this->autodiscoverVersion |
| 142 |
? $asset->enableAutodiscoverVersion() |
| 143 |
: $asset->disableAutodiscoverVersion(); |
| 144 |
} |
| 145 |
|
| 146 |
return $asset; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* The "file"-value can contain: |
| 151 |
* - URL |
| 152 |
* - Path to current folder |
| 153 |
* - Absolute path |
| 154 |
* |
| 155 |
* We try to build a clean path which will be appended to the directoryPath or urlPath. |
| 156 |
* |
| 157 |
* @param string $file |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
protected function sanitizeFileName(string $file): string |
| 162 |
{ |
| 163 |
// Check, if the given "file"-value is an URL |
| 164 |
$parsedUrl = parse_url($file); |
| 165 |
|
| 166 |
// the "file"-value can contain "./file.css" or "/file.css". |
| 167 |
|
| 168 |
return ltrim($parsedUrl['path'] ?? $file, './'); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Internal function to resolve a location for a given file name. |
| 173 |
* |
| 174 |
* @param string $fileName |
| 175 |
* |
| 176 |
* @return int |
| 177 |
* |
| 178 |
* @example foo-customizer.css -> Asset::CUSTOMIZER |
| 179 |
* @example foo-block.css -> Asset::BLOCK_EDITOR_ASSETS |
| 180 |
* @example foo-login.css -> Asset::LOGIN |
| 181 |
* @example foo.css -> Asset::FRONTEND |
| 182 |
* @example foo-backend.css -> Asset::BACKEND |
| 183 |
*/ |
| 184 |
protected function resolveLocation(string $fileName): int |
| 185 |
{ |
| 186 |
if (stristr($fileName, '-backend')) { |
| 187 |
return Asset::BACKEND; |
| 188 |
} |
| 189 |
|
| 190 |
if (stristr($fileName, '-block')) { |
| 191 |
return Asset::BLOCK_EDITOR_ASSETS; |
| 192 |
} |
| 193 |
|
| 194 |
if (stristr($fileName, '-login')) { |
| 195 |
return Asset::LOGIN; |
| 196 |
} |
| 197 |
|
| 198 |
if (stristr($fileName, '-customizer-preview')) { |
| 199 |
return Asset::CUSTOMIZER_PREVIEW; |
| 200 |
} |
| 201 |
|
| 202 |
if (stristr($fileName, '-customizer')) { |
| 203 |
return Asset::CUSTOMIZER; |
| 204 |
} |
| 205 |
|
| 206 |
return Asset::FRONTEND; |
| 207 |
} |
| 208 |
} |
| 209 |
|