From 4fa9dfc159db2155dd77a7018f87fcd3cb7852cd Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Thu, 9 Mar 2023 11:20:15 +0100 Subject: [PATCH] feat(config:command:get-files): implement `returnMaxLastFiles` configuration (#27) --- src/DependencyInjection/Configuration.php | 11 +++++++ src/Patcher/GetFilesCommandPatcher.php | 35 +++++++++++++++++++++++ src/Resources/config/services.yaml | 4 +++ tests/Fixtures/config/ckfinder_config.php | 5 ++++ 4 files changed, 55 insertions(+) create mode 100644 src/Patcher/GetFilesCommandPatcher.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 9c2b10c..4b1fe62 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -167,6 +167,17 @@ public function addConnectorNode(): NodeDefinition ->booleanNode('sessionWriteClose')->defaultTrue()->end() ->booleanNode('csrfProtection')->defaultTrue()->end() ->booleanNode('forceThrowExceptions')->defaultFalse()->end() + ->arrayNode('commands') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('GetFiles') + ->addDefaultsIfNotSet() + ->children() + ->integerNode('returnMaxLastFiles')->defaultNull()->end() + ->end() + ->end() + ->end() + ->end() ->end(); return $connectorNode; diff --git a/src/Patcher/GetFilesCommandPatcher.php b/src/Patcher/GetFilesCommandPatcher.php new file mode 100644 index 0000000..4266afc --- /dev/null +++ b/src/Patcher/GetFilesCommandPatcher.php @@ -0,0 +1,35 @@ +patchFile( + $connectorPath . '/Command/GetFiles.php', + " \$data = new \\stdClass();", + " \$commandConfig = \$this->app['config']->get('commands')['GetFiles'] ?? [];\n \$data = new \\stdClass();", + ); + + $this->patchFile( + $connectorPath . '/Command/GetFiles.php', + " \$files = \$workingFolder->listFiles();", + <<<'PHP' + $files = $workingFolder->listFiles(); + + if (is_int($commandConfig['returnMaxLastFiles'] ?? null)) { + usort($files, function ($a, $b) { + return $a['timestamp'] <=> $b['timestamp']; + }); + + $files = array_slice($files, 0, $commandConfig['returnMaxLastFiles']); + } +PHP, + ); + } +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 5e2c823..a8594f0 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -46,3 +46,7 @@ services: CKSource\Bundle\CKFinderBundle\Patcher\ForceThrowExceptions: tags: - { name: ckfinder.patcher, priority: 100 } + + CKSource\Bundle\CKFinderBundle\Patcher\GetFilesCommandPatcher: + tags: + - { name: ckfinder.patcher, priority: 10 } diff --git a/tests/Fixtures/config/ckfinder_config.php b/tests/Fixtures/config/ckfinder_config.php index 8d83a24..4d8bc21 100644 --- a/tests/Fixtures/config/ckfinder_config.php +++ b/tests/Fixtures/config/ckfinder_config.php @@ -105,5 +105,10 @@ $config['sessionWriteClose'] = true; $config['csrfProtection'] = true; $config['forceThrowExceptions'] = false; +$config['commands'] = [ + 'GetFiles' => [ + 'returnMaxLastFiles' => null + ], +]; return $config;