Skip to content

Commit aad6e68

Browse files
authored
Merge pull request #7 from Sebobo/feature/neos-9
FEATURE: Compatibility with Neos 9
2 parents ae1202e + d98f428 commit aad6e68

24 files changed

+355
-303
lines changed
File renamed without changes.

.github/workflows/tests.yml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
env:
10+
PACKAGE_FOLDER: Shel.CriticalCSS
11+
12+
jobs:
13+
codestyle:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8.2
23+
24+
- name: Add additional dependencies
25+
run: |
26+
composer require --no-update --no-interaction neos/contentgraph-doctrinedbaladapter:"~9.0.0"
27+
28+
- name: Cache dependencies
29+
uses: actions/cache@v3
30+
with:
31+
path: ~/.composer/cache
32+
key: dependencies-composer-${{ hashFiles('composer.json') }}
33+
34+
- name: Install dependencies
35+
uses: php-actions/composer@v6
36+
with:
37+
php_version: 8.2
38+
version: 2
39+
40+
- name: PHPStan
41+
uses: php-actions/phpstan@v3
42+
with:
43+
php_version: 8.2
44+
version: 2.1.17
45+
command: analyse
46+
path: 'Classes/'
47+
48+
build:
49+
runs-on: ubuntu-latest
50+
51+
strategy:
52+
matrix:
53+
php-versions:
54+
- '8.2'
55+
neosVersion:
56+
- '9.0'
57+
58+
services:
59+
mariadb:
60+
# see https://mariadb.com/kb/en/mariadb-server-release-dates/
61+
# this should be a current release, e.g. the LTS version
62+
image: mariadb:10.8
63+
env:
64+
MYSQL_USER: neos
65+
MYSQL_PASSWORD: neos
66+
MYSQL_DATABASE: neos_functional_testing
67+
MYSQL_ROOT_PASSWORD: neos
68+
ports:
69+
- "3306:3306"
70+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
71+
72+
name: 'Shel.CriticalCSS with Neos ${{ matrix.neosVersion }} test'
73+
74+
env:
75+
NEOS_TARGET_VERSION: ${{matrix.neosVersion}}
76+
FLOW_CONTEXT: Testing
77+
FLOW_FOLDER: ../neos-base-distribution
78+
79+
steps:
80+
- uses: actions/checkout@v3
81+
82+
- name: Set package branch name
83+
run: echo "PACKAGE_TARGET_VERSION=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_ENV
84+
working-directory: .
85+
86+
- name: Setup PHP
87+
uses: shivammathur/setup-php@v2
88+
with:
89+
php-version: ${{ matrix.php-versions }}
90+
extensions: mbstring, xml, json, zlib, iconv, intl, pdo_sqlite, mysql
91+
tools: composer:v2
92+
93+
- name: Cache dependencies
94+
uses: actions/cache@v3
95+
with:
96+
path: ~/.composer/cache
97+
key: dependencies-composer-${{ hashFiles('composer.json') }}
98+
99+
- name: Prepare Flow distribution
100+
run: |
101+
git clone https://github.com/neos/neos-base-distribution.git -b ${NEOS_TARGET_VERSION} ${FLOW_FOLDER}
102+
cd ${FLOW_FOLDER}
103+
104+
git -C ../${{ env.PACKAGE_FOLDER }} checkout -b build
105+
composer config repositories.package '{ "type": "path", "url": "../${{ env.PACKAGE_FOLDER }}", "options": { "symlink": false } }'
106+
composer require --no-update --no-interaction shel/critical-css:"dev-build as dev-${PACKAGE_TARGET_VERSION}"
107+
composer require --no-update --no-interaction neos/contentgraph-doctrinedbaladapter:"~9.0.0"
108+
109+
- name: Composer Install
110+
run: |
111+
cd ${FLOW_FOLDER}
112+
composer update --no-interaction --no-progress
113+
114+
- name: Setup Flow configuration
115+
run: |
116+
cd ${FLOW_FOLDER}
117+
rm -f Configuration/Testing/Settings.yaml
118+
cat <<EOF >> Configuration/Testing/Settings.yaml
119+
Neos:
120+
Flow:
121+
persistence:
122+
backendOptions:
123+
host: '127.0.0.1'
124+
driver: pdo_mysql
125+
user: 'neos'
126+
password: 'neos'
127+
dbname: 'neos_functional_testing'
128+
EOF
129+
130+
- name: Run Functional tests
131+
run: |
132+
cd ${FLOW_FOLDER}
133+
bin/phpunit --colors -c Build/BuildEssentials/PhpUnit/FunctionalTests.xml Packages/Plugins/Shel.CriticalCSS/Tests/Functional/

