Skip to content

Commit

Permalink
implement config option to disable mtime check
Browse files Browse the repository at this point in the history
  • Loading branch information
GenieTim committed Feb 14, 2019
1 parent 6af0d33 commit 7e7f785
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public function testSqipConfig()
$this->assertEquals($config['service'], 'bewe_placeholder.generator.sqip');
$this->assertNotEmpty($config['iterations']);
$this->assertEquals($config['iterations'], 13);
// test default
$this->assertNotEmpty($config['ignore_mtime']);
$this->assertEquals($config['ignore_mtime'], false);
}

/**
Expand All @@ -94,6 +97,8 @@ public function testPrimitiveConfig()
$this->assertEquals($config['service'], 'bewe_placeholder.generator.primitive');
$this->assertNotEmpty($config['iterations']);
$this->assertEquals($config['iterations'], 13);
$this->assertNotEmpty($config['ignore_mtime']);
$this->assertEquals($config['ignore_mtime'], true);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Tests/DependencyInjection/Fixtures/primitive-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bewe_placeholder:
bin: primitive
service: bewe_placeholder.generator.primitive
iterations: 13
iterations: 13
ignore_mtime: true
5 changes: 3 additions & 2 deletions src/Commands/PlaceholderPrepareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function configure()
// the name of the command (the part after "bin/console")
->setName('bewe:placeholder:prepare')
->addOption('dry')
->addOption('ignore-exist')
->addOption('ignore-mtime')

// the short description shown while running "php bin/console list"
->setDescription('Creates placeholders for all the images.')
Expand Down Expand Up @@ -53,7 +53,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$inputPath = $image->getRealPath();
$outputPath = $this->provider->getOutputPath($inputPath);
// only output if not already done in another session
if (!\file_exists($outputPath) || (!$input->getOption('ignore-exist') && filemtime($inputPath) > filemtime($outputPath))) {
// TODO: accept ignore_mtime in parameters too
if (!\file_exists($outputPath) || (!$input->getOption('ignore-mtime') && filemtime($inputPath) > filemtime($outputPath))) {
if (!$dry) {
// do output images
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function load(array $defaultConfigs, ContainerBuilder $container)
$providerDefinition->replaceArgument(2, $output_path);
}

if (\array_key_exists('ignore_mtime', $config)) {
$providerDefinition->replaceArgument(3, $config['ignore_mtime']);
}

$service = $config['service'];
$serviceDefinition = $container->getDefinition($config['service']);

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function getConfigTreeBuilder()
->scalarNode('bin')->end()
->scalarNode('node_bin')->end()
->scalarNode('output_path')->end()
->scalarNode('ignore_mtime')->end()
->end();

return $treeBuilder;
Expand Down
5 changes: 3 additions & 2 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ services:
- '@bewe_placeholder.generator'
- ["."]
- ~
# - '@logger'
- false

bewe_placeholder.generator.sqip:
class: BernhardWebstudio\PlaceholderBundle\Service\SqipPlaceholderGenerator
autowire: false
Expand All @@ -39,4 +40,4 @@ services:
arguments:
- 'primitive'
- ~
- 10
- 10
7 changes: 5 additions & 2 deletions src/Service/PlaceholderProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PlaceholderProviderService
protected $generator;
protected $loadPaths;
protected $outputPath;
protected $ignoreMtime;

/**
* The modes how a placeholder can be fetched
Expand All @@ -25,11 +26,13 @@ class PlaceholderProviderService
public function __construct(
PlaceholderGeneratorInterface $generator,
array $loadPaths = array(),
string $outputPath = null
string $outputPath = null,
$ignoreMtime = false
) {
$this->generator = $generator;
$this->loadPaths = $loadPaths;
$this->outputPath = $outputPath;
$this->ignoreMtime = $ignoreMtime;
}

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

0 comments on commit 7e7f785

Please sign in to comment.