Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
21c9b68
Added initial support for allowing a theme to extend a parent theme
jaxwilko Oct 6, 2022
0a4aeea
Added an assetUrl helper for theme
jaxwilko Oct 6, 2022
a95b14c
Switched asset url generation to rely on theme assetUrl method
jaxwilko Oct 6, 2022
c49562e
Added early exit if theme directory not exists
jaxwilko Oct 6, 2022
ef95051
Added fix for loading items at the top of the filesystem
jaxwilko Oct 6, 2022
1bb81ac
Switched order of execution
jaxwilko Oct 7, 2022
4a71c75
Support child themes referencing their parent theme's localization st…
LukeTowers Oct 10, 2022
991717a
Add initial support for child themes referencing combined assets in p…
LukeTowers Oct 19, 2022
527fa69
Avoid cache conflicts when running multiple themes
LukeTowers Oct 19, 2022
a18e3ed
Remove unintentional debugging statement
LukeTowers Oct 19, 2022
0a9f21d
Avoid Twig cache conflicts when running multiple themes
LukeTowers Oct 19, 2022
222b60b
Added file detection allowing per file based theme overrides
jaxwilko Oct 19, 2022
966edea
Set pivot data when initially syncing the relationship
LukeTowers Oct 24, 2022
83420eb
Merge commit '966edea734a0e330f19ebe2547a1332ed39cb907' into wip/chil…
LukeTowers Oct 24, 2022
5cee4bf
Merge branch 'develop' into wip/child-themes-luke
LukeTowers Oct 26, 2022
2b140f4
Improve databaseLayerEnabled() check
LukeTowers Oct 26, 2022
3dccee5
Coding style fix
LukeTowers Oct 26, 2022
b79c58f
Merge branch 'develop' into wip/child-theme
LukeTowers Oct 26, 2022
7fd94b0
Merge branch 'wip/child-theme' into wip/child-themes-luke
LukeTowers Oct 26, 2022
fa822ba
Merge pull request #732 from wintercms/wip/child-themes-luke
LukeTowers Oct 26, 2022
ef7a0b7
Merge branch 'develop' into wip/child-theme
LukeTowers Oct 26, 2022
9dae851
Merge branch 'develop' into wip/child-theme
LukeTowers Oct 26, 2022
6673c15
Merge branch 'develop' into wip/child-theme
LukeTowers Oct 26, 2022
8a64b1e
Include parent theme in twig caching path
LukeTowers Oct 26, 2022
f3efa93
Merge branch 'develop' into wip/child-theme
jaxwilko Oct 28, 2022
8ecf401
Moved backend localzation into theme activation to allow support for …
jaxwilko Nov 3, 2022
fa35ede
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 9, 2022
2523b40
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 9, 2022
8cf656d
Improve performance of active theme resolution, add type hinting
LukeTowers Nov 15, 2022
94d4e19
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 15, 2022
03b4eff
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 16, 2022
6c90ba8
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 17, 2022
7a215c7
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 23, 2022
9b035c2
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 23, 2022
62d4880
Merge branch 'develop' into wip/child-theme
LukeTowers Nov 23, 2022
3d77c7a
Add ability to specify the cache key of the AutoDatasource
LukeTowers Nov 24, 2022
4f0da6c
Set the AutoDatasource's cache key based on the current theme
LukeTowers Nov 24, 2022
5540dad
Added fix to allow nested AutoDatasource instances to manage their ow…
jaxwilko Dec 8, 2022
fe5f754
Merge branch 'develop' into wip/child-theme-autodatasource-caching
LukeTowers Dec 8, 2022
cfb02c4
Merge pull request #791 from wintercms/wip/child-theme-autodatasource…
LukeTowers Dec 13, 2022
5414c62
Added support to the theme filter to validate parent theme for assets
jaxwilko Feb 24, 2023
1a8fb44
Added Carbon cache timer
jaxwilko Feb 27, 2023
49d7ea0
Removed whitespace
jaxwilko Feb 27, 2023
ad3e19e
Update modules/cms/classes/Controller.php
LukeTowers Feb 27, 2023
02319de
Update modules/cms/classes/Controller.php
LukeTowers Feb 27, 2023
3c393b7
Update modules/cms/classes/Controller.php
LukeTowers Feb 27, 2023
85fe239
Update modules/cms/widgets/AssetList.php
LukeTowers Feb 27, 2023
a9e2e01
Update modules/cms/classes/Theme.php
LukeTowers Feb 27, 2023
3febe41
Merge branch 'develop' into wip/child-theme
jaxwilko Feb 27, 2023
c91fe4f
Apply suggestions from code review
LukeTowers Feb 27, 2023
263a1a3
Removed undefined var and replaced with halycon registered event
jaxwilko Feb 27, 2023
af37464
Added child theme tests
jaxwilko Mar 1, 2023
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
35 changes: 12 additions & 23 deletions modules/cms/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ public function boot()

