Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MC-19247: Broken translations with advanced bundling #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 0 additions & 111 deletions Magento_BundleConfig/Block/Html/Head/Config.php

This file was deleted.

122 changes: 122 additions & 0 deletions Magento_BundleConfig/ViewModel/PageSpecificBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\BundleConfig\ViewModel;

use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\View\Asset\File;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\View\Asset\Repository;

/**
* View model responsible for getting page specific bundle js for layout.
*/
class PageSpecificBundle implements ArgumentInterface
{
/**
* @var Repository
*/
private $assetRepo;

/**
* @var HttpRequest
*/
private $httpRequest;

/**
* @var DirectoryList
*/
private $dir;

/**
* @var AppState
*/
private $appState;

/**
* @var File
*/
private $asset;

/**
* @param Repository $assetRepo
* @param HttpRequest $httpRequest
* @param DirectoryList $dir
* @param AppState $appState
*/
public function __construct(
Repository $assetRepo,
HttpRequest $httpRequest,
DirectoryList $dir,
AppState $appState
) {
$this->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;
}
}
116 changes: 116 additions & 0 deletions Magento_BundleConfig/ViewModel/SharedBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\BundleConfig\ViewModel;

use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\View\Asset\File;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\View\Asset\Repository;

/**
* View model responsible for getting shared bundle js for layout.
*/
class SharedBundle implements ArgumentInterface
{
/**
* @var Repository
*/
private $assetRepo;

/**
* @var $filePath
*/
private $filePath;

/**
* @var DirectoryList
*/
private $dir;

/**
* @var AppState
*/
private $appState;

/**
* @var File
*/
private $asset;

/**
* @param Repository $assetRepo
* @param DirectoryList $dir
* @param AppState $appState
* @param string $filePath
*/
public function __construct(
Repository $assetRepo,
DirectoryList $dir,
AppState $appState,
string $filePath = ''
) {
$this->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;
}
}
5 changes: 5 additions & 0 deletions Magento_BundleConfig/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<type name="Magento\Framework\View\Asset\Minification">
<plugin name="bundle_config_minification_plugin" type="Magento\BundleConfig\Plugin\View\Asset\MinificationPlugin" />
</type>
<type name="Magento\BundleConfig\ViewModel\SharedBundle">
<arguments>
<argument name="filePath" xsi:type="string">bundles/shared.js</argument>
</arguments>
</type>
</config>
16 changes: 12 additions & 4 deletions Magento_BundleConfig/view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="default_head_blocks"/>
<body>
<referenceContainer name="after.body.start">
<block class="Magento\BundleConfig\Block\Html\Head\Config" name="bundle-config" before="requirejs-config"/>
</referenceContainer>
<referenceBlock name="head.additional">
<block name="shared_bundle" template="Magento_BundleConfig::shared_bundle.phtml">
<arguments>
<argument name="sharedBundleViewModel" xsi:type="object">Magento\BundleConfig\ViewModel\SharedBundle</argument>
</arguments>
</block>
<block name="page_specific_bundle" template="Magento_BundleConfig::page_specific_bundle.phtml" after="shared_bundle">
<arguments>
<argument name="pageSpecificBundleViewModel" xsi:type="object">Magento\BundleConfig\ViewModel\PageSpecificBundle</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var \Magento\BundleConfig\ViewModel\PageSpecificBundle $viewModel */
$viewModel = $block->getData('pageSpecificBundleViewModel');
if (!$viewModel->isDeveloperModeEnabled() && $viewModel->fileExists()) {
?>
<script type="text/javascript" src="<?= /* @noEscape */$viewModel->getPageSpecificBundleUrl() ?>"></script>
<?php
}
Loading