Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-5630: Added possibility to extend URL schema resolving #242

Merged
merged 37 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
595ae45
IBX-5630
kisztof Jun 19, 2023
093436e
more readabillity
kisztof Jun 19, 2023
635cbac
Fixed extension point
kisztof Jun 20, 2023
368e7c3
Fixed priority
kisztof Jun 26, 2023
0228b20
Refactored NameSchemaSubscriber
kisztof Jun 26, 2023
e7c1a20
Fixed url schema generation
kisztof Jun 27, 2023
7a58566
Ref
kisztof Jun 28, 2023
25602ff
Defined NameSchemaService as DIC Service
alongosz Jun 28, 2023
8de80e4
Fixed some tests
kisztof Jun 29, 2023
0280c6d
Fixed content name generation
kisztof Jun 30, 2023
9723018
Test fix
kisztof Jun 30, 2023
af1ccf8
Fix tests
kisztof Jun 30, 2023
9877d12
Fixed minor issues in NameSchemaSubscriber
alongosz Jun 30, 2023
097cc11
Restored original Helper\NameSchemaService::resolveUrlAliasSchema for BC
alongosz Jun 30, 2023
04db1ed
[CS] Fixed minor issue in NameSchemaService::resolve method
alongosz Jun 30, 2023
f8e9b24
Refactored NameSchemaService to be more lightweight
alongosz Jul 4, 2023
08591ee
[Tests] Defined URLAliasService and required ibexa.kernel.root_dir param
alongosz Jul 3, 2023
2b7c600
[Tests] Created abstract RepositoryTestCase for integration tests
alongosz Jul 3, 2023
f8d46be
[Tests] Unified SF EventDispatcher definition for Legacy integration
alongosz Jul 3, 2023
cbb6453
[Tests] Added coverage for URLAliasService::lookup using IbexaKernelT…
alongosz Jul 3, 2023
ff73ed4
[Tests] Added coverage for SchemaIdentifierExtractor
alongosz Jul 3, 2023
2524a58
[Tests] Refactored tests to avoid comparing proxies
alongosz Jul 4, 2023
d8321a9
[Tests] Revamped NameSchemaService unit test
alongosz Jul 5, 2023
8009aca
fixup! [Tests] Revamped NameSchemaService unit test
alongosz Jul 5, 2023
2992a36
Fixed field name generator
kisztof Jul 5, 2023
e066bd3
Fixed codesmells
kisztof Jul 6, 2023
c31d9f9
Fixed SchemaIdentifierExtractor
kisztof Jul 6, 2023
651c580
Fixed the issue when fielddefinition not found
kisztof Jul 7, 2023
1503ec9
Applied CR
kisztof Jul 10, 2023
8c57681
Added missing return type
kisztof Jul 11, 2023
e7e5f73
Improved NameSchemaService test
kisztof Jul 11, 2023
9da8532
Fixes Applied
kisztof Jul 11, 2023
595532b
Made NameSchemaServiceTest class final
kisztof Jul 11, 2023
3fb0761
[Tests] Refactored NameSchemaServiceTest to avoid code duplication
alongosz Jul 11, 2023
4375c0d
Fixed CR issues
kisztof Jul 12, 2023
ac18a71
Fixed an issue with grouping
kisztof Jul 14, 2023
96020b1
Fixed issue with grouping
kisztof Jul 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/bundle/Core/ApiLoader/RepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Ibexa\Contracts\Core\Persistence\Filter\Location\Handler as LocationFilteringHandler;
use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler;
use Ibexa\Contracts\Core\Repository\LanguageResolver;
use Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface;
use Ibexa\Contracts\Core\Repository\PasswordHashService;
use Ibexa\Contracts\Core\Repository\PermissionService;
use Ibexa\Contracts\Core\Repository\Repository;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(
$this->repositoryClass = $repositoryClass;
$this->policyMap = $policyMap;
$this->languageResolver = $languageResolver;
$this->logger = null !== $logger ? $logger : new NullLogger();
$this->logger = $logger ?? new NullLogger();
}

