diff --git a/README.md b/README.md index 9a8d02f..83275fe 100755 --- a/README.md +++ b/README.md @@ -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: diff --git a/skeleton/AuditTypeInterface.tpl.php b/skeleton/AuditTypeInterface.tpl.php index af22cb1..bb1d901 100644 --- a/skeleton/AuditTypeInterface.tpl.php +++ b/skeleton/AuditTypeInterface.tpl.php @@ -2,11 +2,11 @@ namespace ; -interface +interface { $value){ - echo ' public const '.$key." = '".$value."';\n"; +foreach ($constants as $key => $value) { + echo ' public const ' . $key . " = '" . $value . "';\n"; } ?> } diff --git a/src/ApiResource/AuditResource.php b/src/Api/Resource/AuditResource.php similarity index 98% rename from src/ApiResource/AuditResource.php rename to src/Api/Resource/AuditResource.php index 3004682..15df182 100644 --- a/src/ApiResource/AuditResource.php +++ b/src/Api/Resource/AuditResource.php @@ -1,6 +1,6 @@ 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 { @@ -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 @@ -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(); } @@ -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']]); + } + } } diff --git a/src/DataProvider/AuditDataProvider.php b/src/DataProvider/AuditDataProvider.php index 18cf368..f63463d 100644 --- a/src/DataProvider/AuditDataProvider.php +++ b/src/DataProvider/AuditDataProvider.php @@ -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; diff --git a/src/Entity/Audit.php b/src/Entity/Audit.php index 84e96b8..05db009 100755 --- a/src/Entity/Audit.php +++ b/src/Entity/Audit.php @@ -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; diff --git a/src/Service/AuditService.php b/src/Service/AuditService.php index 62c54d5..c0da30a 100755 --- a/src/Service/AuditService.php +++ b/src/Service/AuditService.php @@ -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; }