Skip to content

Commit

Permalink
Merge pull request #14 from b13/task/v12
Browse files Browse the repository at this point in the history
[!!!] [TASK] Change Requirements
  • Loading branch information
achimfritz authored Nov 29, 2022
2 parents 12f414f + c2895c7 commit 3a96341
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 141 deletions.
20 changes: 20 additions & 0 deletions Build/sites/main/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
base: 'http://localhost/'
baseVariants: { }
errorHandling: { }
languages:
-
title: english
enabled: true
base: /
typo3Language: default
locale: en_US.UTF-8
iso-639-1: en
navigationTitle: ''
hreflang: ''
direction: ''
flag: global
languageId: '0'
websiteTitle: ''
rootPageId: 1
routes: { }
websiteTitle: 'Testing'
82 changes: 21 additions & 61 deletions Classes/AssetCollector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
namespace B13\Assetcollector;

/*
Expand All @@ -10,51 +11,23 @@
* of the License, or any later version.
*/

use B13\Assetcollector\Resource\ResourceCompressor;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use B13\Assetcollector\Resource\ResourceCompressor;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Main collector class to be used everywhere
*/
class AssetCollector implements SingletonInterface
{
/**
* @var array
*/
protected $inlineCss = [];

/**
* @var array
*/
protected $cssFiles = [];

/**
* Array of JS files which are appended in script tag in head element.
* @var array
*/
protected $jsFiles = [];

/**
* @var array
*/
protected $xmlFiles = [];

/**
* Array of CSS files which are appended in link tag in head element.
*
* @var array
*/
protected $externalCssFiles = [];

/**
* @var ?array
*/
protected $typoScriptConfiguration = null;
protected array $inlineCss = [];
protected array $cssFiles = [];
protected array $jsFiles = [];
protected array $xmlFiles = [];
protected array $externalCssFiles = [];
protected ?array $typoScriptConfiguration = null;

public function addInlineCss(string $inlineCss): void
{
Expand All @@ -66,10 +39,6 @@ public function addCssFile(string $cssFile): void
$this->cssFiles[] = GeneralUtility::getFileAbsFileName($cssFile);
}

/**
* @param string $fileName
* @param string $mediaType
*/
public function addExternalCssFile(string $fileName, string $mediaType = 'all'): void
{
// Only add external css file if not added already.
Expand All @@ -94,7 +63,7 @@ public function addJavaScriptFile(string $fileName, array $additionalAttributes
}
$this->jsFiles[] = [
'fileName' => $fileName,
'additionalAttributes' => $additionalAttributes
'additionalAttributes' => $additionalAttributes,
];
}

Expand Down Expand Up @@ -161,9 +130,8 @@ public function buildInlineCssTag(): string
if (trim($inlineCss) !== '') {
$compressor = GeneralUtility::makeInstance(ResourceCompressor::class);
return '<style class="tx_assetcollector">' . $compressor->publicCompressCssString($inlineCss) . '</style>';
} else {
return '';
}
return '';
}

public function buildJavaScriptIncludes(): string
Expand Down Expand Up @@ -196,7 +164,6 @@ public function buildInlineXmlTag(): string
$xmlFiles = $this->getUniqueXmlFiles();
foreach ($xmlFiles as $xmlFile) {
if (file_exists($xmlFile)) {

$iconIdentifier = $this->getIconIdentifierFromFileName($xmlFile);
$svgInline = '';
$xmlContent = new \DOMDocument();
Expand All @@ -214,7 +181,6 @@ public function buildInlineXmlTag(): string
$inlineXml .= '<symbol id="icon-' . $iconIdentifier . '"' . $viewBoxAttribute . '>'
. $svgInline
. '</symbol>';

}
}
if (trim($inlineXml) !== '') {
Expand All @@ -223,9 +189,8 @@ public function buildInlineXmlTag(): string
. '<defs>'
. $inlineXml
. '</defs></svg>';
} else {
return '';
}
return '';
}

protected function removeUtf8Bom(string $text): string
Expand Down Expand Up @@ -254,23 +219,18 @@ public function getTypoScriptValue(string $name): string

protected function loadTypoScript(): void
{
$this->typoScriptConfiguration = $this->getExtbaseFrameworkConfiguration() ?? [];
$this->typoScriptConfiguration = [];
$frontendController = $this->getTypoScriptFrontendController();
if ($frontendController !== null) {
$this->typoScriptConfiguration = $frontendController->tmpl->setup['plugin.']['tx_assetcollector.']['icons.'] ?? [];
}
}

protected function getExtbaseFrameworkConfiguration(): ?array
protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$configurationManager = $objectManager->get(ConfigurationManager::class);
try {
$extbaseFrameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
if (is_array($extbaseFrameworkConfiguration['plugin.']['tx_assetcollector.']['icons.'])) {
return $extbaseFrameworkConfiguration['plugin.']['tx_assetcollector.']['icons.'];
}
} catch (\TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException $e) {

if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) {
return $GLOBALS['TSFE'];
}
return null;

}

}
9 changes: 4 additions & 5 deletions Classes/Hooks/AssetRenderer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
namespace B13\Assetcollector\Hooks;

