Skip to content

Commit

Permalink
Merge pull request #64 from sitegeist/feature/updateLintingToMoreRece…
Browse files Browse the repository at this point in the history
…ntPracrtices

TASK: Update linting to more recent practices
  • Loading branch information
mficzel committed Aug 29, 2023
2 parents 30c9a4d + 3c95847 commit 36daba0
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/DummyImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function imageAction(int $w = 600, int $h = 400, string $bg = '#000', str
} elseif (method_exists($this->response, 'setComponentParameter') && class_exists('\Neos\Flow\Http\Component\SetHeaderComponent')) {
$this->response->setComponentParameter(\Neos\Flow\Http\Component\SetHeaderComponent::class, 'Cache-Control', 'max-age=883000000');
}
$this->response->setContentType('image/'.$f);
$this->response->setContentType('image/' . $f);

return $result;
} else {
Expand Down
19 changes: 10 additions & 9 deletions Classes/Domain/AbstractImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ abstract class AbstractImageSource implements ImageSourceInterface, ProtectedCon
protected $logger;

/**
* @var array[]
* @var mixed[]
*
* @Flow\InjectConfiguration(path="thumbnailPresets", package="Neos.Media")
*/
protected $thumbnailPresets;

/**
* @var array[]
* @var mixed[]
*
* @Flow\InjectConfiguration(path="variantPresets", package="Neos.Media")
*/
Expand Down Expand Up @@ -219,11 +219,11 @@ public function srcset($mediaDescriptors): string
$width = (int) $matches['width'];
$scaleFactor = $width / $this->width();
$scaled = $this->scale($scaleFactor);
$srcsetArray[] = $scaled->src().' '.$width.'w';
$srcsetArray[] = $scaled->src() . ' ' . $width . 'w';
} elseif (preg_match('/^(?<factor>[0-9\\.]+)x$/u', $descriptor, $matches)) {
$factor = (float) $matches['factor'];
$scaled = $this->scale($factor);
$srcsetArray[] = $scaled->src().' '.$factor.'x';
$srcsetArray[] = $scaled->src() . ' ' . $factor . 'x';
}
}

