Skip to content

Commit

Permalink
Merge pull request #8 from Dukecity/feature/merge-upstream
Browse files Browse the repository at this point in the history
Feature/merge upstream
  • Loading branch information
Chris53897 authored Nov 7, 2023
2 parents 6b40a7f + c93c209 commit 336baa9
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 58 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
- name: Validate composer.json
run: composer validate --strict --no-check-lock
Expand All @@ -22,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '8.1', '8.2' ]
php: [ '8.1', '8.2', '8.3' ]
name_suffix: ['']
stability: ['stable']
composer_flags: ['']
Expand All @@ -31,13 +31,13 @@ jobs:
name_suffix: ' (lowest deps)'
stability: 'stable'
composer_flags: '--prefer-lowest'
- php: '8.2'
- php: '8.3'
name_suffix: ' (dev deps)'
stability: 'dev'
composer_flags: ''

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
],
"require": {
"php": "^8.1",
"symfony/cache": "^5.4 || ^6.2",
"symfony/config": "^5.4 || ^6.2",
"symfony/dependency-injection": "^5.4 || ^6.2",
"symfony/event-dispatcher": "^5.4 || ^6.2",
"symfony/http-kernel": "^5.4 || ^6.2",
"gedmo/doctrine-extensions": "^3.11.1"
"symfony/cache": "^5.4 || ^6.3 || ^7.0",
"symfony/config": "^5.4 || ^6.3 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.3 || ^7.0",
"symfony/event-dispatcher": "^5.4 || ^6.3 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.3 || ^7.0",
"gedmo/doctrine-extensions": "^3.13"
},
"require-dev": {
"symfony/mime": "^5.4 || ^6.2",
"phpunit/phpunit": "^10.2",
"symfony/security-core": "^5.4 || ^6.2"
"symfony/mime": "^5.4 || ^6.3 || ^7.0",
"phpunit/phpunit": "^10.4",
"symfony/security-core": "^5.4 || ^6.3 || ^7.0"
},
"suggest": {
"doctrine/doctrine-bundle": "to use the ORM extensions",
Expand Down
5 changes: 2 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('translation_fallback')->defaultFalse()->end()
->booleanNode('persist_default_translation')->defaultFalse()->end()
->booleanNode('skip_translation_on_load')->defaultFalse()->end()
->scalarNode('metadata_cache_pool')
->defaultValue('stof_doctrine_extensions.cache.pool.array')
->end()
->scalarNode('metadata_cache_pool')->defaultNull()->end()
->end()
;

Expand All @@ -62,6 +60,7 @@ private function getVendorNode(string $name): ArrayNodeDefinition
->scalarNode('sluggable')->defaultFalse()->end()
->scalarNode('tree')->defaultFalse()->end()
->scalarNode('loggable')->defaultFalse()->end()
->scalarNode('ip_traceable')->defaultFalse()->end()
->scalarNode('sortable')->defaultFalse()->end()
->scalarNode('softdeleteable')->defaultFalse()->end()
->scalarNode('uploadable')->defaultFalse()->end()
Expand Down
106 changes: 87 additions & 19 deletions src/DependencyInjection/StofDoctrineExtensionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,82 @@
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class StofDoctrineExtensionsExtension extends Extension
{
private const LISTENER_EVENTS = [
'blameable' => [
'prePersist',
'onFlush',
'loadClassMetadata',
],
'ip_traceable' => [
'prePersist',
'onFlush',
'loadClassMetadata',
],
'loggable' => [
'loadClassMetadata',
'onFlush',
'postPersist',
],
'reference_integrity' => [
'loadClassMetadata',
'preRemove',
],
'sluggable' => [
'prePersist',
'onFlush',
'loadClassMetadata',
],
'softdeleteable' => [
'loadClassMetadata',
'onFlush',
],
'sortable' => [
'onFlush',
'loadClassMetadata',
'prePersist',
'postPersist',
'preUpdate',
'postRemove',
'postFlush',
],
'timestampable' => [
'prePersist',
'onFlush',
'loadClassMetadata',
],
'translatable' => [
'postLoad',
'postPersist',
'preFlush',
'onFlush',
'loadClassMetadata',
],
'tree' => [
'prePersist',
'preRemove',
'preUpdate',
'onFlush',
'loadClassMetadata',
'postPersist',
'postUpdate',
'postRemove',
],
'uploadable' => [
'loadClassMetadata',
'preFlush',
'onFlush',
'postFlush',
],
];

private array $entityManagers = [];
private array $documentManagers = [];

Expand All @@ -29,8 +98,8 @@ public function load(array $configs, ContainerBuilder $container): void

$loaded = [];

$this->entityManagers = $this->processObjectManagerConfigurations($config['orm'], $container, $loader, $loaded, 'doctrine.event_subscriber');
$this->documentManagers = $this->processObjectManagerConfigurations($config['mongodb'], $container, $loader, $loaded, 'doctrine_mongodb.odm.event_subscriber');
$this->entityManagers = $this->processObjectManagerConfigurations($config['orm'], $container, $loader, $loaded, 'doctrine.event_listener');
$this->documentManagers = $this->processObjectManagerConfigurations($config['mongodb'], $container, $loader, $loaded, 'doctrine_mongodb.odm.event_listener');

$container->setParameter('stof_doctrine_extensions.default_locale', $config['default_locale']);
$container->setParameter('stof_doctrine_extensions.translation_fallback', $config['translation_fallback']);
Expand Down Expand Up @@ -67,11 +136,15 @@ public function load(array $configs, ContainerBuilder $container): void
}
}