.styleci.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

Classes/Command/StylesCommandController.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,18 @@
1111
use Neos\Flow\Annotations as Flow;
1212
use Neos\Flow\Cli\CommandController;
1313
use Neos\Flow\Mvc\ActionRequest;
14-
use Neos\Flow\Mvc\ActionResponse;
15-
use Neos\Flow\Mvc\Controller\Arguments;
16-
use Neos\Flow\Mvc\Controller\ControllerContext;
17-
use Neos\Flow\Mvc\Routing\UriBuilder;
1814
use Neos\Flow\Security\Exception as SecurityException;
1915
use Neos\Neos\Domain\Repository\SiteRepository;
2016
use Shel\CriticalCSS\Fusion\FusionView;
21-
use Shel\CriticalCSS\Service\FusionService;
2217

2318
/**
2419
* The User Command Controller
25-
*
26-
* @Flow\Scope("singleton")
2720
*/
21+
#[Flow\Scope('singleton')]
2822
class StylesCommandController extends CommandController
2923
{
30-
/**
31-
* @Flow\Inject
32-
* @var SiteRepository
33-
*/
34-
protected $siteRepository;
35-
36-
/**
37-
* @Flow\Inject
38-
* @var FusionService
39-
*/
40-
protected $fusionService;
24+
#[Flow\Inject]
25+
protected SiteRepository $siteRepository;
4126

4227
/**
4328
* This command returns all styles for a given site node and prototype name.
@@ -56,7 +41,7 @@ public function exportCommand(string $siteNodeName, string $basePrototypeName =
5641
}
5742

5843
$fusionView = new FusionView();
59-
$fusionView->setControllerContext($this->createSimpleControllerContext());
44+
$fusionView->assign('request', $this->createSimpleControllerRequest());
6045
$fusionView->setFusionPath('shelCriticalStyles');
6146
$fusionView->setPackageKey($site->getSiteResourcesPackageKey());
6247
$fusionView->assign('site', $site->getNodeName());
@@ -65,17 +50,11 @@ public function exportCommand(string $siteNodeName, string $basePrototypeName =
6550
}
6651

6752
/**
68-
* Create a simple controller context which can be used to instantiate a Fusion runtime etc.
53+
* Create a simple controller context which can be used to instantiate a Fusion runtime, etc.
6954
*/
70-
protected function createSimpleControllerContext(): ControllerContext
55+
protected function createSimpleControllerRequest(): ActionRequest
7156
{
7257
$httpRequest = new ServerRequest('POST', 'http://localhost');
73-
$request = ActionRequest::fromHttpRequest($httpRequest);
74-
$response = new ActionResponse();
75-
$arguments = new Arguments([]);
76-
$uriBuilder = new UriBuilder();
77-
$uriBuilder->setRequest($request);
78-
79-
return new ControllerContext($request, $response, $arguments, $uriBuilder);
58+
return ActionRequest::fromHttpRequest($httpRequest);
8059
}
8160
}