Expand Down Expand Up @@ -294,9 +294,10 @@ public function height(): ?int
*/
public function allowsCallOfMethod($methodName)
{
if (in_array(
$methodName,
[
if (
in_array(
$methodName,
[
'withAlt',
'withTitle',
'withDimensions',
Expand All @@ -321,8 +322,8 @@ public function allowsCallOfMethod($methodName)
'alt',
'width',
'height',
]
)
]
)
) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/AssetImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function dataSrc(): string
if (is_resource($stream)) {
if ($content = stream_get_contents($stream)) {
if (is_string($content)) {
return 'data:image/png;base64,'.base64_encode($content);
return 'data:image/png;base64,' . base64_encode($content);
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions Classes/Domain/DummyImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class DummyImageGenerator
public function initializeObject(): void
{
if (isset($this->settings['dummyImage']['overrideImagineDriver']) && $this->settings['dummyImage']['overrideImagineDriver'] !== false) {
$className = 'Imagine\\'.$this->settings['dummyImage']['overrideImagineDriver'].'\\Imagine';
$this->imagineService = new $className();
$className = 'Imagine\\' . $this->settings['dummyImage']['overrideImagineDriver'] . '\\Imagine';
if (is_a($className, ImagineInterface::class, true)) {
$this->imagineService = new $className();
} else {
throw new \Exception($className . ' does not implement the ImagineInterface');
}
}
}

Expand Down Expand Up @@ -246,7 +250,7 @@ protected function renderBorder(ImageInterface $image, ColorInterface $foregroun
protected function renderText(ImageInterface $image, ColorInterface $textColor, int $width, int $height, string $text, bool $center = false): void
{
$initialFontSize = 10;
$fontFile = $this->packageManager->getPackage('Sitegeist.Kaleidoscope')->getPackagePath().'Resources/Private/Font/NotoSans-Regular.ttf';
$fontFile = $this->packageManager->getPackage('Sitegeist.Kaleidoscope')->getPackagePath() . 'Resources/Private/Font/NotoSans-Regular.ttf';
$initialFont = $this->imagineService->font($fontFile, $initialFontSize, $textColor);

// scale text to fit the image
Expand Down Expand Up @@ -298,7 +302,6 @@ protected function renderPattern(ImageInterface $image, ColorInterface $patternC
if (
$i === $k ||
($i % 2 && $k % 2)

) {
$image->draw()->dot(new Point($borderWidth + $i, $borderWidth + $k), $patternColor);
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/DummyImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function src(): string
$arguments['f'] = $this->targetFormat;
}

return $this->baseUri.'?'.http_build_query($arguments);
return $this->baseUri . '?' . http_build_query($arguments);
}

public function dataSrc(): string
Expand All @@ -119,7 +119,7 @@ public function dataSrc(): string

$dummyImage = $this->dummyImageGenerator->createDummyImage($w, $h, $bg, $fg, $t, $f);
if ($dummyImage) {
return 'data:image/'.$f.';base64,'.base64_encode($dummyImage->get($f));
return 'data:image/' . $f . ';base64,' . base64_encode($dummyImage->get($f));
}

return '';
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/ResourceImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public function src(): string
public function dataSrc(): string
{
if ($this->package) {
$content = file_get_contents('resource://'.$this->package.'/'.$this->path);
$content = file_get_contents('resource://' . $this->package . '/' . $this->path);
} else {
$content = file_get_contents($this->path);
}

if ($content) {
$extension = pathinfo($this->path, PATHINFO_EXTENSION);

return 'data:image/'.$extension.';base64,'.base64_encode($content);
return 'data:image/' . $extension . ';base64,' . base64_encode($content);
} else {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/UriImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function dataSrc(): string
if ($content) {
$extension = pathinfo($this->uri, PATHINFO_EXTENSION);

return 'data:image/'.$extension.';base64,'.base64_encode($content);
return 'data:image/' . $extension . ';base64,' . base64_encode($content);
} else {
return '';
}
Expand Down
157 changes: 157 additions & 0 deletions Resources/Private/Fusion/Prototypes/ImageBase64.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
prototype(Sitegeist.Kaleidoscope:ImageBase64) < prototype(Neos.Fusion:Component) {

@styleguide {

title = "ImageBase64"

props {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource
width= 1000
height = 500
}

propSets {
altAndTitleFromSource {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
alt = 'Alternate assigned to source'
title = 'Title assigned to source'
}
}

imageSourceWithWidth {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
@process.adjust = ${value.withWidth(400)}
}
}

imageSourceWithHeight {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
@process.adjust = ${value.withHeight(400)}
}
}

imageSourceWithDimensions {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
@process.adjust = ${value.withDimensions(400, 400)}
}
}

imageSourceWithTitleAndAlt {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
@process.adjust = ${value.withAlt("Alternate assigned by eel").withTitle("Title assigned by eel")}
}
}

imageSourceWithFormat {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
@process.adjust = ${value.withFormat("png")}
}
}

overrideAltAndTitleFromProp {
alt = 'Alternate assigned as prop'
title = 'Title assigned as prop'
}

withResourceImageSource {
imageSource = Sitegeist.Kaleidoscope:ResourceImageSource {
path = "resource://Sitegeist.Kaleidoscope/Public/Images/imageError.png"
alt = 'Alternate assigned to source'
title = 'Title assigned to source'
}
}

withUriImageSource {
imageSource = Sitegeist.Kaleidoscope:UriImageSource {
uri = "https://dummyimage.com/600x400/000/fff"
alt = 'Alternate assigned to source'
title = 'Title assigned to source'
}
}

withAttributes {
attributes = Neos.Fusion:DataStructure {
data-foo="bar"
style="border: 5px solid pink;"
}
}

multires_array {
srcset = ${['1x', '1.5x', '2x']}
}

multires_string {
srcset = '1x, 1.5x, 2x'
}

multisize_array {
srcset = ${['320w', '400w', '600w', '800w', '1000w', '1200w', '1600']}
}

multisize_string {
srcset = '320w, 400ww, 600w, 800w, 1000w, 1200w, 1600'
}

sizes_array {
srcset = ${['320w', '400w', '600w', '800w', '1000w', '1200w', '1600']}
sizes = ${['(max-width: 320px) 280px', '(max-width: 480px) 440px', '800px']}
}

sizes_string {
srcset = '320w, 400ww, 600w, 800w, 1000w, 1200w, 1600'
sizes = '(max-width: 320px) 280px, (max-width: 480px) 440px, 800px'
}
}
}

@propTypes {
imageSource = ${PropTypes.instanceOf('\\Sitegeist\\Kaleidoscope\\Domain\\ImageSourceInterface')}
}

imageSource = null
srcset = null
sizes = null
alt = null
title = null
// class is deprecated in favor of attributes.class
class = null
loading = 'lazy'
width = null
height = null
format = null
attributes = Neos.Fusion:DataStructure
renderDimensionAttributes = true

renderer = Neos.Fusion:Component {
@if.hasImageSource = ${props.imageSource && Type.instance(props.imageSource, '\\Sitegeist\\Kaleidoscope\\Domain\\ImageSourceInterface')}

# apply format, width and height to the imageSource
imageSource = ${props.imageSource}
[email protected] = ${props.width ? value.withWidth(props.width) : value}
[email protected] = ${props.height ? value.withHeight(props.height) : value}
[email protected] = ${props.format ? value.withFormat(props.format) : value}

srcset = ${props.srcset}
sizes = ${props.sizes}
loading = ${props.loading}
alt = ${props.alt}
title = ${props.title}
class = ${props.class}
attributes = ${props.attributes}
renderDimensionAttributes = ${props.renderDimensionAttributes}

renderer = afx`
<img
src_={props.imageSource.src()}
style={'background-size:cover; background-repeat:no-repeat; background-image: url(' + props.imageSource.withWidth('100', true).dataSrc() + ');'}
onload="console.log('load')"
class={props.class}
alt={props.alt || props.imageSource.alt() || ''}
title={props.title || props.imageSource.title()}
width={props.renderDimensionAttributes ? props.imageSource.width() : null}
height={props.renderDimensionAttributes ? props.imageSource.height() : null}
{...props.attributes}
/>
`
}
}
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"neos/imagine": "*"
},
"require-dev": {
"phpstan/phpstan": "^0.12.78"
"phpstan/phpstan": "^1.8",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {
Expand All @@ -23,8 +24,11 @@
}
},
"scripts": {
"test:stan": ["vendor/bin/phpstan analyse Classes"],
"test": ["composer install", "composer test:stan"]
"fix:style": "phpcbf --colors --standard=PSR12 Classes",
"test:style": "phpcs --colors -n --standard=PSR12 Classes",
"test:stan": "phpstan analyse Classes",
"cc": "phpstan clear cache",
"test": ["composer install", "composer test:style" , "composer test:stan"]
},
"config": {
"allow-plugins": {
Expand Down

0 comments on commit 36daba0

Please sign in to comment.