if (isset($config['metadata_cache_pool'])) {
$container->setAlias('stof_doctrine_extensions.metadata_cache', new Alias($config['metadata_cache_pool'], false));
} else {
$container->register('stof_doctrine_extensions.metadata_cache', ArrayAdapter::class)->setPublic(false);
}

foreach ($config['class'] as $listener => $class) {
$container->setParameter(sprintf('stof_doctrine_extensions.listener.%s.class', $listener), $class);
}

$this->configureCache($container, $config);
}

/**
Expand All @@ -93,16 +166,16 @@ public function configValidate(ContainerBuilder $container): void
}

/**
* @param array $configs
* @param array $configs
* @param ContainerBuilder $container
* @param LoaderInterface $loader
* @param array $loaded
* @param string $doctrineSubscriberTag
* @param LoaderInterface $loader
* @param array $loaded
* @param string $doctrineListenerTag
*
* @return array
* @throws \Exception
*/
private function processObjectManagerConfigurations(array $configs, ContainerBuilder $container, LoaderInterface $loader, array &$loaded, string $doctrineSubscriberTag): array
private function processObjectManagerConfigurations(array $configs, ContainerBuilder $container, LoaderInterface $loader, array &$loaded, string $doctrineListenerTag)
{
$usedManagers = [];

Expand Down Expand Up @@ -130,21 +203,16 @@ private function processObjectManagerConfigurations(array $configs, ContainerBui
}

$definition = $container->getDefinition(sprintf('stof_doctrine_extensions.listener.%s', $ext));
$definition->addTag($doctrineSubscriberTag, $attributes);

foreach (self::LISTENER_EVENTS[$ext] as $event) {
$attributes['event'] = $event;
$definition->addTag($doctrineListenerTag, $attributes);
}

$usedManagers[$name] = true;
}
}

return array_keys($usedManagers);
}


private function configureCache(ContainerBuilder $container, array $config): void
{
$container
->register('stof_doctrine_extensions.cache.pool.array', ArrayAdapter::class);

$container->setAlias('stof_doctrine_extensions.cache.pool.default', $config['metadata_cache_pool']);
}
}
38 changes: 38 additions & 0 deletions src/EventListener/IpTraceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Stof\DoctrineExtensionsBundle\EventListener;