Classes/Fusion/FusionView.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
use Neos\Flow\Annotations as Flow;
1111
use Neos\Flow\Mvc\Exception;
1212
use Neos\Flow\Security\Exception as SecurityException;
13-
use Neos\FluidAdaptor\View\TemplateView;
13+
use Neos\Fusion\Core\FusionConfiguration;
14+
use Neos\Fusion\Core\FusionGlobals;
1415
use Neos\Fusion\View\FusionView as BaseFusionView;
1516
use Neos\Fusion\Core\Runtime as FusionRuntime;
1617
use Shel\CriticalCSS\Service\FusionService;
@@ -20,55 +21,52 @@
2021
*/
2122
class FusionView extends BaseFusionView
2223
{
23-
protected $styleRenderPath = 'shelCriticalStyles';
24+
protected string $styleRenderPath = 'shelCriticalStyles';
2425

25-
/**
26-
* @Flow\Inject
27-
* @var FusionService
28-
*/
29-
protected $fusionService;
30-
31-
/**
32-
* @Flow\Inject
33-
* @var TemplateView
34-
*/
35-
protected $fallbackView;
26+
#[Flow\Inject]
27+
protected FusionService $fusionService;
3628

3729
/**
3830
* @inheritDoc
31+
* @throws Exception
3932
*/
4033
protected function loadFusion(): void
4134
{
42-
$fusionAst = [];
43-
try {
44-
$fusionAst = $this->fusionService->getMergedFusionObjectTreeForSitePackage($this->getOption('packageKey'));
45-
} catch (Exception $e) {
46-
}
35+
$fusionAst = $this->fusionService->getFusionConfigurationForSitePackage(
36+
$this->getOption('packageKey')
37+
);
4738
$this->parsedFusion = $fusionAst;
4839
}
4940

5041
/**
5142
* Iterates through the Fusion AST and renders all instantiated
5243
* objects of the given prototype and returns the concatenated results as string.
5344
*
54-
* @param string $stylePrototypeName
55-
* @return string
5645
* @throws SecurityException
5746
*/
5847
public function renderStyles(string $stylePrototypeName): string
5948
{
49+
/** @noinspection PhpConditionAlreadyCheckedInspection */
50+
/** @phpstan-ignore booleanNot.alwaysFalse */
6051
if (!$this->parsedFusion) {
61-
$this->loadFusion();
52+
try {
53+
$this->loadFusion();
54+
} catch (Exception) {
55+
return '';
56+
}
57+
}
58+
$fusionAst = $this->parsedFusion->toArray();
59+
$prototypes = $fusionAst['__prototypes'] ?? [];
60+
61+
if (!$prototypes) {
62+
return '';
6263
}
63-
$fusionAst = $this->parsedFusion;
64-
$prototypes = $fusionAst['__prototypes'];
6564

6665
$arrayIterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($fusionAst));
6766
$outputArray = [];
6867
/** @noinspection PhpUnusedLocalVariableInspection */
6968
foreach ($arrayIterator as $sub) {
70-
$subArray = $arrayIterator->getSubIterator();
71-
/** @noinspection PhpParamsInspection */
69+
$subArray = iterator_to_array($arrayIterator->getSubIterator());
7270
if (!array_key_exists('__objectType', $subArray)) {
7371
continue;
7472
}
@@ -77,7 +75,7 @@ public function renderStyles(string $stylePrototypeName): string
7775
($prototypeName
7876
&& array_key_exists($prototypeName, $prototypes)
7977
&& array_key_exists('__prototypeChain', $prototypes[$prototypeName])
80-
&& in_array($stylePrototypeName, $prototypes[$prototypeName]['__prototypeChain']))) {
78+
&& in_array($stylePrototypeName, $prototypes[$prototypeName]['__prototypeChain'], true))) {
8179
$props = iterator_to_array($subArray);
8280
$props['__meta']['stylesOnly'] = true;
8381
$outputArray[] = $props;
@@ -89,7 +87,10 @@ public function renderStyles(string $stylePrototypeName): string
8987
// Render each found instantiated prototype
9088
foreach ($outputArray as $props) {
9189
$fusionAst[$this->styleRenderPath] = $props;
92-
$fusionRuntime = new FusionRuntime($fusionAst, $this->controllerContext);
90+
$fusionGlobals = FusionGlobals::fromArray(array_filter([
91+
'request' => $this->assignedActionRequest,
92+
]));
93+
$fusionRuntime = new FusionRuntime(FusionConfiguration::fromArray($fusionAst), $fusionGlobals);
9394
$fusionRuntime->pushContextArray($this->variables);
9495
$output .= $fusionRuntime->render($this->styleRenderPath);
9596
$fusionRuntime->popContext();

Classes/FusionObjects/LoadStylesImplementation.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Shel\CriticalCSS\FusionObjects;
@@ -17,8 +18,6 @@ class LoadStylesImplementation extends AbstractFusionObject
1718
{
1819
/**
1920
* HTML content that should be processed (optional)
20-
*
21-
* @return string|null
2221
*/
2322
protected function getContent(): ?string
2423
{
@@ -27,22 +26,17 @@ protected function getContent(): ?string
2726

2827
/**
2928
* Path to a CSS resource
30-
*
31-
* @return string
3229
*/
3330
protected function getPath(): string
3431
{
3532
return $this->fusionValue('path');
3633
}
3734

38-
/**
39-
* @return string
40-
*/
4135
public function evaluate(): string
4236
{
4337
try {
4438
$styles = $this->loadResourceContent($this->getPath());
45-
} catch (InvalidVariableException $e) {
39+
} catch (InvalidVariableException) {
4640
throw new \InvalidArgumentException('Resource ' . $this->getPath() . ' cannot be loaded!', 1573314641);
4741
}
4842

@@ -51,16 +45,13 @@ public function evaluate(): string
5145

5246
/**
5347
* Loads the content of a resource and returns it as string
54-
*
55-
* @return string Content of the resource
5648
* @throws InvalidVariableException
5749
*/
5850
public function loadResourceContent(?string $path): string
5951
{
6052
if ($path === null) {
6153
throw new InvalidVariableException('You have to define a path.', 1573317165);
6254
}
63-
64-
return file_get_contents($path);
55+
return file_get_contents($path) ?: '';
6556
}
6657
}

0 commit comments

Comments
 (0)