/**
Expand Down Expand Up @@ -91,9 +92,10 @@ public function buildRepository(
ContentFilteringHandler $contentFilteringHandler,
LocationFilteringHandler $locationFilteringHandler,
PasswordValidatorInterface $passwordValidator,
ConfigResolverInterface $configResolver
ConfigResolverInterface $configResolver,
NameSchemaServiceInterface $nameSchemaService
): Repository {
$config = $this->container->get(\Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider::class)->getRepositoryConfig();
$config = $this->container->get(RepositoryConfigurationProvider::class)->getRepositoryConfig();

return new $this->repositoryClass(
$persistenceHandler,
Expand All @@ -116,6 +118,7 @@ public function buildRepository(
$locationFilteringHandler,
$passwordValidator,
$configResolver,
$nameSchemaService,
[
'role' => [
'policyMap' => $this->policyMap,
Expand Down
7 changes: 0 additions & 7 deletions src/bundle/Core/Resources/config/events.yml

This file was deleted.

1 change: 0 additions & 1 deletion src/bundle/Core/Resources/config/papi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ parameters:
services:
# API
Ibexa\Bundle\Core\ApiLoader\RepositoryFactory:
class: Ibexa\Bundle\Core\ApiLoader\RepositoryFactory
arguments:
- '@ibexa.config.resolver'
- Ibexa\Core\Repository\Repository
Expand Down
53 changes: 53 additions & 0 deletions src/contracts/Event/ResolveUrlAliasSchemaEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Event;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Symfony\Contracts\EventDispatcher\Event;

final class ResolveUrlAliasSchemaEvent extends Event
{
/** @var array<string, array> */
private array $schemaIdentifiers;

private Content $content;

/**
* @var array<string, array<string>>
*/
private array $names = [];
konradoboza marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(
array $schemaIdentifiers,
Content $content
) {
$this->schemaIdentifiers = $schemaIdentifiers;
$this->content = $content;
}

public function getSchemaIdentifiers(): array
{
return $this->schemaIdentifiers;
}

public function getContent(): Content
{
return $this->content;
}

public function getTokenValues(): array
{
return $this->names;
}

public function setTokenValues(array $names): void
{
$this->names = $names;
}
}
50 changes: 50 additions & 0 deletions src/contracts/Repository/NameSchema/NameSchemaServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\NameSchema;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;

/**
* @internal Meant to be used by 1st party only
*
* @experimental
*/
interface NameSchemaServiceInterface
{
/**
* @return array<string, string> key value map of names for a language code
*/
public function resolveUrlAliasSchema(Content $content, ContentType $contentType = null): array;

/**
* @param array<string, array<string, string>> $fieldMap
* @param array<string> $languageCodes
*
* @return array
*/
public function resolveNameSchema(
Content $content,
array $fieldMap = [],
array $languageCodes = [],
ContentType $contentType = null
): array;

/**
* Returns the real name for a content name pattern.
*
* @param array<string, array<string, string>> $fieldMap
* @param array<string> $languageCodes
*
* @return array<string>
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function resolve(string $nameSchema, ContentType $contentType, array $fieldMap, array $languageCodes): array;
kisztof marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\NameSchema;

interface SchemaIdentifierExtractorInterface
{
/**
* @return array<string, array<string, string>>
*/
public function extract(string $schemaString): array;
}
6 changes: 6 additions & 0 deletions src/contracts/Test/IbexaKernelTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Ibexa\Contracts\Core\Repository\RoleService;
use Ibexa\Contracts\Core\Repository\SearchService;
use Ibexa\Contracts\Core\Repository\SectionService;
use Ibexa\Contracts\Core\Repository\URLAliasService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Test\Persistence\Fixture\FixtureImporter;
use Ibexa\Core\Repository\Values\User\UserReference;
Expand Down Expand Up @@ -163,6 +164,11 @@ protected static function getSectionService(): SectionService
return self::getServiceByClassName(SectionService::class);
}

protected static function getUrlAliasService(): URLAliasService
{
return self::getServiceByClassName(URLAliasService::class);
}

protected static function setAnonymousUser(): void
{
$anonymousUserId = 10;
Expand Down
2 changes: 2 additions & 0 deletions src/contracts/Test/IbexaTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class IbexaTestKernel extends Kernel implements IbexaTestKernelInterface
Repository\SectionService::class,
Repository\UserService::class,
Repository\TokenService::class,
Repository\URLAliasService::class,
];