$this->bootMenuItemEvents();
$this->bootRichEditorEvents();

if ($this->app->runningInBackend()) {
$this->bootBackendLocalization();
}
}

/**
Expand Down Expand Up @@ -109,8 +105,18 @@ protected function registerTwigParser()
];

if ($useCache) {
$theme = Theme::getActiveTheme();
$themeDir = $theme->getDirName();
if ($parent = $theme->getConfig()['parent'] ?? false) {
$themeDir .= '-' . $parent;
}

$options['cache'] = new TwigCacheFilesystem(
storage_path().'/cms/twig',
storage_path(implode(DIRECTORY_SEPARATOR, [
'cms',
'twig',
$themeDir,
])) . DIRECTORY_SEPARATOR,
$forceBytecode ? TwigCacheFilesystem::FORCE_BYTECODE_INVALIDATION : 0
);
}
Expand Down Expand Up @@ -381,24 +387,6 @@ protected function registerBackendSettings()
});
}

/**
* Boots localization from an active theme for backend items.
*/
protected function bootBackendLocalization()
{
$theme = Theme::getActiveTheme();

if (is_null($theme)) {
return;
}

$langPath = $theme->getPath() . '/lang';

if (File::isDirectory($langPath)) {
Lang::addNamespace('themes.' . $theme->getId(), $langPath);
}
}

