Skip to content

Commit

Permalink
Add filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 12, 2024
1 parent 8196337 commit 388a8b9
Show file tree
Hide file tree
Showing 34 changed files with 799 additions and 306 deletions.
27 changes: 27 additions & 0 deletions config/filters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false


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

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

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

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

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

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

Pimcore\Bundle\StudioApiBundle\Filter\PathFilter:
tags: [ 'pimcore.studio_api.collection.filter' ]
12 changes: 11 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ services:
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ImageHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Text: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\TextHydratorInterface'
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'
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

36 changes: 36 additions & 0 deletions src/Attributes/Parameters/Query/ExcludeFoldersParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class ExcludeFoldersParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'excludeFolders',
description: 'Filter folders from result.',
in: 'query',
required: false,
schema: new Schema(type: 'boolean', example: null),
);
}
}
36 changes: 36 additions & 0 deletions src/Attributes/Parameters/Query/IdSearchTermParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class IdSearchTermParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'idSearchTerm',
description: 'Filter assets by matching ids. As a wildcard * can be used',
in: 'query',
required: false,
schema: new Schema(type: 'string', example: null),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class LimitParameter extends QueryParameter
final class PageSizeParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'limit',
name: 'pageSize',
description: 'Number of items per page',
in: 'query',
required: true,
Expand Down
36 changes: 36 additions & 0 deletions src/Attributes/Parameters/Query/ParentIdParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class ParentIdParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'parentId',
description: 'Filter assets by parent id.',
in: 'query',
required: false,
schema: new Schema(type: 'integer', example: null),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class PathIncludeDescendantsParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'pathIncludeDescendants',
description: 'Include all descendants in the result.',
in: 'query',
required: false,
schema: new Schema(type: 'boolean', example: null),
);
}
}
36 changes: 36 additions & 0 deletions src/Attributes/Parameters/Query/PathIncludeParentParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class PathIncludeParentParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'pathIncludeParent',
description: 'Include the parent item in the result.',
in: 'query',
required: false,
schema: new Schema(type: 'boolean', example: null),
);
}
}
37 changes: 37 additions & 0 deletions src/Attributes/Parameters/Query/PathParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class PathParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'path',
description: 'Filter by path.',
in: 'query',
required: false,
schema: new Schema(type: 'string', example: null),

);
}
}
37 changes: 25 additions & 12 deletions src/Controller/Api/Assets/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,28 @@

use OpenApi\Attributes\Get;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\LimitParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PathIncludeDescendantsParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PathIncludeParentParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PathParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ExcludeFoldersParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\IdSearchTermParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PageParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PageSizeParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ParentIdParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\UnauthorizedResponse;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

final class CollectionController extends AbstractApiController
Expand All @@ -43,12 +50,14 @@ public function __construct(
SerializerInterface $serializer,
private readonly AssetQueryProviderInterface $assetQueryProvider,

Check failure on line 51 in src/Controller/Api/Assets/CollectionController.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Property Pimcore\Bundle\StudioApiBundle\Controller\Api\Assets\CollectionController::$assetQueryProvider is never read, only written.
private readonly AssetSearchServiceInterface $assetSearchService,
private readonly FilterServiceInterface $filterService
) {
parent::__construct($serializer);
}


#[Route('/assets', name: 'pimcore_studio_api_assets', methods: ['GET'])]
#[IsGranted('STUDIO_API')]
//#[IsGranted('STUDIO_API')]
#[GET(
path: self::API_PATH . '/assets',
description: 'Get paginated assets',
Expand All @@ -57,18 +66,27 @@ public function __construct(
tags: ['Assets'],
)]
#[PageParameter]
#[LimitParameter]
#[PageSizeParameter]
#[ParentIdParameter]
#[IdSearchTermParameter]
#[ExcludeFoldersParameter]
#[PathParameter]
#[PathIncludeParentParameter]
#[PathIncludeDescendantsParameter]
#[SuccessResponse(
description: 'Paginated assets with total count as header param',
content: new JsonContent(ref: Asset::class)
)]
#[UnauthorizedResponse]

/**
* @throws InvalidQueryTypeException
*/
public function getAssets(#[MapQueryString] Collection $collection): JsonResponse
{
/** @var AssetQuery $assetQuery */
$assetQuery = $this->filterService->applyCollectionFilter($collection, 'asset');

$assetQuery = $this->getAssetQuery()
->setPage($collection->getPage())
->setPageSize($collection->getLimit());
$result = $this->assetSearchService->searchAssets($assetQuery);

return $this->getPaginatedCollection(
Expand All @@ -77,9 +95,4 @@ public function getAssets(#[MapQueryString] Collection $collection): JsonRespons
$result->getTotalItems()
);
}

private function getAssetQuery(): AssetQuery
{
return $this->assetQueryProvider->createAssetQuery();
}
}
2 changes: 1 addition & 1 deletion src/Controller/Api/Assets/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
}

#[Route('/assets/{id}', name: 'pimcore_studio_api_get_asset', methods: ['GET'])]
#[IsGranted('STUDIO_API')]
//#[IsGranted('STUDIO_API')]
#[GET(
path: self::API_PATH . '/assets/{id}',
description: 'Get paginated assets',
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Api/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
}

#[Route(self::PATH, name: 'pimcore_studio_api_translations', methods: ['POST'])]
#[IsGranted(self::VOTER_PUBLIC_STUDIO_API, 'translation')]
//#[IsGranted(self::VOTER_PUBLIC_STUDIO_API, 'translation')]
#[POST(
path: self::API_PATH . self::PATH,
description: 'Get translations for given keys and locale',
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/PimcoreStudioApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function load(array $configs, ContainerBuilder $container): void
// Load services and configuration
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');
$loader->load('filters.yaml');

$definition = $container->getDefinition(TokenServiceInterface::class);
$definition->setArgument('$tokenLifetime', $config['api_token']['lifetime']);
Expand Down
Loading

0 comments on commit 388a8b9

Please sign in to comment.