Skip to content

Commit

Permalink
improve bundle connector management (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed May 24, 2023
1 parent 977b1a2 commit a2f392f
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 107 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -15,7 +15,7 @@ The Toolbox is a Kickstarter for your every day project. It provides some import

| Release | Supported Pimcore Versions | Supported Symfony Versions | Release Date | Maintained | Branch |
|---------|-----------------------------------|----------------------------|--------------|----------------|------------|
| **4.x** | `10.1` - `10.5` | `5.4` | 01.10.2021 | Feature Branch | master |
| **4.x** | `10.5` - `10.6 ` | `5.4` | 01.10.2021 | Feature Branch | master |
| **3.x** | `6.0` - `6.9` | `3.4`, `^4.4` | 16.07.2019 | Unsupported | 3.x |
| **2.8** | `5.4`, `5.5`, `5.6`, `5.7`, `5.8` | `3.4` | 30.06.2019 | Unsupported | 2.8 |
| **1.8** | `4.0` | -- | 28.04.2017 | Unsupported | pimcore4 |
Expand All @@ -24,7 +24,7 @@ The Toolbox is a Kickstarter for your every day project. It provides some import

```json
"require" : {
"dachcom-digital/toolbox" : "~4.0.0"
"dachcom-digital/toolbox" : "~4.1.0"
}
```

Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
@@ -1,5 +1,11 @@
# Upgrade Notes

## Version 4.1.0
- [FEATURE] PIMCORE 10.5 support only
- [IMPROVEMENT] Compatibility with Members 4.1 added
- [IMPROVEMENT] Remove unused js classes
- [BUGFIX] [VHS Element] Use right element context in video type selector

## Version 4.0.10
- [BUGFIX] fix case sensitivity for accordion in bootstrap3 theme `Resources/views/toolbox/bootstrap3/accordion/partial/Accordion => Resources/views/toolbox/bootstrap3/accordion/partial/accordion`

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -31,7 +31,7 @@
}
},
"require": {
"pimcore/pimcore": "^10.1"
"pimcore/pimcore": "^10.5"
},
"require-dev": {
"codeception/codeception": "^4.1",
Expand Down
17 changes: 5 additions & 12 deletions src/ToolboxBundle/Connector/BundleConnector.php
Expand Up @@ -2,17 +2,16 @@

namespace ToolboxBundle\Connector;

use Pimcore\Extension\Bundle\PimcoreBundleManager;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class BundleConnector
{
protected PimcoreBundleManager $bundleManager;
protected array $activeBundles = [];
protected array $services = [];

public function __construct(PimcoreBundleManager $bundleManager)
public function addActiveBundle(string $bundleName): void
{
$this->bundleManager = $bundleManager;
$this->activeBundles[$bundleName] = true;
}

public function registerBundleService(string $serviceId, mixed $service): void
Expand All @@ -29,14 +28,8 @@ public function getBundleService(string $serviceId): mixed
return $this->services[$serviceId];
}

public function hasBundle(string $bundleName = 'ExtensionBundle\ExtensionBundle'): bool
public function hasBundle(string $bundleName): bool
{
try {
$hasExtension = $this->bundleManager->isEnabled($bundleName);
} catch (\Exception $e) {
$hasExtension = false;
}

return $hasExtension;
return array_key_exists($bundleName, $this->activeBundles);
}
}
Expand Up @@ -18,21 +18,17 @@ public function process(ContainerBuilder $container): void
return;
}

$toolboxBundleConnector = $container->getDefinition(BundleConnector::class);
foreach ($this->getRequiredServices() as $service) {
$toolboxBundleConnector->addMethodCall(
'registerBundleService',
[$service, new Reference($service)]
);
}
}

private function getRequiredServices(): array
{
return [
$requiredServices = [
\MembersBundle\Manager\RestrictionManager::class,
\MembersBundle\Security\RestrictionUri::class,
\MembersBundle\Security\RestrictionQuery::class,
];

$toolboxBundleConnector = $container->getDefinition(BundleConnector::class);
$toolboxBundleConnector->addMethodCall('addActiveBundle', ['MembersBundle']);

foreach ($requiredServices as $service) {
$toolboxBundleConnector->addMethodCall('registerBundleService', [$service, new Reference($service)]);
}
}
}
20 changes: 7 additions & 13 deletions src/ToolboxBundle/Document/Areabrick/Download/Download.php
Expand Up @@ -2,6 +2,7 @@

namespace ToolboxBundle\Document\Areabrick\Download;

use Doctrine\DBAL\Query\QueryBuilder;
use Pimcore\Model\Document\Editable\Relations;
use Symfony\Component\HttpFoundation\Response;
use ToolboxBundle\Connector\BundleConnector;
Expand All @@ -11,11 +12,8 @@

