silverstripe-webpack/app/src/Templates/WebpackTemplateProvider.php

154 lines
3.7 KiB
PHP
Raw Normal View History

2018-02-05 12:11:01 +01:00
<?php
/** @noinspection PhpUnusedPrivateFieldInspection */
2018-02-05 12:11:01 +01:00
2020-01-28 15:26:51 +01:00
/**
* Directs assets requests to Webpack server or to static files
*/
2018-04-21 06:29:32 +02:00
namespace Site\Templates;
2020-01-28 15:26:51 +01:00
use SilverStripe\Core\Manifest\ModuleManifest;
use SilverStripe\View\SSViewer;
2018-03-24 11:45:31 +01:00
use SilverStripe\View\TemplateGlobalProvider;
use SilverStripe\View\Requirements;
use SilverStripe\Control\Director;
2020-01-28 15:26:51 +01:00
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
2018-03-24 11:45:31 +01:00
2020-01-28 15:26:51 +01:00
class WebpackTemplateProvider implements TemplateGlobalProvider
2018-02-05 12:11:01 +01:00
{
2020-01-28 15:26:51 +01:00
/**
* @var int port number
*/
private static $port = 3000;
/**
* @var string host name
*/
private static $hostname = 'localhost';
/**
* @var string assets static files directory
*/
private static $dist = 'client/dist';
private static $webp = false;
2018-02-22 14:39:58 +01:00
/**
* @return array
*/
2019-12-09 12:04:12 +01:00
public static function get_template_global_variables(): array
2018-02-22 14:39:58 +01:00
{
return [
2020-01-28 15:26:51 +01:00
'WebpackDevServer' => 'isActive',
'WebpackCSS' => 'loadCSS',
'WebpackJS' => 'loadJS',
'ResourcesURL' => 'resourcesURL',
'ProjectName' => 'themeName',
2018-02-22 14:39:58 +01:00
];
}
2018-02-05 12:11:01 +01:00
2020-01-28 15:26:51 +01:00
/**
* Load CSS file
* @param $path
*/
public static function loadCSS($path): void
2018-02-05 12:11:01 +01:00
{
/*if (self::isActive()) {
2020-01-28 15:26:51 +01:00
return;
}*/
2020-01-28 15:26:51 +01:00
Requirements::css(self::_getPath($path));
2018-02-05 12:11:01 +01:00
}
2020-01-28 15:26:51 +01:00
/**
* Load JS file
* @param $path
*/
public static function loadJS($path): void
2019-04-11 00:15:29 +02:00
{
2020-04-02 05:24:55 +02:00
Requirements::javascript(self::_getPath($path));
2019-04-11 00:15:29 +02:00
}
2020-01-28 15:26:51 +01:00
public static function projectName(): string
2019-04-11 00:38:19 +02:00
{
2020-01-28 15:26:51 +01:00
return Config::inst()->get(ModuleManifest::class, 'project');
2019-04-11 00:38:19 +02:00
}
2020-01-28 15:26:51 +01:00
public static function mainTheme()
2018-09-15 00:55:42 +02:00
{
2020-01-28 15:26:51 +01:00
$themes = Config::inst()->get(SSViewer::class, 'themes');
return is_array($themes) && $themes[0] !== '$public' && $themes[0] !== '$default' ? $themes[0] : false;
2018-09-15 00:55:42 +02:00
}
2020-01-28 15:26:51 +01:00
public static function resourcesURL($link = null): string
2018-02-05 12:11:01 +01:00
{
$cfg = self::config();
if ($cfg['webp'] && !self::isActive()) {
$link = str_replace(['.png','.jpg','.jpeg'], '.webp', $link);
}
return Controller::join_links(
Director::baseURL(),
'/resources/',
self::projectName(),
$cfg['dist'],
'img',
$link
);
2018-02-22 14:39:58 +01:00
}
2020-01-28 15:26:51 +01:00
/**
* Checks if dev mode is enabled and if webpack server is online
* @return bool
*/
public static function isActive(): bool
2018-02-22 14:39:58 +01:00
{
2020-01-28 15:26:51 +01:00
$cfg = self::config();
return Director::isDev() && @fsockopen(
$cfg['HOSTNAME'],
$cfg['PORT']
);
2018-02-05 12:11:01 +01:00
}
2020-01-28 15:26:51 +01:00
protected static function _getPath($path): string
2018-02-05 12:11:01 +01:00
{
2020-01-28 15:26:51 +01:00
return self::isActive() && strpos($path, '//') === false ?
self::_toDevServerPath($path) :
2020-04-20 12:01:08 +02:00
self::toPublicPath($path);
2018-02-05 12:11:01 +01:00
}
2020-01-28 15:26:51 +01:00
protected static function _toDevServerPath($path): string
2018-02-05 12:11:01 +01:00
{
2020-01-28 15:26:51 +01:00
$cfg = self::config();
return sprintf(
'%s%s:%s/%s',
($cfg['HTTPS'] ? 'https://' : 'http://'),
2020-01-28 15:26:51 +01:00
$cfg['HOSTNAME'],
$cfg['PORT'],
basename($path)
//Controller::join_links($cfg['APPDIR'], $cfg['SRC'], basename($path))
2020-01-28 15:26:51 +01:00
);
}
2020-04-20 12:01:08 +02:00
public static function toPublicPath($path): string
2020-01-28 15:26:51 +01:00
{
$cfg = self::config();
return strpos($path, '//') === false ?
Controller::join_links(
2020-04-24 12:31:16 +02:00
RESOURCES_DIR,
2020-01-28 15:26:51 +01:00
self::projectName(),
2020-04-20 12:01:08 +02:00
$cfg['dist'],
2020-01-28 15:26:51 +01:00
(strpos($path, '.css') ? 'css' : 'js'),
$path
)
: $path;
2018-02-05 12:11:01 +01:00
}
2019-12-09 12:04:12 +01:00
public static function config(): array
{
return Config::inst()->get(__CLASS__);
}
2018-02-05 12:11:01 +01:00
}