/**
* Registers events for menu items.
*/
Expand Down Expand Up @@ -448,6 +436,7 @@ protected function registerHalcyonModels()
{
Event::listen('system.console.theme.sync.getAvailableModelClasses', function () {
return [
Classes\Theme::class,
Classes\Meta::class,
Classes\Page::class,
Classes\Layout::class,
Expand Down
30 changes: 27 additions & 3 deletions modules/cms/classes/AutoDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class AutoDatasource extends Datasource implements DatasourceInterface
*/
protected $datasources = [];

/**
* @var string The cache key to use for this datasource instance
*/
protected $cacheKey = 'halcyon-datastore-auto';

/**
* @var array Local cache of paths available in the datasources
*/
Expand All @@ -48,17 +53,29 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param array $datasources Array of datasources to utilize. Lower indexes = higher priority ['datasourceName' => $datasource]
* @return void
*/
public function __construct(array $datasources)
public function __construct(array $datasources, ?string $cacheKey = null)
{
$this->datasources = $datasources;

if ($cacheKey) {
$this->cacheKey = $cacheKey;
}

$this->activeDatasourceKey = array_keys($datasources)[0];

$this->populateCache();

$this->postProcessor = new Processor;
}

/**
* Returns the in memory path cache map
*/
public function getPathCache(): array
{
return $this->pathCache;
}

/**
* Populate the local cache of paths available in each datasource
*
Expand All @@ -69,6 +86,13 @@ public function populateCache($refresh = false)
{
$pathCache = [];
foreach ($this->datasources as $datasource) {
// Allow AutoDatasource instances to handle their own internal caching
if ($datasource instanceof AutoDatasource) {
$datasource->populateCache($refresh);
$pathCache[] = array_merge(...array_reverse($datasource->getPathCache()));
continue;
}

// Remove any existing cache data
if ($refresh && $this->allowCacheRefreshes) {
Cache::forget($datasource->getPathsCacheKey());
Expand Down Expand Up @@ -309,7 +333,7 @@ protected function getValidPaths(string $dirName, array $options = [])
*/
protected function makeFilePath(string $dirName, string $fileName, string $extension)
{
return $dirName . '/' . $fileName . '.' . $extension;
return ltrim($dirName . '/' . $fileName . '.' . $extension, '/');
}

/**
Expand Down Expand Up @@ -507,7 +531,7 @@ public function makeCacheKey($name = ''): string
*/
public function getPathsCacheKey(): string
{
return 'halcyon-datastore-auto';
return $this->cacheKey;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/classes/CmsObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function loadCached($theme, $fileName)
* This method is used internally by the system.
* @param \Cms\Classes\Theme $theme Specifies a parent theme.
* @param boolean $skipCache Indicates if objects should be reloaded from the disk bypassing the cache.
* @return Collection Returns a collection of CMS objects.
* @return CmsObjectCollection Returns a collection of CMS objects.
*/
public static function listInTheme($theme, $skipCache = false)
{
Expand Down
23 changes: 14 additions & 9 deletions modules/cms/classes/CodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cache;
use Config;
use SystemException;
use Winter\Storm\Support\Str;

/**
* Parses the PHP code section of CMS objects.
Expand Down Expand Up @@ -233,18 +234,22 @@ protected function storeCachedInfo($result)

/**
* Returns path to the cached parsed file
* @return string
*/
protected function getCacheFilePath()
protected function getCacheFilePath(): string
{
$hash = md5($this->filePath);
$result = storage_path().'/cms/cache/';
$result .= substr($hash, 0, 2).'/';
$result .= substr($hash, 2, 2).'/';
$result .= basename($this->filePath);
$result .= '.php';
$pathSegments = [
storage_path('cms' . DIRECTORY_SEPARATOR . 'cache'),
trim(
Str::after(
pathinfo($this->filePath, PATHINFO_DIRNAME),
base_path()
),
DIRECTORY_SEPARATOR
),
basename($this->filePath) . '.php',
];

return $result;
return implode(DIRECTORY_SEPARATOR, $pathSegments);
}

/**
Expand Down
57 changes: 47 additions & 10 deletions modules/cms/classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use Url;
use App;
use View;
use File;
use Lang;
use Flash;
use Cache;
use Config;
use Session;
use Request;
Expand Down Expand Up @@ -1353,22 +1355,57 @@ public function currentPageUrl($parameters = [], $routePersistence = true)
* @param mixed $url Specifies the theme-relative URL. If null, the theme path is returned.
* @return string
*/
public function themeUrl($url = null)
public function themeUrl($url = null): string
{
return is_array($url)
? $this->themeCombineAssets($url)
: $this->getTheme()->assetUrl($url);
}

/**
* Generates a URL to the AssetCombiner for the provided array of assets
*/
protected function themeCombineAssets(array $url): string
{
$themeDir = $this->getTheme()->getDirName();
$parentTheme = $this->getTheme()->getConfig()['parent'] ?? false;

if (is_array($url)) {
$_url = Url::to(CombineAssets::combine($url, themes_path().'/'.$themeDir));
}
else {
$_url = Config::get('cms.themesPath', '/themes').'/'.$themeDir;
if ($url !== null) {
$_url .= '/'.$url;
$cacheKey = __METHOD__ . '.' . md5(json_encode($url));

if (!($assets = Cache::get($cacheKey))) {
$assets = [];
$sources = [
themes_path($themeDir)
];

if ($parentTheme) {
$sources[] = themes_path($parentTheme);
}
$_url = Url::asset($_url);

foreach ($url as $file) {
// Leave Combiner Aliases assets unmodified
if (str_starts_with($file, '@')) {
$assets[] = $file;
continue;
}

foreach ($sources as $source) {
$asset = $source . DIRECTORY_SEPARATOR . $file;
if (File::exists($asset)) {
$assets[] = $asset;
break 2;
}
}

// Skip combining missing assets and log an error
Log::error("$file could not be found in any of the theme's sources (" . implode(', ', $sources) . ',');
continue;
}

Cache::put($cacheKey, $assets);
}

return $_url;
return Url::to(CombineAssets::combine($assets));
}

/**
Expand Down
Loading