Refactor CDN assets URL to separate service
[MAILPOET-3076]
This commit is contained in:
committed by
Veljko V
parent
e7206193e8
commit
6fb755eda7
@ -2,7 +2,9 @@
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Twig;
|
||||
use MailPoet\Util\CdnAssetUrl;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoetVendor\Twig\Environment as TwigEnv;
|
||||
use MailPoetVendor\Twig\Extension\DebugExtension;
|
||||
@ -72,11 +74,10 @@ class Renderer {
|
||||
public function setupGlobalVariables() {
|
||||
$this->renderer->addExtension(new Twig\Assets([
|
||||
'version' => Env::$version,
|
||||
'base_url' => Env::$baseUrl,
|
||||
'assets_url' => Env::$assetsUrl,
|
||||
'assets_manifest_js' => $this->assetsManifestJs,
|
||||
'assets_manifest_css' => $this->assetsManifestCss,
|
||||
]));
|
||||
], ContainerWrapper::getInstance()->get(CdnAssetUrl::class)));
|
||||
}
|
||||
|
||||
public function setupSyntax() {
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace MailPoet\DI;
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
||||
use MailPoetVendor\Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use MailPoetVendor\Symfony\Component\DependencyInjection\Reference;
|
||||
@ -310,6 +311,9 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\Util\Installation::class);
|
||||
$container->autowire(\MailPoet\Util\License\Features\Subscribers::class);
|
||||
$container->autowire(\MailPoet\Util\License\License::class);
|
||||
$container->register(\MailPoet\Util\CdnAssetUrl::class)
|
||||
->setPublic(true)
|
||||
->setFactory([__CLASS__, 'getCdnAssetsUrl']);
|
||||
// WooCommerce
|
||||
$container->autowire(\MailPoet\WooCommerce\Helper::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\WooCommerce\Settings::class)->setPublic(true);
|
||||
@ -348,4 +352,8 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
}
|
||||
return $container->get(IContainerConfigurator::PREMIUM_CONTAINER_SERVICE_SLUG)->get($id);
|
||||
}
|
||||
|
||||
public static function getCdnAssetsUrl(): \MailPoet\Util\CdnAssetUrl {
|
||||
return new \MailPoet\Util\CdnAssetUrl(Env::$baseUrl);
|
||||
}
|
||||
}
|
||||
|
@ -2,16 +2,24 @@
|
||||
|
||||
namespace MailPoet\Twig;
|
||||
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Util\CdnAssetUrl;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoetVendor\Twig\Extension\AbstractExtension;
|
||||
use MailPoetVendor\Twig\TwigFunction;
|
||||
|
||||
class Assets extends AbstractExtension {
|
||||
const CDN_URL = 'https://ps.w.org/mailpoet/';
|
||||
private $globals;
|
||||
|
||||
public function __construct($globals) {
|
||||
/** @var CdnAssetUrl */
|
||||
private $cdnAssetsUrl;
|
||||
|
||||
public function __construct(array $globals, CdnAssetUrl $cdnAssetsUrl = null) {
|
||||
$this->globals = $globals;
|
||||
if ($cdnAssetsUrl === null) {
|
||||
$cdnAssetsUrl = ContainerWrapper::getInstance()->get(CdnAssetUrl::class);
|
||||
}
|
||||
$this->cdnAssetsUrl = $cdnAssetsUrl;
|
||||
}
|
||||
|
||||
public function getFunctions() {
|
||||
@ -97,7 +105,6 @@ class Assets extends AbstractExtension {
|
||||
}
|
||||
|
||||
public function generateCdnUrl($path) {
|
||||
$useCdn = defined('MAILPOET_USE_CDN') ? MAILPOET_USE_CDN : true;
|
||||
return ($useCdn ? self::CDN_URL : $this->globals['base_url'] . '/plugin_repository/') . "assets/$path";
|
||||
return $this->cdnAssetsUrl->generateCdnUrl($path);
|
||||
}
|
||||
}
|
||||
|
18
lib/Util/CdnAssetUrl.php
Normal file
18
lib/Util/CdnAssetUrl.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Util;
|
||||
|
||||
class CdnAssetUrl {
|
||||
const CDN_URL = 'https://ps.w.org/mailpoet/';
|
||||
/** @var string */
|
||||
private $baseUrl;
|
||||
|
||||
public function __construct(string $baseUrl) {
|
||||
$this->baseUrl = $baseUrl;
|
||||
}
|
||||
|
||||
public function generateCdnUrl($path) {
|
||||
$useCdn = defined('MAILPOET_USE_CDN') ? MAILPOET_USE_CDN : true;
|
||||
return ($useCdn ? self::CDN_URL : $this->baseUrl . '/plugin_repository/') . "assets/$path";
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace MailPoet\Test\Twig;
|
||||
|
||||
use MailPoet\Twig\Assets;
|
||||
use MailPoet\Util\CdnAssetUrl;
|
||||
|
||||
class AssetsTest extends \MailPoetTest {
|
||||
public $assetsExtension;
|
||||
@ -19,7 +20,8 @@ class AssetsTest extends \MailPoetTest {
|
||||
'assets_manifest_js' => false,
|
||||
'assets_manifest_css' => false,
|
||||
'version' => $this->version,
|
||||
]
|
||||
],
|
||||
new CdnAssetUrl('http://localhost/')
|
||||
);
|
||||
}
|
||||
|
||||
@ -34,7 +36,8 @@ class AssetsTest extends \MailPoetTest {
|
||||
'assets_url' => $this->assetsUrl,
|
||||
'assets_manifest_js' => $manifest,
|
||||
'version' => $this->version,
|
||||
]
|
||||
],
|
||||
new CdnAssetUrl('http://localhost/')
|
||||
);
|
||||
|
||||
expect($assetsExtension->generateJavascript('script1.js', 'script2.js'))->equals(
|
||||
@ -63,7 +66,8 @@ class AssetsTest extends \MailPoetTest {
|
||||
'assets_url' => $this->assetsUrl,
|
||||
'assets_manifest_css' => $manifest,
|
||||
'version' => $this->version,
|
||||
]
|
||||
],
|
||||
new CdnAssetUrl('http://localhost/')
|
||||
);
|
||||
|
||||
expect($assetsExtension->generateStylesheet('style1.css', 'style2.css'))->equals(
|
||||
|
Reference in New Issue
Block a user