Skip to content

Commit

Permalink
Merge pull request #30 from whitedigital-eu/feature/allow-overriding-…
Browse files Browse the repository at this point in the history
…of-api-resource-class

Changes regarding api resource auto registering with api platform
  • Loading branch information
k0d3r1s authored Aug 23, 2023
2 parents 8e8aefb + 4e19202 commit ab51488
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,45 @@ return static function (AuditConfig $config): void {
};
```
---
**Overriding default api resource (and therefore api endpoints)**
By default, Audit resource is based on `AuditResource`
If you wish not to use this resource and not expose the api endpoints it provides, just set a custom api resource path
with a configuration value. If you set it as `null`, api platform will not register api resource located within this
package.
```yaml
audit:
custom_api_resource_path: '%kernel.project_dir%/src/MyCustomPath'
# custom_api_resource_path: null
```
```php
use Symfony\Config\AuditConfig;
return static function (AuditConfig $config): void {
$config
->customApiResourcePath('%kernel.project_dir%/src/MyCustomPath')
// or ->customApiResourcePath(null);
};
```
After overriding default api resource, do not forget to update ClassMapperConfigurator configuration that is used for
resource <-> entity mapping in `whitedigital-eu/entity-resource-mapper-bundle`
```php
use App\ApiResource\Admin\AuditResource;
use WhiteDigital\Audit\Entity\Audit;
use WhiteDigital\EntityResourceMapper\Mapper\ClassMapper;
use WhiteDigital\EntityResourceMapper\Mapper\ClassMapperConfiguratorInterface;
final class ClassMapperConfigurator implements ClassMapperConfiguratorInterface
{
public function __invoke(ClassMapper $classMapper): void
{
$classMapper->registerMapping(AuditResource::class, Audit::class);
}
}
```
---
**Overriding default entity**
By default, Audit entity is based on `BaseEntity` that comes from `whitedigital-eu/entity-resource-mapper-bundle`.
If you wish not to use this base at all, you need to create new Entity and implement `AuditEntityInterface` into it:
Expand Down
6 changes: 3 additions & 3 deletions skeleton/AuditTypeInterface.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace <?php echo $namespace; ?>;

interface <?php echo $class_name."\n"; ?>
interface <?php echo $class_name . "\n"; ?>
{
<?php
foreach($constants as $key => $value){
echo ' public const '.$key." = '".$value."';\n";
foreach ($constants as $key => $value) {
echo ' public const ' . $key . " = '" . $value . "';\n";
}
?>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace WhiteDigital\Audit\ApiResource;
namespace WhiteDigital\Audit\Api\Resource;

use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
Expand Down
28 changes: 18 additions & 10 deletions src/AuditBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class AuditBundle extends AbstractBundle
'mapping' => true,
];

private const PATHS = [
'%kernel.project_dir%/vendor/whitedigital-eu/audit-service/src/ApiResource',
];
private const API_RESOURCE_PATH = '%kernel.project_dir%/vendor/whitedigital-eu/audit-service/src/Api/Resource';

public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
Expand Down Expand Up @@ -72,18 +70,18 @@ public function loadExtension(array $config, ContainerConfigurator $container, C

public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
$audit = array_merge_recursive(...($builder->getExtensionConfig('audit') ?? []));
$extensionConfig = array_merge_recursive(...($builder->getExtensionConfig('audit') ?? []));

if (true === ($audit['set_doctrine_mappings'] ?? true)) {
$this->validate($audit);
if (true === ($extensionConfig['set_doctrine_mappings'] ?? true)) {
$this->validate($extensionConfig);

$mappings = $this->getOrmMappings($builder, $audit['default_entity_manager'] ?? 'default');
$mappings = $this->getOrmMappings($builder, $extensionConfig['default_entity_manager'] ?? 'default');

$this->addDoctrineConfig($container, $audit['audit_entity_manager'] ?? 'audit', 'Audit', self::MAPPINGS, $mappings);
$this->addDoctrineConfig($container, $audit['default_entity_manager'] ?? 'default', 'Audit', self::MAPPINGS);
$this->addDoctrineConfig($container, $extensionConfig['audit_entity_manager'] ?? 'audit', 'Audit', self::MAPPINGS, $mappings);
$this->addDoctrineConfig($container, $extensionConfig['default_entity_manager'] ?? 'default', 'Audit', self::MAPPINGS);
}

$this->addApiPlatformPaths($container, self::PATHS);
$this->configureApiPlatformExtension($container, $extensionConfig);
}

public function configure(DefinitionConfigurator $definition): void
Expand Down Expand Up @@ -114,6 +112,7 @@ public function configure(DefinitionConfigurator $definition): void
->end()
->scalarNode('audit_type_interface_namespace')->defaultValue('App\\Audit')->end()
->scalarNode('audit_type_interface_class_name')->defaultValue('AuditType')->end()
->scalarNode('custom_api_resource_path')->defaultNull()->end()
->end();
}

Expand All @@ -140,4 +139,13 @@ private function validate(array $config): void
throw new InvalidConfigurationException('WhiteDigital\Audit: "audit_entity_manager" and "default_entity_manager" names must be different');
}
}

private function configureApiPlatformExtension(ContainerConfigurator $container, array $extensionConfig): void
{
if (!array_key_exists('custom_api_resource_path', $extensionConfig)) {
$this->addApiPlatformPaths($container, [self::API_RESOURCE_PATH]);
} elseif (!empty($extensionConfig['custom_api_resource_path'])) {
$this->addApiPlatformPaths($container, [$extensionConfig['custom_api_resource_path']]);
}
}
}
2 changes: 1 addition & 1 deletion src/DataProvider/AuditDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ApiPlatform\Exception\ResourceClassNotFoundException;
use ReflectionException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use WhiteDigital\Audit\ApiResource\AuditResource;
use WhiteDigital\Audit\Api\Resource\AuditResource;
use WhiteDigital\EntityResourceMapper\DataProvider\AbstractDataProvider;
use WhiteDigital\EntityResourceMapper\Entity\BaseEntity;

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace WhiteDigital\Audit\Entity;

use Doctrine\ORM\Mapping as ORM;
use WhiteDigital\Audit\ApiResource\AuditResource;
use WhiteDigital\Audit\Api\Resource\AuditResource;
use WhiteDigital\Audit\Contracts\AuditEntityInterface;
use WhiteDigital\EntityResourceMapper\Attribute\Mapping;
use WhiteDigital\EntityResourceMapper\Entity\BaseEntity;
Expand Down
4 changes: 2 additions & 2 deletions src/Service/AuditService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function __construct(
public function auditException(Throwable $exception, ?string $url = null, string $class = Audit::class): void
{
if (
method_exists($exception, 'getStatusCode') &&
in_array($exception->getStatusCode(), $this->excludedCodes, true)
method_exists($exception, 'getStatusCode')
&& in_array($exception->getStatusCode(), $this->excludedCodes, true)
) {
return;
}
Expand Down

0 comments on commit ab51488

Please sign in to comment.