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

Introduce Twig function asset() #1175

Closed
Closed
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
2 changes: 2 additions & 0 deletions classes/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function __construct(UpgradeContainer $upgradeContainer)
*/
public function handle(Request $request)
{
$this->upgradeContainer->setRequest($request);

$routeName = $request->query->get('route') ?? Routes::HOME_PAGE;
$redirected = $request->query->get('_redirected') === '1';

Expand Down
55 changes: 55 additions & 0 deletions classes/Twig/AssetFunctionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\AutoUpgrade\Twig;

use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Twig_Extension;
use Twig_SimpleFunction;

class AssetFunctionExtension extends Twig_Extension
{
/** @var UpgradeContainer */
private $upgradeContainer;

public function __construct(
UpgradeContainer $upgradeContainer
) {
// We set the whole container because some of the variables we need
// will be set later in the script execution.
$this->upgradeContainer = $upgradeContainer;
}

public function getFunctions(): array
{
return [
new Twig_SimpleFunction('asset', [$this, 'asset']),
];
}

/**
* @param string $asset
*/
public function asset(string $asset): string
{
return $this->upgradeContainer->getAssetsEnvironment()->getAssetsBaseUrl($this->upgradeContainer->getRequest()) . '/' . $asset;
}
}
58 changes: 58 additions & 0 deletions classes/Twig/AssetFunctionExtension3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\AutoUpgrade\Twig;

use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AssetFunctionExtension3 extends AbstractExtension
{
/** @var UpgradeContainer */
private $upgradeContainer;

public function __construct(
UpgradeContainer $upgradeContainer
) {
// We set the whole container because some of the variables we need
// will be set later in the script execution.
$this->upgradeContainer = $upgradeContainer;
}

/**
* @return TwigFunction[]
*/
public function getFunctions(): array
{
return [
new TwigFunction('asset', [$this, 'asset']),
];
}

/**
* @param string $asset
*/
public function asset(string $asset): string
{
return $this->upgradeContainer->getAssetsEnvironment()->getAssetsBaseUrl($this->upgradeContainer->getRequest()) . '/' . $asset;
}
}
18 changes: 18 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
use PrestaShop\Module\AutoUpgrade\State\RestoreState;
use PrestaShop\Module\AutoUpgrade\State\UpdateState;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;
use PrestaShop\Module\AutoUpgrade\Twig\AssetFunctionExtension;
use PrestaShop\Module\AutoUpgrade\Twig\AssetFunctionExtension3;
use PrestaShop\Module\AutoUpgrade\Twig\AssetsEnvironment;
use PrestaShop\Module\AutoUpgrade\Twig\TransFilterExtension;
use PrestaShop\Module\AutoUpgrade\Twig\TransFilterExtension3;
Expand All @@ -66,6 +68,7 @@
use PrestaShop\Module\AutoUpgrade\Xml\FileLoader;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\FilesystemLoader;
Expand Down Expand Up @@ -203,6 +206,9 @@ class UpgradeContainer
/** @var PhpVersionResolverService */
private $phpVersionResolverService;

/** @var Request */
private $request;

/** @var DistributionApiService */
private $distributionApiService;

Expand Down Expand Up @@ -664,13 +670,15 @@ public function getTwig()
$loader->addPath(realpath(__DIR__ . '/..') . '/views/templates', 'ModuleAutoUpgrade');
$twig = new Environment($loader);
$twig->addExtension(new TransFilterExtension3($this->getTranslator()));
$twig->addExtension(new AssetFunctionExtension3($this));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the container as a parameter triggers the autowiring on the storybook side. Because of its string parameters, it cannot complete and makes the startup fail.

} else {
// We use Twig 1
// Using independant template engine for 1.6 & 1.7 compatibility
$loader = new Twig_Loader_Filesystem();
$loader->addPath(realpath(__DIR__ . '/..') . '/views/templates', 'ModuleAutoUpgrade');
$twig = new Twig_Environment($loader);
$twig->addExtension(new TransFilterExtension($this->getTranslator()));
$twig->addExtension(new AssetFunctionExtension($this));
}

