Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 13 additions & 12 deletions Classes/Suffle/Snapshot/Aspects/FusionCachingAspect.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
namespace Suffle\Snapshot\Aspects;

/**
Expand All @@ -13,28 +15,23 @@
*
*/

use Neos\Cache\Exception;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Cache\Frontend\VariableFrontend;

/**
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
#[Flow\Scope('singleton')]
#[Flow\Aspect]
class FusionCachingAspect
{
/**
* @Flow\Inject
* @var VariableFrontend
*/
#[Flow\Inject]
protected $fusionCache;

/**
* @Flow\Around("method(Suffle\Snapshot\Fusion\FusionService->getMergedFusionObjectTreeForSitePackage())")
* @param JoinPointInterface $joinPoint The current join point
* @return mixed
*/
public function cacheGetMergedFusionObjectTree(JoinPointInterface $joinPoint)
#[Flow\Around('method(Suffle\Snapshot\Fusion\FusionService->getMergedFusionObjectTreeForSitePackage())')]
public function cacheGetMergedFusionObjectTree(JoinPointInterface $joinPoint): mixed
{
$siteResourcesPackageKey = $joinPoint->getMethodArgument('siteResourcesPackageKey');
$cacheIdentifier = str_replace('.', '_', $siteResourcesPackageKey);
Expand All @@ -43,7 +40,11 @@ public function cacheGetMergedFusionObjectTree(JoinPointInterface $joinPoint)
$fusionObjectTree = $this->fusionCache->get($cacheIdentifier);
} else {
$fusionObjectTree = $joinPoint->getAdviceChain()->proceed($joinPoint);
$this->fusionCache->set($cacheIdentifier, $fusionObjectTree);
try {
$this->fusionCache->set($cacheIdentifier, $fusionObjectTree);
} catch (Exception) {
// Cache could not be written
}
}

return $fusionObjectTree;
Expand Down
53 changes: 22 additions & 31 deletions Classes/Suffle/Snapshot/Command/SnapshotCommandController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Suffle\Snapshot\Command;

/**
Expand All @@ -13,14 +15,13 @@
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Suffle\Snapshot\Fusion\FusionService;
use Suffle\Snapshot\Service\TestingService;
use Suffle\Snapshot\Service\SnapshotService;
use Suffle\Snapshot\Service\TestingService;
use Suffle\Snapshot\Traits\PackageTrait;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;

/**
* Class SnapshotCommandController
* @package Suffle\Snapshot\Command
Expand All @@ -29,18 +30,14 @@ class SnapshotCommandController extends CommandController
{
use PackageTrait;

/**
* @Flow\Inject
* @var FusionService
*/
protected $fusionService;

#[Flow\Inject]
protected FusionService $fusionService;