use Gedmo\IpTraceable\IpTraceableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* @internal
*/
final class IpTraceListener implements EventSubscriberInterface
{
public function __construct(private readonly IpTraceableListener $ipTraceableListener)
{}

public function onKernelRequest(RequestEvent $event): void
{
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
return;
}

$ip = $event->getRequest()->getClientIp();

if (null !== $ip) {
$this->ipTraceableListener->setIpValue($ip);
}
}

public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 500],
];
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/blameable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.blameable', '%stof_doctrine_extensions.listener.blameable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])

->set('stof_doctrine_extensions.event_listener.blame', '%stof_doctrine_extensions.event_listener.blame.class%')
->tag('kernel.event_subscriber')
Expand Down
23 changes: 23 additions & 0 deletions src/Resources/config/ip_traceable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Gedmo\IpTraceable\IpTraceableListener;
use Stof\DoctrineExtensionsBundle\EventListener\IpTraceListener;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.ip_traceable', IpTraceableListener::class)
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])

->set('stof_doctrine_extensions.event_listener.ip_trace', IpTraceListener::class)
->tag('kernel.event_subscriber')
->args([
new ReferenceConfigurator('stof_doctrine_extensions.listener.ip_traceable'),
])
;
};
3 changes: 2 additions & 1 deletion src/Resources/config/loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.loggable', '%stof_doctrine_extensions.listener.loggable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])

->set('stof_doctrine_extensions.event_listener.logger', '%stof_doctrine_extensions.event_listener.logger.class%')
->tag('kernel.event_subscriber')
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/reference_integrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

$services
->set('stof_doctrine_extensions.listener.reference_integrity', '%stof_doctrine_extensions.listener.reference_integrity.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
;
};
3 changes: 2 additions & 1 deletion src/Resources/config/sluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.sluggable', '%stof_doctrine_extensions.listener.sluggable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
;
};
3 changes: 2 additions & 1 deletion src/Resources/config/softdeleteable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.softdeleteable', '%stof_doctrine_extensions.listener.softdeleteable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')]);
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()]);
};
3 changes: 2 additions & 1 deletion src/Resources/config/sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.sortable', '%stof_doctrine_extensions.listener.sortable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
;
};
3 changes: 2 additions & 1 deletion src/Resources/config/timestampable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.timestampable', '%stof_doctrine_extensions.listener.timestampable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
;
};
3 changes: 2 additions & 1 deletion src/Resources/config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.translatable', '%stof_doctrine_extensions.listener.translatable.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
->call('setDefaultLocale', ['%stof_doctrine_extensions.default_locale%'])
->call('setTranslatableLocale', ['%stof_doctrine_extensions.default_locale%'])
->call('setTranslationFallback', ['%stof_doctrine_extensions.translation_fallback%'])
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

$containerConfigurator->services()
->set('stof_doctrine_extensions.listener.tree', '%stof_doctrine_extensions.listener.tree.class%')
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
;
};
4 changes: 2 additions & 2 deletions src/Resources/config/uploadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
->args([
new ReferenceConfigurator('stof_doctrine_extensions.uploadable.mime_type_guesser'),
])
->call('setAnnotationReader', [new ReferenceConfigurator('annotation_reader')])
->call('setCacheItemPool', [new ReferenceConfigurator('stof_doctrine_extensions.metadata_cache')])
->call('setAnnotationReader', [(new ReferenceConfigurator('annotation_reader'))->ignoreOnInvalid()])
->call('setDefaultFileInfoClass', ['%stof_doctrine_extensions.uploadable.default_file_info.class%'])
->factory([new ReferenceConfigurator('stof_doctrine_extensions.uploadable.configurator'), 'configure'])

->set('stof_doctrine_extensions.uploadable.mime_type_guesser', '%stof_doctrine_extensions.uploadable.mime_type_guesser.class%')

Expand Down
Loading

0 comments on commit 336baa9

Please sign in to comment.