$this->twig = $twig;
Expand Down Expand Up @@ -767,6 +775,16 @@ public function getPhpVersionResolverService(): PhpVersionResolverService
return $this->phpVersionResolverService;
}

public function getRequest(): ?Request
{
return $this->request;
}

public function setRequest(Request $request): void
{
$this->request = $request;
}

/**
* @throws Exception
*/
Expand Down
3 changes: 0 additions & 3 deletions controllers/admin/self-managed/Error404Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ protected function getPageTemplate(): string
protected function getParams(): array
{
return [
// TODO: assets_base_path is provided by all controllers. What about a asset() twig function instead?
'assets_base_path' => $this->upgradeContainer->getAssetsEnvironment()->getAssetsBaseUrl($this->request),

'error_code' => Response::HTTP_NOT_FOUND,

'exit_to_shop_admin' => $this->upgradeContainer->getUrlGenerator()->getShopAdminAbsolutePathFromRequest($this->request),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public function submitUpdate(): JsonResponse
'dialogId' => 'dialog-confirm-update',

'form_route_to_confirm' => Routes::UPDATE_STEP_BACKUP_CONFIRM_UPDATE,

// TODO: assets_base_path is provided by all controllers. What about a asset() twig function instead?
'assets_base_path' => $this->upgradeContainer->getAssetsEnvironment()->getAssetsBaseUrl($this->request),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ protected function getParams(): array
'dev_doc_upgrade_web_url' => DocumentationLinks::DEV_DOC_UPGRADE_WEB_URL,
'up_to_date' => !$isNewerVersionAvailableOnline,
'no_local_archive' => !$this->upgradeContainer->getLocalArchiveRepository()->hasLocalArchive(),
// TODO: assets_base_path is provided by all controllers. What about a asset() twig function instead?
'assets_base_path' => $this->upgradeContainer->getAssetsEnvironment()->getAssetsBaseUrl($this->request),
'current_prestashop_version' => $this->getPsVersion(),
'current_php_version' => VersionUtils::getHumanReadableVersionOf(PHP_VERSION_ID),
'local_archives' => [
Expand Down
6 changes: 1 addition & 5 deletions storybook/stories/components/Dialog.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ export default {
excludeStories: ["Default"],
};

export const Default = {
args: {
assets_base_path: "",
},
};
export const Default = {};
1 change: 0 additions & 1 deletion storybook/stories/layouts/Error404.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export default {
args: {
psBaseUri: "/",
error_code: "404",
assets_base_path: "",

exit_to_shop_admin: '#',
exit_to_app_home: '#',
Expand Down
1 change: 0 additions & 1 deletion storybook/stories/layouts/Error500.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export default {
args: {
psBaseUri: "/",
error_code: "500",
assets_base_path: "",
}
};

Expand Down
1 change: 0 additions & 1 deletion storybook/stories/pages/VersionChoice.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const VersionChoice = {
no_local_archive: true,
current_prestashop_version: "8.1.6",
current_php_version: "8.1",
assets_base_path: "",
step_parent_id: "ua_container",
stepper_parent_id: "stepper_content",
radio_card_online_parent_id: "radio_card_online",
Expand Down
2 changes: 1 addition & 1 deletion views/templates/dialogs/dialog-update.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
>
<img
class="dialog__rocket-icon rocket-icon"
src="{{ assets_base_path }}/img/rocket_white.svg"
src="{{ asset('img/rocket_white.svg') }}"
width="20"
height="20"
alt=""
Expand Down
2 changes: 1 addition & 1 deletion views/templates/steps/version-choice.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<div class="up-to-date {% if no_local_archive %} up-to-date--no-archive {% endif %}">
{% if no_local_archive %}
<img class="up-to-date__img"
src="{{ assets_base_path }}/img/up_to_date.svg"
src="{{ asset('img/up_to_date.svg') }}"
width="243"
height="203"
alt=""
Expand Down