/**
* Take Snapshot of given Fusion Component
*
* @param string $prototypeName to take snapshot of
* @param string $packageKey site-package (defaults to first found)
* @param string|null $packageKey site-package (defaults to first found)
* @throws \Exception
*/
public function takeCommand(string $prototypeName, string $packageKey = null): void
Expand All @@ -54,7 +51,7 @@ public function takeCommand(string $prototypeName, string $packageKey = null): v
/**
* Take Snapshots of all Fusion Components
*
* @param string $packageKey site-package (defaults to first found)
* @param string|null $packageKey site-package (defaults to first found)
* @throws \Exception
*/
public function takeAllCommand(string $packageKey = null): void
Expand All @@ -69,15 +66,15 @@ public function takeAllCommand(string $packageKey = null): void
/**
* Test given Fusion Component
*
* @param string §prototypeName name of prototype to be tested
* @param string $prototypeName name of prototype to be tested
* @param bool $interactive use interactive mode
* @param bool $updateall update all failed snapshots
* @param string $packageKey site-package (defaults to first found)
* @param bool $updateAll update all failed snapshots
* @param string|null $packageKey site-package (defaults to first found)
* @throws \Exception
*/
public function testCommand(string $prototypeName, bool $interactive = false, bool $updateall = false, string $packageKey = null): void
public function testCommand(string $prototypeName, bool $interactive = false, bool $updateAll = false, string $packageKey = null): void
{
$testingService = new TestingService($packageKey, $interactive, $updateall);
$testingService = new TestingService($packageKey, $interactive, $updateAll);
$testStats = $testingService->testPrototype($prototypeName);

$this->outputTestResults($testStats);
Expand All @@ -87,13 +84,13 @@ public function testCommand(string $prototypeName, bool $interactive = false, bo
* Test all Fusion Components
*
* @param bool $interactive use interactive mode
* @param bool $updateall update all failed snapshots
* @param string $packageKey site-package (defaults to first found)
* @param bool $updateAll update all failed snapshots
* @param string|null $packageKey site-package (defaults to first found)
* @throws \Exception
*/
public function testAllCommand(bool $interactive = false, bool $updateall = false, $packageKey = null): void
public function testAllCommand(bool $interactive = false, bool $updateAll = false, string $packageKey = null): void
{
$testingService = new TestingService($packageKey, $interactive, $updateall);
$testingService = new TestingService($packageKey, $interactive, $updateAll);
$testStats = $testingService->testAllPrototypes();

$this->outputTestResults($testStats);
Expand All @@ -103,7 +100,7 @@ public function testAllCommand(bool $interactive = false, bool $updateall = fals
/**
* Get all items currently available for testing
*
* @param string $packageKey site-package (defaults to first found)
* @param string|null $packageKey site-package (defaults to first found)
* @throws \Exception
*/
public function itemsCommand(string $packageKey = null): void
Expand All @@ -124,9 +121,6 @@ public function itemsCommand(string $packageKey = null): void

/**
* Output results returned from snapshot service
*
* @param array $stats
* @throws \Exception
*/
private function outputSnapshotResults(array $stats): void
{
Expand All @@ -143,17 +137,14 @@ private function outputSnapshotResults(array $stats): void
}

if (!$stats['success']) {
throw new \Exception('Not all Snapshots could be written');
throw new \RuntimeException('Not all Snapshots could be written');
}

$this->output(PHP_EOL . "<fg=green>All Snapshots saved successfully</>");
}

/**
* Output results returned from TestService
*
* @param array $stats
* @throws \Exception
*/
private function outputTestResults(array $stats): void
{
Expand All @@ -176,12 +167,12 @@ private function outputTestResults(array $stats): void
if($stats['failedPrototypes']) {
foreach ($stats['failedPrototypes'] as $key => $propSets) {
$propsetString = count($propSets) > 1 ? 'propsets' : 'propset';
$this->output("\t<fg=yellow>" . $key . " with " . $propsetString . ": " . join(', ', $propSets) . "</>" . PHP_EOL);
$this->output("\t<fg=yellow>" . $key . " with " . $propsetString . ": " . implode(', ', $propSets) . "</>" . PHP_EOL);
}
}

if (!$stats['success']) {
throw new \Exception('Snapshot Testing failed');
throw new \RuntimeException('Snapshot Testing failed');
}

$this->output(PHP_EOL . "<fg=green>Snapshot testing successful</>");
Expand Down
103 changes: 45 additions & 58 deletions Classes/Suffle/Snapshot/Controller/ApiController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Suffle\Snapshot\Controller;

/**
Expand All @@ -15,48 +18,31 @@

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\Mvc\Exception;
use Neos\Flow\Mvc\View\JsonView;
use Neos\Utility\Exception\FilesException;
use SebastianBergmann\Diff\Differ;
use Suffle\Snapshot\Traits\SimulateContextTrait;
use Suffle\Snapshot\Traits\PackageTrait;
use Suffle\Snapshot\Diff\DiffOutputBuilder;
use Suffle\Snapshot\Fusion\FusionService;
use Suffle\Snapshot\Fusion\FusionView;
use Suffle\Snapshot\Service\SnapshotService;
use Suffle\Snapshot\Diff\DiffOutputBuilder;
use Suffle\Snapshot\Traits\PackageTrait;
use Suffle\Snapshot\Traits\SimulateContextTrait;

class ApiController extends ActionController {
class ApiController extends ActionController
{
use PackageTrait, SimulateContextTrait;

/**
* @var array
*/
protected $defaultViewObjectName = 'Neos\Flow\Mvc\View\JsonView';
protected $defaultViewObjectName = JsonView::class;

/**
* @Flow\Inject
* @var FusionService
*/
protected $fusionService;

/**
* @var ControllerContext
*/
protected $controllerContext;

/**
* @Flow\InjectConfiguration()
* @var array
*/
protected $settings;
#[Flow\Inject]
protected FusionService $fusionService;

/**
* get all names of testable objects
*
* @Flow\SkipCsrfProtection
* @param string $packageKey
* @return void
*/
public function snapshotObjectsAction($packageKey = null)
#[Flow\SkipCsrfProtection]
public function snapshotObjectsAction(string $packageKey = null): void
{
$packageKey = $packageKey ?: $this->getFirstOnlineSitePackageKey();

Expand All @@ -70,16 +56,13 @@ public function snapshotObjectsAction($packageKey = null)

/**
* Get data for single prototype
*
* @Flow\SkipCsrfProtection
* @param string $prototypeName
* @param string $packageKey
* @return void
*/
public function snapshotDataAction($prototypeName, $packageKey = null)
#[Flow\SkipCsrfProtection]
public function snapshotDataAction(string $prototypeName, string $packageKey = null): void
{
$packageKey = $packageKey ?: $this->getFirstOnlineSitePackageKey();
$prototypePreviewRenderPath = FusionService::RENDERPATH_DISCRIMINATOR . str_replace(['.', ':'], ['_', '__'], $prototypeName);
$prototypePreviewRenderPath = FusionService::RENDERPATH_DISCRIMINATOR . str_replace(['.', ':'], ['_', '__'],
$prototypeName);

$fusionView = new FusionView();
$fusionView->setControllerContext($this->createDummyContext());
Expand All @@ -93,10 +76,20 @@ public function snapshotDataAction($prototypeName, $packageKey = null)
$builder = new DiffOutputBuilder();
$differ = new Differ($builder);

$renderedPrototypes = $fusionView->renderSnapshotPrototype($prototypeName);
try {
$renderedPrototypes = $fusionView->renderSnapshotPrototype($prototypeName);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage());
$renderedPrototypes = [];
}

foreach ($renderedPrototypes as $propSetName => $renderedPrototype) {
$savedSnapshot = $snapshotService->getSnapshotOfPropSet($prototypeName, $propSetName, $packageKey);
try {
$savedSnapshot = $snapshotService->getSnapshotOfPropSet($prototypeName, $propSetName, $packageKey);
} catch (FilesException $e) {
$this->logger->error($e->getMessage());
$savedSnapshot = null;
}

if ($savedSnapshot) {
$diff = $differ->diff($savedSnapshot, $renderedPrototype);
Expand All @@ -105,8 +98,8 @@ public function snapshotDataAction($prototypeName, $packageKey = null)
$result[$propSetName] = [
'snapshot' => $savedSnapshot,
'current' => $renderedPrototype,
'hasSnapshot' => $savedSnapshot ? true : false,
'testSuccess' => $diff ? false : true
'hasSnapshot' => (bool)$savedSnapshot,
'testSuccess' => $savedSnapshot && !$diff,
];
}

Expand All @@ -115,39 +108,33 @@ public function snapshotDataAction($prototypeName, $packageKey = null)

/**
* Get preview markup
*
* @Flow\SkipCsrfProtection
* @param string $packageKey
* @return void
*/
public function previewMarkupAction($packageKey = null)
#[Flow\SkipCsrfProtection]
public function previewMarkupAction(string $packageKey = null): void
{
$packageKey = $packageKey ?: $this->getFirstOnlineSitePackageKey();
$previewPrototypeName = $this->settings['previewPrototypeName'];
$prototypePreviewRenderPath = str_replace(['.', ':'], ['_', '__'], $previewPrototypeName);

$fusionView = new FusionView();

$fusionRootPath = sprintf('/<%s>', $prototypePreviewRenderPath);
$fusionView->setControllerContext($this->controllerContext);
$fusionView->setPackageKey($packageKey);

// get the status and headers from the view
$result = [
'previewMarkup' => $fusionView->renderPrototype($previewPrototypeName)
];


try {
$result = [
'previewMarkup' => $fusionView->renderPrototype($previewPrototypeName)
];
} catch (\Throwable $e) {
$result = 'Error: ' . $e->getMessage();
}
$this->view->assign('value', $result);
}

/**
* Get all site packages
*
* @Flow\SkipCsrfProtection
* @return void
*/
public function sitePackagesAction()
#[Flow\SkipCsrfProtection]
public function sitePackagesAction(): void
{
$sitePackages = $this->getSitePackages();
$result = [];
Expand Down
Loading