/**
Expand Down Expand Up @@ -150,6 +151,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(static function (ContainerBuilder $container): void {
$container->setParameter('ibexa.core.test.resource_dir', self::getResourcesPath());
$container->setParameter('ibexa.kernel.root_dir', dirname(__DIR__, 3));
});

$this->loadConfiguration($loader);
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Base/Container/ApiLoader/RepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Ibexa\Contracts\Core\Persistence\Filter\Location\Handler as LocationFilteringHandler;
use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler;
use Ibexa\Contracts\Core\Repository\LanguageResolver;
use Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface;
use Ibexa\Contracts\Core\Repository\PasswordHashService;
use Ibexa\Contracts\Core\Repository\PermissionService;
use Ibexa\Contracts\Core\Repository\Repository;
Expand Down Expand Up @@ -83,6 +84,7 @@ public function buildRepository(
LocationFilteringHandler $locationFilteringHandler,
PasswordValidatorInterface $passwordValidator,
ConfigResolverInterface $configResolver,
NameSchemaServiceInterface $nameSchemaService,
array $languages
): Repository {
return new $this->repositoryClass(
Expand All @@ -106,6 +108,7 @@ public function buildRepository(
$locationFilteringHandler,
$passwordValidator,
$configResolver,
$nameSchemaService,
[
'role' => [
'policyMap' => $this->policyMap,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Ibexa\Contracts\Core\Persistence\Handler;
use Ibexa\Contracts\Core\Repository\ContentService as ContentServiceInterface;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as APINotFoundException;
use Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface;
use Ibexa\Contracts\Core\Repository\PermissionService;
use Ibexa\Contracts\Core\Repository\Repository as RepositoryInterface;
use Ibexa\Contracts\Core\Repository\Validator\ContentValidator;
Expand Down Expand Up @@ -87,8 +88,7 @@ class ContentService implements ContentServiceInterface
/** @var \Ibexa\Core\Repository\Helper\RelationProcessor */
protected $relationProcessor;

/** @var \Ibexa\Core\Repository\Helper\NameSchemaService */
protected $nameSchemaService;
protected NameSchemaServiceInterface $nameSchemaService;

/** @var \Ibexa\Core\FieldType\FieldTypeRegistry */
protected $fieldTypeRegistry;
Expand All @@ -110,7 +110,7 @@ public function __construct(
Handler $handler,
ContentDomainMapper $contentDomainMapper,
Helper\RelationProcessor $relationProcessor,
Helper\NameSchemaService $nameSchemaService,
NameSchemaServiceInterface $nameSchemaService,
kisztof marked this conversation as resolved.
Show resolved Hide resolved
FieldTypeRegistry $fieldTypeRegistry,
PermissionService $permissionService,
ContentMapper $contentMapper,
Expand Down
75 changes: 75 additions & 0 deletions src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\Repository\EventSubscriber;

use Ibexa\Contracts\Core\Event\ResolveUrlAliasSchemaEvent;
use Ibexa\Core\FieldType\FieldTypeRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @internal
*/
final class NameSchemaSubscriber implements EventSubscriberInterface
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
private FieldTypeRegistry $fieldTypeRegistry;

public function __construct(FieldTypeRegistry $fieldTypeRegistry)
{
$this->fieldTypeRegistry = $fieldTypeRegistry;
}

public static function getSubscribedEvents(): array
{
return [
ResolveUrlAliasSchemaEvent::class => [
['onResolveUrlAliasSchema', -100],
],
];
}

/**
* Resolves the URL alias schema by setting token values for specified field identifiers and languages.
*
* @param \Ibexa\Contracts\Core\Event\ResolveUrlAliasSchemaEvent $event
*/
public function onResolveUrlAliasSchema(ResolveUrlAliasSchemaEvent $event): void
{
if (!array_key_exists('field', $event->getSchemaIdentifiers())) {
return;
}

$content = $event->getContent();
$identifiers = $event->getSchemaIdentifiers()['field'];
$languages = $event->getContent()->getVersionInfo()->getLanguages();
$tokenValues = $event->getTokenValues();

$contentType = $content->getContentType();
foreach ($languages as $language) {
$languageCode = $language->getLanguageCode();
foreach ($identifiers as $identifier) {
$fieldDefinition = $contentType->getFieldDefinition($identifier);
if (null === $fieldDefinition) {
continue;
}
$persistenceFieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldTypeIdentifier);

$fieldValue = $content->getFieldValue($identifier, $languageCode);
$fieldValue = $persistenceFieldType->getName(
$fieldValue,
$fieldDefinition,
$languageCode
);

$tokenValues[$languageCode][$identifier] = $fieldValue;
}
}

$event->setTokenValues($tokenValues);
}
}
Loading
Loading