Skip to content

Commit

Permalink
Add compiler pass for filterinterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 15, 2024
1 parent 0940db0 commit 9ed2136
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 22 deletions.
5 changes: 4 additions & 1 deletion config/filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ services:
autoconfigure: true
public: false


#Filter
Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterLoaderInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\Filter\Loader\TaggedIteratorAdapter

Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterService

Pimcore\Bundle\StudioApiBundle\Filter\PageFilter:
tags: [ 'pimcore.studio_api.collection.filter' ]

Expand Down
5 changes: 0 additions & 5 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ services:
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Unknown: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\UnknownHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\VideoHydratorInterface'


#Filter
Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterService

#Factory
Pimcore\Bundle\StudioApiBundle\Factory\QueryFactoryInterface:
class: Pimcore\Bundle\StudioApiBundle\Factory\QueryFactory
Expand Down
48 changes: 48 additions & 0 deletions src/DependencyInjection/CompilerPass/FilterPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\DependencyInjection\CompilerPass;

use Pimcore\Bundle\StudioApiBundle\Exception\MustImplementInterfaceException;
use Pimcore\Bundle\StudioApiBundle\Filter\FilterInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\Loader\TaggedIteratorAdapter;
use Pimcore\Bundle\StudioApiBundle\Util\Traits\MustImplementInterfaceTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @internal
*/
final class FilterPass implements CompilerPassInterface
{
use MustImplementInterfaceTrait;

/**
* @throws MustImplementInterfaceException
*/
public function process(ContainerBuilder $container): void
{
$taggedServices = array_keys(
$container->findTaggedServiceIds(
TaggedIteratorAdapter::FILTER_TAG
)
);

foreach ($taggedServices as $environmentType) {
$this->checkInterface($environmentType, FilterInterface::class);
}
}
}
26 changes: 26 additions & 0 deletions src/Exception/MustImplementInterfaceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Exception;

use Exception;

/**
* @internal
*/
final class MustImplementInterfaceException extends Exception
{
}
4 changes: 1 addition & 3 deletions src/Filter/ExcludeFolderFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class ExcludeFolderFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): mixed
public function apply(Collection $collection, QueryInterface $query): QueryInterface
{
$excludeFolders = $collection->getExcludeFolders();
if(!$excludeFolders) {
return $query;
}

/** @var AssetQuery $query */
return $query->excludeFolders();
}
}
2 changes: 1 addition & 1 deletion src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

interface FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): mixed;
public function apply(Collection $collection, QueryInterface $query): QueryInterface;
}
4 changes: 1 addition & 3 deletions src/Filter/IdSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class IdSearchFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): mixed
public function apply(Collection $collection, QueryInterface $query): QueryInterface
{
$idSearchTerm = $collection->getIdSearchTerm();

if(!$idSearchTerm) {
return $query;
}

/** @var AssetQuery $query */
return $query->setSearchTerm($idSearchTerm);
}
}
2 changes: 1 addition & 1 deletion src/Filter/PageFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

final class PageFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): mixed
public function apply(Collection $collection, QueryInterface $query): QueryInterface
{
return $query->setPage($collection->getPage());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/PageSizeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

final class PageSizeFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): mixed
public function apply(Collection $collection, QueryInterface $query): QueryInterface
{
return $query->setPageSize($collection->getPageSize());
}
Expand Down
5 changes: 2 additions & 3 deletions src/Filter/ParentIdFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class ParentIdFilter implements FilterInterface
{
public function apply(Collection $collection, mixed $query): mixed
public function apply(Collection $collection, mixed $query): QueryInterface
{
$parentId = $collection->getParentId();

if(!$parentId) {
return $query;
}

/** @var AssetQuery $query */
return $query->filterParentId($parentId);
}
}
4 changes: 1 addition & 3 deletions src/Filter/PathFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final class PathFilter implements FilterInterface
{
public function apply(Collection $collection, QueryInterface $query): mixed
public function apply(Collection $collection, QueryInterface $query): QueryInterface
{
$path = $collection->getPath();
$includeParent = $collection->getPathIncludeParent();
Expand All @@ -32,7 +31,6 @@ public function apply(Collection $collection, QueryInterface $query): mixed
return $query;
}

/** @var AssetQuery $query */
return $query->filterPath($path, $includeDescendants, $includeParent);
}
}
7 changes: 7 additions & 0 deletions src/PimcoreStudioApiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

namespace Pimcore\Bundle\StudioApiBundle;

use Pimcore\Bundle\StudioApiBundle\DependencyInjection\CompilerPass\FilterPass;
use Pimcore\Extension\Bundle\AbstractPimcoreBundle;
use Pimcore\Extension\Bundle\Installer\InstallerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class PimcoreStudioApiBundle extends AbstractPimcoreBundle
{
Expand Down Expand Up @@ -53,4 +55,9 @@ public function getInstaller(): ?InstallerInterface
/** @var InstallerInterface|null */
return $this->container->get(Installer::class);
}

public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new FilterPass());
}
}
4 changes: 3 additions & 1 deletion src/Service/Filter/Loader/TaggedIteratorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
*/
final class TaggedIteratorAdapter implements FilterLoaderInterface
{
public const FILTER_TAG = 'pimcore.studio_api.collection.filter';

public function __construct(
#[TaggedIterator('pimcore.studio_api.collection.filter')]
#[TaggedIterator(self::FILTER_TAG)]
private readonly iterable $taggedServices
) {
}
Expand Down
41 changes: 41 additions & 0 deletions src/Util/Traits/MustImplementInterfaceTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Util\Traits;

use Pimcore\Bundle\StudioApiBundle\Exception\MustImplementInterfaceException;

/**
* @internal
*/
trait MustImplementInterfaceTrait
{
/**
* @throws MustImplementInterfaceException
*/
private function checkInterface(string $class, string $interface): void
{
$classInterfaces = class_implements($class, false);
if (
$classInterfaces === false ||
!in_array($interface, $classInterfaces, true)
) {
throw new MustImplementInterfaceException(
sprintf('%s must implement %s', $class, $interface)
);
}
}
}

0 comments on commit 9ed2136

Please sign in to comment.