Skip to content

Commit

Permalink
feat(config:command:get-files): implement returnMaxLastFiles config…
Browse files Browse the repository at this point in the history
…uration (#27)
  • Loading branch information
Kocal authored Mar 9, 2023
1 parent 2536792 commit 4fa9dfc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions src/Patcher/GetFilesCommandPatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace CKSource\Bundle\CKFinderBundle\Patcher;

class GetFilesCommandPatcher implements PatcherInterface
{
use PatcherTrait;

public function patch(string $connectorPath): void
{
$this->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,
);
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
5 changes: 5 additions & 0 deletions tests/Fixtures/config/ckfinder_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,10 @@
$config['sessionWriteClose'] = true;
$config['csrfProtection'] = true;
$config['forceThrowExceptions'] = false;
$config['commands'] = [
'GetFiles' => [
'returnMaxLastFiles' => null
],
];

return $config;

0 comments on commit 4fa9dfc

Please sign in to comment.