class Download extends AbstractAreabrick
{
protected BundleConnector $bundleConnector;

public function __construct(BundleConnector $bundleConnector)
public function __construct(protected BundleConnector $bundleConnector)
{
$this->bundleConnector = $bundleConnector;
}

public function action(Info $info): ?Response
Expand Down Expand Up @@ -44,7 +42,7 @@ public function action(Info $info): ?Response

protected function getByFile(Asset $node): array
{
if (!$this->hasMembersBundle()) {
if (!$this->bundleConnector->hasBundle('MembersBundle')) {
return [$node];
}

Expand All @@ -64,9 +62,10 @@ protected function getByFolder(Asset\Folder $node): array
$assetListing->addConditionParam('path LIKE ?', $fullPath . '%');
$assetListing->addConditionParam('type != ?', 'folder');

if ($this->hasMembersBundle()) {
$assetListing->onCreateQueryBuilder(function (\Doctrine\DBAL\Query\QueryBuilder $query) use ($assetListing) {
$this->bundleConnector->getBundleService(\MembersBundle\Security\RestrictionQuery::class)
if ($this->bundleConnector->hasBundle('MembersBundle')) {
$assetListing->onCreateQueryBuilder(function (QueryBuilder $query) use ($assetListing) {
$this->bundleConnector
->getBundleService(\MembersBundle\Security\RestrictionQuery::class)
->addRestrictionInjection($query, $assetListing, 'id');
});
}
Expand All @@ -77,11 +76,6 @@ protected function getByFolder(Asset\Folder $node): array
return $assetListing->getAssets();
}

protected function hasMembersBundle(): bool
{
return $this->bundleConnector->hasBundle('MembersBundle\MembersBundle');
}

public function getName(): string
{
return 'Downloads';
Expand Down
6 changes: 0 additions & 6 deletions src/ToolboxBundle/Resources/public/js/backend/toolbox.js

This file was deleted.

Expand Up @@ -93,11 +93,11 @@ pimcore.plugin.toolbox.vhs.editor = Class.create({
}.bind(this),
keyup: function (el) {
if ((el.getValue().indexOf('youtu.be') >= 0 || el.getValue().indexOf('youtube.com') >= 0) && el.getValue().indexOf('http') >= 0) {
this.up('form').getComponent('type').setValue('youtube');
el.up('form').getComponent('type').setValue('youtube');
} else if (el.getValue().indexOf('vimeo') >= 0 && el.getValue().indexOf('http') >= 0) {
this.up('form').getComponent('type').setValue('vimeo');
el.up('form').getComponent('type').setValue('vimeo');
} else if ((el.getValue().indexOf('dai.ly') >= 0 || el.getValue().indexOf('dailymotion') >= 0) && el.getValue().indexOf('http') >= 0) {
this.up('form').getComponent('type').setValue('dailymotion');
el.up('form').getComponent('type').setValue('dailymotion');
}
}.bind(this)
}
Expand Down Expand Up @@ -334,7 +334,6 @@ pimcore.plugin.toolbox.vhs.editor = Class.create({
values['videoParameter'] = videoParameter;

return values;

},

hideWindow: function () {
Expand All @@ -344,7 +343,6 @@ pimcore.plugin.toolbox.vhs.editor = Class.create({
this.form = {};

document.body.classList.remove('toolbox-modal-open');

},

onNodeOver: function (target, dd, e, data) {
Expand Down
13 changes: 0 additions & 13 deletions src/ToolboxBundle/Resources/public/js/startup.js

This file was deleted.

4 changes: 1 addition & 3 deletions src/ToolboxBundle/ToolboxBundle.php
Expand Up @@ -41,15 +41,13 @@ public function getJsPaths(): array
return [
'/admin/toolbox-ckeditor-object-style.js',
'/bundles/toolbox/js/toolbox-ckeditor-plugins.js',
'/bundles/toolbox/js/document/edit.js',
'/bundles/toolbox/js/startup.js',
'/bundles/toolbox/js/document/edit.js'
];
}

public function getEditmodeJsPaths(): array
{
return [
'/bundles/toolbox/js/backend/toolbox.js',
'/bundles/toolbox/js/toolbox-ckeditor-plugins.js',
'/bundles/toolbox/js/document/editables/areablock.js',
'/bundles/toolbox/js/document/editables/googlemap.js',
Expand Down
63 changes: 23 additions & 40 deletions src/ToolboxBundle/Twig/Extension/DownloadExtension.php
Expand Up @@ -2,6 +2,7 @@

namespace ToolboxBundle\Twig\Extension;

use Pimcore\File;
use Pimcore\Model\Asset;
use ToolboxBundle\Connector\BundleConnector;
use Pimcore\Translation\Translator;
Expand All @@ -11,15 +12,11 @@

class DownloadExtension extends AbstractExtension
{
protected ConfigManagerInterface $configManager;
protected BundleConnector $bundleConnector;
protected Translator $translator;

public function __construct(ConfigManagerInterface $configManager, BundleConnector $bundleConnector, Translator $translator)
{
$this->configManager = $configManager;
$this->bundleConnector = $bundleConnector;
$this->translator = $translator;
public function __construct(
protected ConfigManagerInterface $configManager,
protected BundleConnector $bundleConnector,
protected Translator $translator
) {
}

public function getFunctions(): array
Expand All @@ -35,14 +32,9 @@ public function getFunctions(): array
}

/**
* @param string|array $areaType toolbox element or custom config
* @param null|object $element related element to track
*
* @return string
*
* @throws \Exception
*/
public function getDownloadTracker($areaType, $element = null)
public function getDownloadTracker(mixed $areaType, mixed $element = null): string
{
if (empty($areaType)) {
return '';
Expand All @@ -62,7 +54,7 @@ public function getDownloadTracker($areaType, $element = null)

$str = 'data-tracking="active" ';

$str .= implode(' ', array_map(function ($key) use ($trackerInfo, $element) {
$str .= implode(' ', array_map(static function ($key) use ($trackerInfo, $element) {
$val = $trackerInfo[$key];

if (is_bool($val)) {
Expand All @@ -85,20 +77,19 @@ public function getDownloadTracker($areaType, $element = null)
}

/**
* @param Asset $download
* @param bool $showPreviewImage
* @param string $fileSizeUnit
* @param int $fileSizePrecision
* @param bool $showFileNameIfTitleEmpty
*
* @return array
*
* @throws \Exception
*/
public function getDownloadInfo($download, $showPreviewImage = false, $fileSizeUnit = 'optimized', $fileSizePrecision = 0, $showFileNameIfTitleEmpty = false)
{
if ($this->bundleConnector->hasBundle('MembersBundle\MembersBundle') === true
&& strpos($download->getFullPath(), \MembersBundle\Security\RestrictionUri::PROTECTED_ASSET_FOLDER) !== false
public function getDownloadInfo(
Asset $download,
bool $showPreviewImage = false,
string $fileSizeUnit = 'optimized',
int $fileSizePrecision = 0,
bool $showFileNameIfTitleEmpty = false
): array {

if (
$this->bundleConnector->hasBundle('MembersBundle') === true &&
$this->bundleConnector->getBundleService(\MembersBundle\Manager\RestrictionManager::class)->elementIsInProtectedStorageFolder($download)
) {
$dPath = $this->bundleConnector->getBundleService(\MembersBundle\Security\RestrictionUri::class)->generateAssetUrl($download);
} else {
Expand All @@ -112,7 +103,7 @@ public function getDownloadInfo($download, $showPreviewImage = false, $fileSizeU
$dSize = $download->getFileSize($fileSizeUnit, $fileSizePrecision);
}

$dType = \Pimcore\File::getFileExtension($download->getFilename());
$dType = File::getFileExtension($download->getFilename());
$downloadTitle = $showFileNameIfTitleEmpty ? $download->getFilename() : $this->translator->trans('Download', [], 'admin');
$dName = ($download->getMetadata('title')) ?: $downloadTitle;
$dAltText = $download->getMetadata('alt') ?: '';
Expand All @@ -122,8 +113,8 @@ public function getDownloadInfo($download, $showPreviewImage = false, $fileSizeU
$previewThumbName = $this->configManager->getImageThumbnailFromConfig('download_preview_thumbnail');

if ($showPreviewImage) {
/** @var Asset|null $metaPreviewImage */
$metaPreviewImage = $download->getMetadata('previewImage');
/** @phpstan-ignore-next-line */
if ($metaPreviewImage instanceof Asset\Image) {
$dPreviewImage = $metaPreviewImage->getThumbnail($previewThumbName);
} elseif ($download instanceof Asset\Image) {
Expand Down Expand Up @@ -157,15 +148,7 @@ public function getDownloadInfo($download, $showPreviewImage = false, $fileSizeU
];
}

/**
* Get optimized file size.
*
* @param int $bytes
* @param int $precision
*
* @return string
*/
public function getOptimizedFileSize($bytes, $precision)
public function getOptimizedFileSize(mixed $bytes, int $precision): string
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2);
Expand All @@ -184,6 +167,6 @@ public function getOptimizedFileSize($bytes, $precision)
$format = 'bytes';
}

return round((float)$bytes, $precision) . ' ' . $format;
return round((float) $bytes, $precision) . ' ' . $format;
}
}

0 comments on commit a2f392f

Please sign in to comment.