diff --git a/Magento_BundleConfig/Block/Html/Head/Config.php b/Magento_BundleConfig/Block/Html/Head/Config.php deleted file mode 100644 index 3fa9236..0000000 --- a/Magento_BundleConfig/Block/Html/Head/Config.php +++ /dev/null @@ -1,111 +0,0 @@ -fileManager = $fileManager; - $this->pageConfig = $pageConfig; - $this->appState = $appState; - $this->dir = $dir; - $this->httpRequest = $httpRequest; - } - - /** - * Include specified AMD bundle as an asset on the page - * - * @return \Magento\Framework\View\Element\AbstractBlock - */ - protected function _prepareLayout() - { - if ($this->appState->getMode() === AppState::MODE_DEVELOPER) { - return parent::_prepareLayout(); - } - - $staticDir = $this->dir->getPath('static'); - $fullActionName = $this->httpRequest->getFullActionName(); - - $assetCollection = $this->pageConfig->getAssetCollection(); - - $shared = $this->fileManager->createSharedJsBundleAsset(); - $sharedBundleRelPath = $shared->getFilePath(); - $sharedBundleAbsPath = $staticDir . "/" . $sharedBundleRelPath; - - if (file_exists($sharedBundleAbsPath)) { - $assetCollection->insert( - $sharedBundleRelPath, - $shared, - RequireJsConfig::REQUIRE_JS_FILE_NAME - ); - } - - $bundleConfig = $this->fileManager->createJsBundleAsset($fullActionName); - $pageSpecificBundleRelPath = $bundleConfig->getFilePath(); - $pageSpecificBundleAbsPath = $staticDir . "/" . $pageSpecificBundleRelPath; - - if (file_exists($pageSpecificBundleAbsPath)) { - $assetCollection->insert( - $pageSpecificBundleRelPath, - $bundleConfig, - $sharedBundleRelPath - ); - } - - return parent::_prepareLayout(); - } -} diff --git a/Magento_BundleConfig/ViewModel/PageSpecificBundle.php b/Magento_BundleConfig/ViewModel/PageSpecificBundle.php new file mode 100644 index 0000000..6254ca3 --- /dev/null +++ b/Magento_BundleConfig/ViewModel/PageSpecificBundle.php @@ -0,0 +1,122 @@ +assetRepo = $assetRepo; + $this->httpRequest = $httpRequest; + $this->dir = $dir; + $this->appState = $appState; + } + + /** + * Return page specific bundle asset url. + * + * @return string + * @throws LocalizedException + */ + public function getPageSpecificBundleUrl() + { + $this->asset = $this->asset ?? $this->getAsset(); + + return $this->asset->getUrl(); + } + + /** + * Check if page specific bundle asset exists. + * + * @return bool + * @throws LocalizedException + * @throws \Magento\Framework\Exception\FileSystemException + */ + public function fileExists() + { + $this->asset = $this->asset ?? $this->getAsset(); + + $staticDir = $this->dir->getPath('static'); + + $pageSpecificBundleRelPath = $this->asset->getPath(); + $pageSpecificBundleAbsPath = $staticDir . "/" . $pageSpecificBundleRelPath; + + return file_exists($pageSpecificBundleAbsPath); + } + + /** + * Get page specific bundle asset. + * + * @return File + * @throws LocalizedException + */ + private function getAsset() + { + $fullActionName = $this->httpRequest->getFullActionName(); + + $formattedActionName = str_replace("_", "-", $fullActionName); + $filePath = 'bundles/' . $formattedActionName . '.js'; + + return $this->assetRepo->createAsset($filePath); + } + + /** + * Check if developer mode is enabled. + * + * @return bool + */ + public function isDeveloperModeEnabled() + { + return $this->appState->getMode() === AppState::MODE_DEVELOPER; + } +} diff --git a/Magento_BundleConfig/ViewModel/SharedBundle.php b/Magento_BundleConfig/ViewModel/SharedBundle.php new file mode 100644 index 0000000..18c6639 --- /dev/null +++ b/Magento_BundleConfig/ViewModel/SharedBundle.php @@ -0,0 +1,116 @@ +assetRepo = $assetRepo; + $this->dir = $dir; + $this->appState = $appState; + $this->filePath = $filePath; + } + + /** + * Return shared bundle asset url. + * + * @return string + * @throws LocalizedException + */ + public function getSharedBundleUrl() + { + $this->asset = $this->asset ?? $this->getAsset(); + + return $this->asset->getUrl(); + } + + /** + * Check if shared bundle asset exists. + * + * @return bool + * @throws LocalizedException + * @throws \Magento\Framework\Exception\FileSystemException + */ + public function fileExists() + { + $this->asset = $this->asset ?? $this->getAsset(); + + $staticDir = $this->dir->getPath('static'); + + $sharedBundleRelPath = $this->asset->getPath(); + $sharedBundleAbsPath = $staticDir . "/" . $sharedBundleRelPath; + + return file_exists($sharedBundleAbsPath); + } + + /** + * Get shared bundle asset. + * + * @return File + * @throws LocalizedException + */ + private function getAsset() + { + return $this->assetRepo->createAsset($this->filePath); + } + + /** + * Check if developer mode is enabled. + * + * @return bool + */ + public function isDeveloperModeEnabled() + { + return $this->appState->getMode() === AppState::MODE_DEVELOPER; + } +} diff --git a/Magento_BundleConfig/etc/di.xml b/Magento_BundleConfig/etc/di.xml index ba4092c..ba42146 100644 --- a/Magento_BundleConfig/etc/di.xml +++ b/Magento_BundleConfig/etc/di.xml @@ -9,4 +9,9 @@ + + + bundles/shared.js + + diff --git a/Magento_BundleConfig/view/frontend/layout/default.xml b/Magento_BundleConfig/view/frontend/layout/default.xml index 8be69df..2eae4c9 100644 --- a/Magento_BundleConfig/view/frontend/layout/default.xml +++ b/Magento_BundleConfig/view/frontend/layout/default.xml @@ -6,10 +6,18 @@ */ --> - - - - + + + + Magento\BundleConfig\ViewModel\SharedBundle + + + + + Magento\BundleConfig\ViewModel\PageSpecificBundle + + + diff --git a/Magento_BundleConfig/view/frontend/templates/page_specific_bundle.phtml b/Magento_BundleConfig/view/frontend/templates/page_specific_bundle.phtml new file mode 100644 index 0000000..49a3fb1 --- /dev/null +++ b/Magento_BundleConfig/view/frontend/templates/page_specific_bundle.phtml @@ -0,0 +1,13 @@ +getData('pageSpecificBundleViewModel'); +if (!$viewModel->isDeveloperModeEnabled() && $viewModel->fileExists()) { + ?> + + getData('sharedBundleViewModel'); +if (!$viewModel->isDeveloperModeEnabled() && $viewModel->fileExists()) { + ?> + +