Theme.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Webkul\Theme;
  3. use Illuminate\Support\Facades\Vite;
  4. class Theme
  5. {
  6. /**
  7. * Contains theme parent.
  8. *
  9. * @var \Webkul\Theme\Theme
  10. */
  11. public $parent;
  12. /**
  13. * Create a new theme instance.
  14. *
  15. * @param string $code
  16. * @param string $name
  17. * @param string $assetsPath
  18. * @param string $viewsPath
  19. * @return void
  20. */
  21. public function __construct(
  22. public $code,
  23. public $name = null,
  24. public $assetsPath = null,
  25. public $viewsPath = null,
  26. public $viewsNamespace = null,
  27. public $vite = []
  28. ) {
  29. $this->assetsPath = $assetsPath === null ? $code : $assetsPath;
  30. $this->viewsPath = $viewsPath === null ? $code : $viewsPath;
  31. }
  32. /**
  33. * Sets the parent.
  34. *
  35. * @param \Webkul\Theme\Theme
  36. * @return void
  37. */
  38. public function setParent(Theme $parent)
  39. {
  40. $this->parent = $parent;
  41. }
  42. /**
  43. * Return the parent.
  44. *
  45. * @return \Webkul\Theme\Theme
  46. */
  47. public function getParent()
  48. {
  49. return $this->parent;
  50. }
  51. /**
  52. * Return all the possible view paths.
  53. *
  54. * @return array
  55. */
  56. public function getViewPaths()
  57. {
  58. $paths = [];
  59. $theme = $this;
  60. do {
  61. if (substr($theme->viewsPath, 0, 1) === DIRECTORY_SEPARATOR) {
  62. $path = base_path(substr($theme->viewsPath, 1));
  63. } else {
  64. $path = $theme->viewsPath;
  65. }
  66. if (! in_array($path, $paths)) {
  67. $paths[] = $path;
  68. }
  69. } while ($theme = $theme->parent);
  70. return $paths;
  71. }
  72. /**
  73. * Convert to asset url based on current theme.
  74. *
  75. * @return string
  76. */
  77. public function url(string $url)
  78. {
  79. try {
  80. $viteUrl = trim($this->vite['package_assets_directory'], '/').'/'.$url;
  81. return Vite::useHotFile($this->vite['hot_file'])
  82. ->useBuildDirectory($this->vite['build_directory'])
  83. ->asset($viteUrl);
  84. } catch (\Exception $e) {
  85. report($e);
  86. abort(404);
  87. }
  88. }
  89. /**
  90. * Set bagisto vite.
  91. *
  92. * @return \Illuminate\Foundation\Vite
  93. */
  94. public function setBagistoVite(array $entryPoints)
  95. {
  96. return Vite::useHotFile($this->vite['hot_file'])
  97. ->useBuildDirectory($this->vite['build_directory'])
  98. ->withEntryPoints($entryPoints);
  99. }
  100. }