Skip to content

Commit 36ddf16

Browse files
committed
[TASK] Extract AssetPublisher
1 parent 24ea914 commit 36ddf16

2 files changed

Lines changed: 66 additions & 42 deletions

File tree

Classes/Loader/AssetPublisher.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the TYPO3 CMS project.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*
12+
* For the full copyright and license information, please read the
13+
* LICENSE.txt file that was distributed with this source code.
14+
*
15+
* The TYPO3 project - inspiring people to share!
16+
*/
17+
18+
namespace TYPO3\CMS\ContentBlocks\Loader;
19+
20+
use Symfony\Component\Filesystem\Exception\IOException;
21+
use Symfony\Component\Filesystem\Filesystem;
22+
use TYPO3\CMS\ContentBlocks\Utility\ContentBlockPathUtility;
23+
use TYPO3\CMS\Core\Utility\GeneralUtility;
24+
25+
class AssetPublisher
26+
{
27+
/**
28+
* @param LoadedContentBlock[] $loadedContentBlocks
29+
*/
30+
public function publishAssets(array $loadedContentBlocks): void
31+
{
32+
$fileSystem = new Filesystem();
33+
foreach ($loadedContentBlocks as $loadedContentBlock) {
34+
$hostExtension = $loadedContentBlock->getHostExtension();
35+
$contentBlockExtPublicPath = $loadedContentBlock->getExtPath() . '/' . ContentBlockPathUtility::getAssetsFolder();
36+
$contentBlockAbsolutePublicPath = GeneralUtility::getFileAbsFileName($contentBlockExtPublicPath);
37+
// If the Content Block does not have an Assets folder, nothing to publish here.
38+
if (!file_exists($contentBlockAbsolutePublicPath)) {
39+
continue;
40+
}
41+
$hostAbsolutePublicContentBlockBasePath = ContentBlockPathUtility::getHostAbsolutePublicContentBlockBasePath($hostExtension);
42+
// Prevent symlinks from being added to git index.
43+
$gitIgnorePath = $hostAbsolutePublicContentBlockBasePath . '/.gitignore';
44+
if (!file_exists($gitIgnorePath)) {
45+
GeneralUtility::mkdir_deep($hostAbsolutePublicContentBlockBasePath);
46+
file_put_contents($gitIgnorePath, '*');
47+
}
48+
$hostAbsolutePublicContentBlockBasePathWithVendor = $hostAbsolutePublicContentBlockBasePath . '/' . $loadedContentBlock->getVendor();
49+
$contentBlockRelativePublicPath = $fileSystem->makePathRelative(
50+
$contentBlockAbsolutePublicPath,
51+
$hostAbsolutePublicContentBlockBasePathWithVendor
52+
);
53+
$hostAbsolutePublicContentBlockPath = ContentBlockPathUtility::getHostAbsolutePublicContentBlockPath(
54+
$hostExtension,
55+
$loadedContentBlock->getName(),
56+
);
57+
try {
58+
$fileSystem->symlink($contentBlockRelativePublicPath, $hostAbsolutePublicContentBlockPath);
59+
} catch (IOException) {
60+
$fileSystem->mirror($contentBlockAbsolutePublicPath, $hostAbsolutePublicContentBlockPath);
61+
}
62+
}
63+
}
64+
}

Classes/Loader/ContentBlockLoader.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
namespace TYPO3\CMS\ContentBlocks\Loader;
1919

2020
use Symfony\Component\DependencyInjection\Attribute\Autowire;
21-
use Symfony\Component\Filesystem\Exception\IOException;
22-
use Symfony\Component\Filesystem\Filesystem;
2321
use Symfony\Component\Finder\Finder;
2422
use Symfony\Component\Yaml\Yaml;
2523
use TYPO3\CMS\ContentBlocks\Basics\BasicsLoader;
@@ -37,7 +35,6 @@
3735
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
3836
use TYPO3\CMS\Core\Package\PackageManager;
3937
use TYPO3\CMS\Core\Resource\FileType;
40-
use TYPO3\CMS\Core\Utility\GeneralUtility;
4138

4239
/**
4340
* Main bootstrap loader for Content Blocks. This class finds registered
@@ -79,6 +76,7 @@ public function __construct(
7976
protected readonly BasicsLoader $basicsLoader,
8077
protected readonly PackageManager $packageManager,
8178
protected readonly IconProcessor $iconProcessor,
79+
protected readonly AssetPublisher $assetPublisher,
8280
) {}
8381

8482
public function load(): ContentBlockRegistry
@@ -123,7 +121,7 @@ public function loadUncached(): ContentBlockRegistry
123121
usort($loadedContentBlocks, $sortByPriority);
124122
$contentBlockRegistry = $this->fillContentBlockRegistry($loadedContentBlocks);
125123

126-
$this->publishAssets($loadedContentBlocks);
124+
$this->assetPublisher->publishAssets($loadedContentBlocks);
127125
$this->iconProcessor->process();
128126

129127
return $contentBlockRegistry;
@@ -317,44 +315,6 @@ protected function createPageIcon(
317315
return $pageIcon;
318316
}
319317

320-
/**
321-
* @param LoadedContentBlock[] $loadedContentBlocks
322-
*/
323-
protected function publishAssets(array $loadedContentBlocks): void
324-
{
325-
$fileSystem = new Filesystem();
326-
foreach ($loadedContentBlocks as $loadedContentBlock) {
327-
$hostExtension = $loadedContentBlock->getHostExtension();
328-
$contentBlockExtPublicPath = $loadedContentBlock->getExtPath() . '/' . ContentBlockPathUtility::getAssetsFolder();
329-
$contentBlockAbsolutePublicPath = GeneralUtility::getFileAbsFileName($contentBlockExtPublicPath);
330-
// If the Content Block does not have an Assets folder, nothing to publish here.
331-
if (!file_exists($contentBlockAbsolutePublicPath)) {
332-
continue;
333-
}
334-
$hostAbsolutePublicContentBlockBasePath = ContentBlockPathUtility::getHostAbsolutePublicContentBlockBasePath($hostExtension);
335-
// Prevent symlinks from being added to git index.
336-
$gitIgnorePath = $hostAbsolutePublicContentBlockBasePath . '/.gitignore';
337-
if (!file_exists($gitIgnorePath)) {
338-
GeneralUtility::mkdir_deep($hostAbsolutePublicContentBlockBasePath);
339-
file_put_contents($gitIgnorePath, '*');
340-
}
341-
$hostAbsolutePublicContentBlockBasePathWithVendor = $hostAbsolutePublicContentBlockBasePath . '/' . $loadedContentBlock->getVendor();
342-
$contentBlockRelativePublicPath = $fileSystem->makePathRelative(
343-
$contentBlockAbsolutePublicPath,
344-
$hostAbsolutePublicContentBlockBasePathWithVendor
345-
);
346-
$hostAbsolutePublicContentBlockPath = ContentBlockPathUtility::getHostAbsolutePublicContentBlockPath(
347-
$hostExtension,
348-
$loadedContentBlock->getName(),
349-
);
350-
try {
351-
$fileSystem->symlink($contentBlockRelativePublicPath, $hostAbsolutePublicContentBlockPath);
352-
} catch (IOException) {
353-
$fileSystem->mirror($contentBlockAbsolutePublicPath, $hostAbsolutePublicContentBlockPath);
354-
}
355-
}
356-
}
357-
358318
protected function getFromCache(): false|array
359319
{
360320
return $this->cache->require('ContentBlocks_Raw');

0 commit comments

Comments
 (0)