From 692dd535e08846325b8ccc35df4be1ff08f382ae Mon Sep 17 00:00:00 2001 From: Martin Ficzel Date: Thu, 10 Jun 2021 18:13:08 +0200 Subject: [PATCH 1/2] FEATURE: Preview for non site packages The `packages` configuration can now be used to configure non-site packages for previewing in the styleguide. When a prototyope of a non-site package is rendered Monocle will only load the Root.fusion of this specific package and the Monocle Root.fusion. Every other fusion including the default fusion has to be included explicitly. This mimics the behavior of the classic FusionView that is used for FusionRendering of Flow Controller Actions. ```YAML Sitegeist: Monocle: packages: #add a key to the package list without package specific configuration 'Vendor.Example': {} ``` --- Classes/Fusion/FusionService.php | 28 +++++++++++++++++------ Classes/Service/PackageKeyTrait.php | 17 +++++++++----- README.md | 35 ++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/Classes/Fusion/FusionService.php b/Classes/Fusion/FusionService.php index dad1ca59..28d6f862 100644 --- a/Classes/Fusion/FusionService.php +++ b/Classes/Fusion/FusionService.php @@ -14,6 +14,7 @@ */ use Neos\Flow\Annotations as Flow; +use Neos\Flow\Package\PackageManager; use \Neos\Neos\Domain\Service\FusionService as NeosFusionService; /** @@ -30,6 +31,12 @@ class FusionService extends NeosFusionService */ protected $autoIncludeConfiguration = array(); + /** + * @Flow\Inject + * @var PackageManager + */ + protected $packageManager; + /** * Returns a merged fusion object tree in the context of the given site-package * @@ -53,15 +60,22 @@ public function getMergedTypoScriptObjectTreeForSitePackage($siteResourcesPackag public function getMergedFusionObjectTreeForSitePackage($siteResourcesPackageKey) { $siteRootFusionPathAndFilename = sprintf($this->siteRootFusionPattern, $siteResourcesPackageKey); + $package = $this->packageManager->getPackage($siteResourcesPackageKey); - $mergedFusionCode = ''; - $mergedFusionCode .= $this->generateNodeTypeDefinitions(); - $mergedFusionCode .= $this->getFusionIncludes($this->prepareAutoIncludeFusion()); - $mergedFusionCode .= $this->getFusionIncludes($this->prependFusionIncludes); - $mergedFusionCode .= $this->readExternalFusionFile($siteRootFusionPathAndFilename); - $mergedFusionCode .= $this->getFusionIncludes($this->appendFusionIncludes); + $fusionCode = ''; + + if ($package->getComposerManifest('type') == 'neos-site') { + $fusionCode .= $this->generateNodeTypeDefinitions(); + $fusionCode .= $this->getFusionIncludes($this->prepareAutoIncludeFusion()); + $fusionCode .= $this->getFusionIncludes($this->prependFusionIncludes); + $fusionCode .= $this->readExternalFusionFile($siteRootFusionPathAndFilename); + $fusionCode .= $this->getFusionIncludes($this->appendFusionIncludes); + } else { + $fusionCode .= 'include: resource://Sitegeist.Monocle/Private/Fusion/Root.fusion' . PHP_EOL; + $fusionCode .= 'include: resource://' . $siteResourcesPackageKey . '/Private/Fusion/Root.fusion' . PHP_EOL; + } - return $this->fusionParser->parse($mergedFusionCode, $siteRootFusionPathAndFilename); + return $this->fusionParser->parse($fusionCode, $siteRootFusionPathAndFilename); } diff --git a/Classes/Service/PackageKeyTrait.php b/Classes/Service/PackageKeyTrait.php index dfa2d659..ebde5557 100644 --- a/Classes/Service/PackageKeyTrait.php +++ b/Classes/Service/PackageKeyTrait.php @@ -17,6 +17,12 @@ trait PackageKeyTrait */ protected $packageManager; + /** + * @Flow\InjectConfiguration("packages") + * @var mixed[] + */ + protected $packageConfigurations; + /** * Determine the default site package key * @@ -30,16 +36,17 @@ protected function getDefaultSitePackageKey() /** * Get a list of all active site package keys - * @return array + * @return string[] */ - protected function getActiveSitePackageKeys() + protected function getActiveSitePackageKeys(): array { $sitePackages = $this->packageManager->getFilteredPackages('available', null, 'neos-site'); - $result = []; + $sitePackageKeys = []; foreach ($sitePackages as $sitePackage) { $packageKey = $sitePackage->getPackageKey(); - $result[] = $packageKey; + $sitePackageKeys[] = $packageKey; } - return $result; + $configuredPackageKeys = $this->packageConfigurations ? array_keys($this->packageConfigurations) : []; + return array_unique(array_merge($sitePackageKeys, $configuredPackageKeys)); } } diff --git a/README.md b/README.md index cb4d1f72..c9c2b1eb 100644 --- a/README.md +++ b/README.md @@ -352,7 +352,7 @@ Sitegeist: This allows for including prototypes of packages selectively. -### Site-specific configuration +### Package-specific configuration All configurations can be overwritten for each selected site package. @@ -369,6 +369,19 @@ Sitegeist: height: 1000 ``` +This packages configuration can also be used to configure non-site packages for previewing in the styleguide. + +When a prototyope of a non-site package is rendered Monocle will only load the Root.fusion of this specific package and the Monocle Root.fusion. Every other fusion including the default fusion has to be included +explicitly. This mimics the behavior of the classic FusionView that is used for FusionRendering of Flow Controller Actions. + +```YAML +Sitegeist: + Monocle: + packages: + #add a key to the package list without package specific configuration + 'Vendor.Example': {} +``` + ### Fusion Sitegeist.Monocle brings some fusion-prototypes that you can use or adjust to your needs. @@ -384,19 +397,19 @@ prototype(Sitegeist.Monocle:Preview.Page) { metaViewport = '' stylesheets.main = Neos.Fusion:Tag { - tagName = 'link' - attributes.rel = 'stylesheet' - attributes.href = Neos.Fusion:ResourceUri { - path = 'resource://Vendor.Site/Public/Styles/main.css' - } + tagName = 'link' + attributes.rel = 'stylesheet' + attributes.href = Neos.Fusion:ResourceUri { + path = 'resource://Vendor.Site/Public/Styles/main.css' + } } javascripts.main = Neos.Fusion:Tag { - tagName = 'script' - attributes.src = Neos.Fusion:ResourceUri { - path = 'resource://Vendor.Site/Public/JavaScript/main.js' - } - } + tagName = 'script' + attributes.src = Neos.Fusion:ResourceUri { + path = 'resource://Vendor.Site/Public/JavaScript/main.js' + } + } } } ``` From 79545769e06e66b97d42e6fbd9296973ed49f74c Mon Sep 17 00:00:00 2001 From: Martin Ficzel Date: Thu, 10 Jun 2021 18:33:36 +0200 Subject: [PATCH 2/2] FEATURE: Smarter and configurable determination of defaultPackageKey If a domain can be identified from the request monocle will use this to determine the initial sitePackage. If that does not work a newly added setting `defaultPackageKey` is used. If this setting was not set the key of the first site package or the first key in the `packages` configuration is used. --- Classes/Service/PackageKeyTrait.php | 26 ++++++++++++++++++++++++++ Configuration/Settings.yaml | 2 ++ 2 files changed, 28 insertions(+) diff --git a/Classes/Service/PackageKeyTrait.php b/Classes/Service/PackageKeyTrait.php index ebde5557..11051df9 100644 --- a/Classes/Service/PackageKeyTrait.php +++ b/Classes/Service/PackageKeyTrait.php @@ -4,6 +4,7 @@ use Neos\Flow\Annotations as Flow; use Neos\Flow\Package\PackageManager; use Neos\Flow\Package\PackageInterface; +use Neos\Neos\Domain\Repository\DomainRepository; /** * Utility trait to determine package keys @@ -17,6 +18,18 @@ trait PackageKeyTrait */ protected $packageManager; + /** + * @Flow\Inject + * @var Neos\Neos\Domain\Repository\DomainRepository + */ + protected $domainRepository; + + /** + * @Flow\InjectConfiguration("defaultPackageKey") + * @var string|null + */ + protected $defaultPackageKey; + /** * @Flow\InjectConfiguration("packages") * @var mixed[] @@ -30,6 +43,19 @@ trait PackageKeyTrait */ protected function getDefaultSitePackageKey() { + try { + $domain = $this->domainRepository->findOneByActiveRequest(); + if ($domain && $domain->getSite()) { + return $domain->getSite()->getSiteResourcesPackageKey(); + } + } catch (\Exception $e) { + // ignore errors that may occur if no database is present + } + + if ($this->defaultPackageKey) { + return $this->defaultPackageKey; + } + $sitePackageKeys = $this->getActiveSitePackageKeys(); return reset($sitePackageKeys); } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 8b17a49e..9ba1d6af 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -11,6 +11,8 @@ Sitegeist: fusion: enableObjectTreeCache: true + defaultPackageKey: null + packages: { } ui: