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.

45 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)
  14. {
  15. $this->manifestReader = $manifestReader;
  16. }
  17. public function getBundlerType(): string
  18. {
  19. return array_flip(self::$bundlers)[get_class($this)];
  20. }
  21. public function validateBundlerType(string $bundlerType)
  22. {
  23. if ($this->getBundlerType() != $bundlerType) {
  24. throw new AssetsManifestException(
  25. sprintf(
  26. 'Expected bundler type is %s, but actual %s',
  27. $bundlerType,
  28. $this->getBundlerType()
  29. )
  30. );
  31. }
  32. }
  33. public function getEntrypoint(string $entrypointPathInManifest): array
  34. {
  35. return $this->manifestReader->get($entrypointPathInManifest);
  36. }
  37. }