/*
Expand Down Expand Up @@ -35,7 +36,6 @@
*/
class AssetRenderer implements SingletonInterface
{

/**
* Called via PageRenderer->render-postProcess(). Get this:
*
Expand Down Expand Up @@ -134,7 +134,7 @@ public function collectInlineAssets($params, TypoScriptFrontendController $front
'jsFiles' => $assetCollector->getJavaScriptFiles(),
'cssFiles' => $assetCollector->getUniqueCssFiles(),
'inlineCss' => $assetCollector->getUniqueInlineCss(),
'xmlFiles' => $assetCollector->getUniqueXmlFiles()
'xmlFiles' => $assetCollector->getUniqueXmlFiles(),
];
$frontendController->config['b13/assetcollector'] = $cached;
}
Expand All @@ -158,8 +158,7 @@ protected function getTypoScriptFrontendController(): ?TypoScriptFrontendControl
{
if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) {
return $GLOBALS['TSFE'];
} else {
return null;
}
return null;
}
}
32 changes: 32 additions & 0 deletions Classes/Listener/AfterCacheableContentIsGenerated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace B13\Assetcollector\Listener;

/*
* This file is part of TYPO3 CMS-based extension "assetcollector" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

use B13\Assetcollector\Hooks\AssetRenderer;
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent;

class AfterCacheableContentIsGenerated
{
protected AssetRenderer $assetRenderer;

public function __construct(AssetRenderer $assetRenderer)
{
$this->assetRenderer = $assetRenderer;
}

public function __invoke(AfterCacheableContentIsGeneratedEvent $event)
{
$frontendController = $event->getController();
$this->assetRenderer->collectInlineAssets([], $frontendController);
}
}
11 changes: 5 additions & 6 deletions Classes/Middleware/InlineSvgInjector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
namespace B13\Assetcollector\Middleware;

/*
Expand All @@ -11,14 +12,14 @@
*/

use B13\Assetcollector\AssetCollector;
use Psr\Http\Server\MiddlewareInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Middleware to add inline SVGs at the end of the HTML <body> tag.
Expand Down Expand Up @@ -47,7 +48,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$body->write($content);
$response = $response->withBody($body);
}

}
return $response;
}
Expand Down Expand Up @@ -82,5 +82,4 @@ protected function getTypoScriptFrontendController(): ?TypoScriptFrontendControl
{
return $GLOBALS['TSFE'] ?? null;
}

}
11 changes: 4 additions & 7 deletions Classes/Resource/ResourceCompressor.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
namespace B13\Assetcollector\Resource;

/*
Expand All @@ -14,12 +15,8 @@
* This new class is used to allow to separately compress CSS code, which is not possible
* by directly using TYPO3 Core.
*/
class ResourceCompressor extends \TYPO3\CMS\Core\Resource\ResourceCompressor {

/**
* @param $content
* @return string
*/
class ResourceCompressor extends \TYPO3\CMS\Core\Resource\ResourceCompressor
{
public function publicCompressCssString(string $content): string
{
return $this->compressCssString($content);
Expand Down
14 changes: 4 additions & 10 deletions Classes/ViewHelpers/CssViewHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
namespace B13\Assetcollector\ViewHelpers;

/*
Expand All @@ -18,15 +19,9 @@
*/
class CssViewHelper extends AbstractViewHelper
{
/**
* @var AssetCollector
*/
protected $assetCollector;
protected AssetCollector $assetCollector;

/**
* @param AssetCollector $assetCollector
*/
public function injectAssetCollector(AssetCollector $assetCollector): void
public function __construct(AssetCollector $assetCollector)
{
$this->assetCollector = $assetCollector;
}
Expand All @@ -44,7 +39,6 @@ public function initializeArguments(): void
'external',
'boolean',
'Specifies if the given CSS file should be loaded within link tag.'

);
$this->registerArgument(
'media',
Expand Down
26 changes: 6 additions & 20 deletions Classes/ViewHelpers/JsViewHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
namespace B13\Assetcollector\ViewHelpers;

/*
Expand All @@ -15,24 +16,13 @@

class JsViewHelper extends AbstractViewHelper
{
protected AssetCollector $assetCollector;

/**
* @var AssetCollector
*/
protected $assetCollector;

/**
* @param AssetCollector $assetCollector
*/
public function injectAssetCollector(AssetCollector $assetCollector): void
public function __construct(AssetCollector $assetCollector)
{
$this->assetCollector = $assetCollector;
}

/**
* @return void
* @api
*/
public function initializeArguments(): void
{
parent::initializeArguments();
Expand All @@ -51,16 +41,12 @@ public function initializeArguments(): void
);
}

/**
* @return void
*/
public function render(): void
{
if (!empty($this->arguments['file'])) {
$this->assetCollector->addJavaScriptFile($this->arguments['file'], $this->arguments['additionalAttributes']);
} else {
// @todo
// $this->assetCollector->addInlineJavaScript($this->renderChildren());
}
// @todo
// $this->assetCollector->addInlineJavaScript($this->renderChildren());
}
}
Loading

0 comments on commit 3a96341

Please sign in to comment.