Skip to content

Commit

Permalink
Merge pull request #147 from sitegeist/feature/useCases
Browse files Browse the repository at this point in the history
FEATURE: Add `useCases` for styleguide prototypes
  • Loading branch information
mficzel authored Jun 7, 2021
2 parents b6013e1 + b65e992 commit 6d598d8
Show file tree
Hide file tree
Showing 34 changed files with 780 additions and 88 deletions.
20 changes: 15 additions & 5 deletions Classes/Command/StyleguideCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ public function itemsCommand($format = 'json', $packageKey = null)
* Render a given fusion component to HTML
*
* @param string $prototypeName The prototype name of the component
* @param string $packageKey site-package (defaults to first found)
* @param string $propSet The propSet used for the preview
* @param string $props Custom props for the preview
* @param string $locales Custom locales for the preview
* @param string|null $packageKey site-package (defaults to first found)
* @param string|null $useCase The useCase for the preview
* @param string|null $propSet The propSet used for the preview
* @param string|null $props Custom props for the preview
* @param string|null $locales Custom locales for the preview
* @return void
*/
public function renderCommand($prototypeName, $packageKey = null, $propSet = '__default', $props = '', $locales = '')
public function renderCommand($prototypeName, $packageKey = null, ?string $useCase = '__default', ?string $propSet = '__default', ?string $props = '', ?string $locales = '')
{
$sitePackageKey = $packageKey ?: $this->getDefaultSitePackageKey();
$convertedProps = json_decode($props, true) ?? [];
Expand All @@ -98,9 +99,18 @@ public function renderCommand($prototypeName, $packageKey = null, $propSet = '__
$fusionView->setPackageKey($sitePackageKey);
$fusionView->setFusionPath($fusionRootPath);

if ($useCase == '__default') {
$useCase = null;
}

if ($propSet == '__default') {
$propSet = null;
}

$fusionView->assignMultiple([
'sitePackageKey' => $packageKey,
'prototypeName' => $prototypeName,
'useCase' => $useCase,
'propSet' => $propSet,
'props' => $convertedProps,
'locales' => $convertedLocales
Expand Down
18 changes: 14 additions & 4 deletions Classes/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ class PreviewController extends ActionController
/**
* @param string $prototypeName
* @param string $sitePackageKey
* @param string $propSet
* @param string $props props as json encoded string
* @param string $locales locales-fallback-chain as comma sepertated string
* @param string|null $useCase
* @param string|null $propSet
* @param string|null $props props as json encoded string
* @param string|null $locales locales-fallback-chain as comma sepertated string
* @return void
*/
public function indexAction($prototypeName, $sitePackageKey, $propSet = '__default', $props = '', $locales = '')
public function indexAction(string $prototypeName, string $sitePackageKey, ?string $useCase = '__default', ?string $propSet = '__default', ?string $props = '', ?string $locales = '')
{
$renderProps = [];

Expand All @@ -76,6 +77,14 @@ public function indexAction($prototypeName, $sitePackageKey, $propSet = '__defau
}
}

if ($useCase == '__default') {
$useCase = null;
}

if ($propSet == '__default') {
$propSet = null;
}

if ($locales) {
$renderLocales = explode(',', $locales);
} else {
Expand All @@ -92,6 +101,7 @@ public function indexAction($prototypeName, $sitePackageKey, $propSet = '__defau
$this->view->assignMultiple([
'sitePackageKey' => $sitePackageKey,
'prototypeName' => $prototypeName,
'useCase' => $useCase,
'propSet' => $propSet,
'props' => $renderProps,
'locales' => $renderLocales
Expand Down
22 changes: 20 additions & 2 deletions Classes/Domain/PrototypeDetails/PrototypeDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sitegeist\Monocle\Domain\Fusion\PrototypeName;
use Sitegeist\Monocle\Domain\PrototypeDetails\Props\PropsCollectionInterface;
use Sitegeist\Monocle\Domain\PrototypeDetails\PropSets\PropSetCollection;
use Sitegeist\Monocle\Domain\PrototypeDetails\UseCases\UseCaseCollection;

/**
* @Flow\Proxy(false)
Expand Down Expand Up @@ -58,6 +59,11 @@ final class PrototypeDetails implements PrototypeDetailsInterface
*/
private $propSets;

/**
* @var UseCaseCollection
*/
private $useCases;

/**
* @param PrototypeName $prototypeName
* @param RenderedCode $renderedCode
Expand All @@ -66,6 +72,7 @@ final class PrototypeDetails implements PrototypeDetailsInterface
* @param Anatomy $anatomy
* @param PropsCollectionInterface $props
* @param PropSetCollection $propSets
* @param UseCaseCollection $useCases
*/
public function __construct(
PrototypeName $prototypeName,
Expand All @@ -74,7 +81,8 @@ public function __construct(
FusionPrototypeAst $fusionAst,
Anatomy $anatomy,
PropsCollectionInterface $props,
PropSetCollection $propSets
PropSetCollection $propSets,
UseCaseCollection $useCases
) {
$this->prototypeName = $prototypeName;
$this->renderedCode = $renderedCode;
Expand All @@ -83,6 +91,7 @@ public function __construct(
$this->anatomy = $anatomy;
$this->props = $props;
$this->propSets = $propSets;
$this->useCases = $useCases;
}

/**
Expand Down Expand Up @@ -141,6 +150,14 @@ public function getPropSets(): PropSetCollection
return $this->propSets;
}

/**
* @return UseCaseCollection
*/
public function getUseCases(): UseCaseCollection
{
return $this->useCases;
}

/**
* @return array<mixed>
*/
Expand All @@ -153,7 +170,8 @@ public function jsonSerialize()
'fusionAst' => $this->fusionAst,
'anatomy' => $this->anatomy,
'props' => $this->props,
'propSets' => $this->propSets
'propSets' => $this->propSets,
'useCases' => $this->useCases
];
}
}
5 changes: 4 additions & 1 deletion Classes/Domain/PrototypeDetails/PrototypeDetailsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Sitegeist\Monocle\Domain\Fusion\Prototype;
use Sitegeist\Monocle\Domain\PrototypeDetails\Props\PropsCollectionFactoryInterface;
use Sitegeist\Monocle\Domain\PrototypeDetails\PropSets\PropSetCollection;
use Sitegeist\Monocle\Domain\PrototypeDetails\UseCases\UseCase;
use Sitegeist\Monocle\Domain\PrototypeDetails\UseCases\UseCaseCollection;
use Sitegeist\Monocle\Fusion\ReverseFusionParser;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -60,7 +62,8 @@ public function forPrototype(Prototype $prototype): PrototypeDetailsInterface
->fromPrototypeForPrototypeDetails($prototype),
$this->propsCollectionFactory
->fromPrototypeForPrototypeDetails($prototype),
PropSetCollection::fromPrototype($prototype)
PropSetCollection::fromPrototype($prototype),
UseCaseCollection::fromPrototype($prototype)
);
}
}
63 changes: 63 additions & 0 deletions Classes/Domain/PrototypeDetails/UseCases/UseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);
namespace Sitegeist\Monocle\Domain\PrototypeDetails\UseCases;

/**
* This file is part of the Sitegeist.Monocle package
*
* (c) 2020
* Martin Ficzel <[email protected]>
* Wilhelm Behncke <[email protected]>
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;

/**
* @Flow\Proxy(false)
*/
final class UseCase implements \JsonSerializable
{
/**
* @var UseCaseName
*/
private $name;

/**
* @var UseCaseTitle
*/
private $title;

/**
* @var array<string,PropValue>
*/
private $overrides;

/**
* @param UseCaseName $name
* @param array $overrides
*/
public function __construct(
UseCaseName $name,
UseCaseTitle $title,
array $overrides
) {
$this->name = $name;
$this->title = $title;
$this->overrides = $overrides;
}

/**
* @return array<mixed>
*/
public function jsonSerialize()
{
return [
'name' => $this->name,
'title' => $this->title,
'overrides' => $this->overrides
];
}
}
80 changes: 80 additions & 0 deletions Classes/Domain/PrototypeDetails/UseCases/UseCaseCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);
namespace Sitegeist\Monocle\Domain\PrototypeDetails\UseCases;

/**
* This file is part of the Sitegeist.Monocle package
*
* (c) 2020
* Martin Ficzel <[email protected]>
* Wilhelm Behncke <[email protected]>
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Sitegeist\Monocle\Domain\Fusion\Prototype;
use Sitegeist\Monocle\Domain\PrototypeDetails\Props\PropValue;

/**
* @Flow\Proxy(false)
*/
final class UseCaseCollection implements \JsonSerializable
{
/**
* @var array|UseCase[]
*/
private $useCases;

/**
* @param UseCase ...$useCases
*/
private function __construct(UseCase ...$useCases)
{
$this->useCases = $useCases;
}

/**
* @param Prototype $prototype
* @return self
*/
public static function fromPrototype(Prototype $prototype): self
{
$ast = $prototype->getAst();
$useCases = [];

if (isset($ast['__meta']['styleguide']['useCases'])) {
foreach ($ast['__meta']['styleguide']['useCases'] as $useCaseNameAsString => $useCaseAst) {
$useCaseName = UseCaseName::fromString((string)$useCaseNameAsString);
$useCaseTitle = UseCaseTitle::fromString((string)($useCaseAst['title'] ?? $useCaseNameAsString));
$values = $prototype->evaluate(sprintf(
'/__meta/styleguide/useCases/%s/props<Neos.Fusion:DataStructure>',
$useCaseName
));
$overrides = [];

if (is_array($values)) {
foreach ($values as $name => $value) {
if (PropValue::isValid($value)) {
$overrides[(string) $name] =
PropValue::fromAny($value);
}
}
}

$useCases[] = new UseCase($useCaseName, $useCaseTitle, $overrides);
}
}

return new self(...$useCases);
}

/**
* @return array|UseCases[]
*/
public function jsonSerialize()
{
return $this->useCases;
}
}
60 changes: 60 additions & 0 deletions Classes/Domain/PrototypeDetails/UseCases/UseCaseName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
namespace Sitegeist\Monocle\Domain\PrototypeDetails\UseCases;

/**
* This file is part of the Sitegeist.Monocle package
*
* (c) 2020
* Martin Ficzel <[email protected]>
* Wilhelm Behncke <[email protected]>
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;

/**
* @Flow\Proxy(false)
*/
final class UseCaseName implements \JsonSerializable
{
/**
* @var string
*/
private $value;

/**
* @param string $value
*/
private function __construct(string $value)
{
$this->value = $value;
}

/**
* @param string $string
* @return self
*/
public static function fromString(string $string): self
{
return new self($string);
}

/**
* @return string
*/
public function jsonSerialize()
{
return $this->value;
}

/**
* @return string
*/
public function __toString(): string
{
return $this->value;
}
}
Loading

0 comments on commit 6d598d8

Please sign in to comment.