Skip to content

Commit 7e7f785

Browse files
committed
implement config option to disable mtime check
1 parent 6af0d33 commit 7e7f785

7 files changed

+23
-7
lines changed

Tests/DependencyInjection/BernhardWebstudioPlaceholderExtensionTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public function testSqipConfig()
7878
$this->assertEquals($config['service'], 'bewe_placeholder.generator.sqip');
7979
$this->assertNotEmpty($config['iterations']);
8080
$this->assertEquals($config['iterations'], 13);
81+
// test default
82+
$this->assertNotEmpty($config['ignore_mtime']);
83+
$this->assertEquals($config['ignore_mtime'], false);
8184
}
8285

8386
/**
@@ -94,6 +97,8 @@ public function testPrimitiveConfig()
9497
$this->assertEquals($config['service'], 'bewe_placeholder.generator.primitive');
9598
$this->assertNotEmpty($config['iterations']);
9699
$this->assertEquals($config['iterations'], 13);
100+
$this->assertNotEmpty($config['ignore_mtime']);
101+
$this->assertEquals($config['ignore_mtime'], true);
97102
}
98103

99104
/**
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bewe_placeholder:
22
bin: primitive
33
service: bewe_placeholder.generator.primitive
4-
iterations: 13
4+
iterations: 13
5+
ignore_mtime: true

src/Commands/PlaceholderPrepareCommand.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected function configure()
2525
// the name of the command (the part after "bin/console")
2626
->setName('bewe:placeholder:prepare')
2727
->addOption('dry')
28-
->addOption('ignore-exist')
28+
->addOption('ignore-mtime')
2929

3030
// the short description shown while running "php bin/console list"
3131
->setDescription('Creates placeholders for all the images.')
@@ -53,7 +53,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
5353
$inputPath = $image->getRealPath();
5454
$outputPath = $this->provider->getOutputPath($inputPath);
5555
// only output if not already done in another session
56-
if (!\file_exists($outputPath) || (!$input->getOption('ignore-exist') && filemtime($inputPath) > filemtime($outputPath))) {
56+
// TODO: accept ignore_mtime in parameters too
57+
if (!\file_exists($outputPath) || (!$input->getOption('ignore-mtime') && filemtime($inputPath) > filemtime($outputPath))) {
5758
if (!$dry) {
5859
// do output images
5960
try {

src/DependencyInjection/BernhardWebstudioPlaceholderExtension.php

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function load(array $defaultConfigs, ContainerBuilder $container)
3434
$providerDefinition->replaceArgument(2, $output_path);
3535
}
3636

37+
if (\array_key_exists('ignore_mtime', $config)) {
38+
$providerDefinition->replaceArgument(3, $config['ignore_mtime']);
39+
}
40+
3741
$service = $config['service'];
3842
$serviceDefinition = $container->getDefinition($config['service']);
3943

src/DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function getConfigTreeBuilder()
2222
->scalarNode('bin')->end()
2323
->scalarNode('node_bin')->end()
2424
->scalarNode('output_path')->end()
25+
->scalarNode('ignore_mtime')->end()
2526
->end();
2627

2728
return $treeBuilder;

src/Resources/config/services.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ services:
2323
- '@bewe_placeholder.generator'
2424
- ["."]
2525
- ~
26-
# - '@logger'
26+
- false
27+
2728
bewe_placeholder.generator.sqip:
2829
class: BernhardWebstudio\PlaceholderBundle\Service\SqipPlaceholderGenerator
2930
autowire: false
@@ -39,4 +40,4 @@ services:
3940
arguments:
4041
- 'primitive'
4142
- ~
42-
- 10
43+
- 10

src/Service/PlaceholderProviderService.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class PlaceholderProviderService
1313
protected $generator;
1414
protected $loadPaths;
1515
protected $outputPath;
16+
protected $ignoreMtime;
1617

1718
/**
1819
* The modes how a placeholder can be fetched
@@ -25,11 +26,13 @@ class PlaceholderProviderService
2526
public function __construct(
2627
PlaceholderGeneratorInterface $generator,
2728
array $loadPaths = array(),
28-
string $outputPath = null
29+
string $outputPath = null,
30+
$ignoreMtime = false
2931
) {
3032
$this->generator = $generator;
3133
$this->loadPaths = $loadPaths;
3234
$this->outputPath = $outputPath;
35+
$this->ignoreMtime = $ignoreMtime;
3336
}
3437

3538
/**
@@ -49,7 +52,7 @@ public function getPlaceholder(string $inputfile, string $mode = '')
4952
return;
5053
}
5154
$outputfile = $this->getOutputPath($inputfile);
52-
if (!\file_exists($outputfile) || filemtime($inputfile) > filemtime($outputfile)) {
55+
if (!\file_exists($outputfile) || (!$this->ignoreMtime && filemtime($inputfile) > filemtime($outputfile))) {
5356
// the following line may throw exceptions. do they have to be catched?
5457
// if so: what to do with the error?
5558
$this->generator->generate($inputfile, $outputfile);

0 commit comments

Comments
 (0)