You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

  1. <?php namespace Wpstudio\AssetsManifest\Classes\Bundlers;
  2. use Wpstudio\AssetsManifest\Classes\AssetsManifestException;
  3. use Wpstudio\AssetsManifest\Classes\ManifestReader;
  4. abstract class Bundler
  5. {
  6. const BUNDLER_WEBPACK_ENCORE = 'webpack-encore';
  7. const BUNDLER_VITE = 'vite';
  8. public static array $bundlers = [
  9. self::BUNDLER_WEBPACK_ENCORE => WebpackEncoreBundler::class,
  10. self::BUNDLER_VITE => ViteBundler::class,
  11. ];
  12. protected ?ManifestReader $manifestReader;
  13. public function __construct(?ManifestReader $manifestReader = null)
  14. {
  15. if (isset($manifestReader)) {
  16. $this->manifestReader = $manifestReader;
  17. }
  18. }
  19. public function getBundlerType(): string
  20. {
  21. return array_flip(self::$bundlers)[get_class($this)];
  22. }
  23. public function validateBundlerType(string $bundlerType)
  24. {
  25. if ($this->getBundlerType() != $bundlerType) {
  26. throw new AssetsManifestException(
  27. sprintf(
  28. 'Expected bundler type is %s, but actual %s',
  29. $bundlerType,
  30. $this->getBundlerType()
  31. )
  32. );
  33. }
  34. }
  35. public function getEntrypoint(string $entrypointPathInManifest): array
  36. {
  37. return $this->manifestReader->get($entrypointPathInManifest);
  38. }
  39. }