Skip to content

Commit d5a5d9e

Browse files
committed
Add FindCommand
1 parent 704b68b commit d5a5d9e

File tree

5 files changed

+112
-12
lines changed

5 files changed

+112
-12
lines changed

command.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use TypistTech\ImageOptimizeCommand\CLI\Commands\AttachmentCommand;
88
use TypistTech\ImageOptimizeCommand\CLI\Commands\BatchCommand;
99
use TypistTech\ImageOptimizeCommand\CLI\Commands\CommandNamespace;
10+
use TypistTech\ImageOptimizeCommand\CLI\Commands\FindCommand;
1011
use TypistTech\ImageOptimizeCommand\CLI\Commands\ResetCommand;
1112
use TypistTech\ImageOptimizeCommand\CLI\Commands\RestoreCommand;
1213
use WP_CLI;
@@ -16,7 +17,11 @@
1617
}
1718

1819
WP_CLI::add_command('image-optimize', CommandNamespace::class);
20+
1921
WP_CLI::add_command('image-optimize attachment', AttachmentCommand::class);
2022
WP_CLI::add_command('image-optimize batch', BatchCommand::class);
23+
2124
WP_CLI::add_command('image-optimize restore', RestoreCommand::class);
2225
WP_CLI::add_command('image-optimize reset', ResetCommand::class);
26+
27+
WP_CLI::add_command('image-optimize find', FindCommand::class);

src/CLI/Commands/CommandNamespace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*
2323
* # Restore all full sized images and drop all meta flags
2424
* $ wp image-optimize reset
25+
*
26+
* # Find and optimize images under a given directory.
27+
* $ wp image-optimize find /path/to/my/directory
2528
*/
2629
class CommandNamespace extends BaseCommandNamespace
2730
{

src/CLI/Commands/FindCommand.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TypistTech\ImageOptimizeCommand\CLI\Commands;
5+
6+
use Symfony\Component\Filesystem\Filesystem;
7+
use Symfony\Component\Finder\Finder;
8+
use TypistTech\ImageOptimizeCommand\CLI\LoggerFactory;
9+
use TypistTech\ImageOptimizeCommand\Operations\Find;
10+
use TypistTech\ImageOptimizeCommand\Operations\Optimize;
11+
use TypistTech\ImageOptimizeCommand\OptimizerChainFactory;
12+
13+
class FindCommand
14+
{
15+
/**
16+
* Find and optimize images under a given directory.
17+
*
18+
* ## OPTIONS
19+
*
20+
* <directory>
21+
* : Path to the directory.
22+
*
23+
* [--extensions=<extensions>]
24+
* : File types to optimize, separated by commas.
25+
* Default: gif,jpeg,jpg,png
26+
*
27+
* ## EXAMPLES
28+
*
29+
* # Find and optimize images under /path/to/my/directory
30+
* $ wp image-optimize find /path/to/my/directory
31+
*
32+
* # Find and optimize SVGs,PNGs under /path/to/my/directory
33+
* $ wp image-optimize find /path/to/my/directory --extensions=svg,png
34+
*/
35+
public function __invoke($args, $assocArgs): void
36+
{
37+
$extensions = $assocArgs['extensions'] ?? 'gif,jpeg,jpg,png';
38+
$extensions = explode(',', $extensions);
39+
40+
$logger = LoggerFactory::create();
41+
$findOperation = new Find(
42+
new Finder(),
43+
$logger
44+
);
45+
46+
$optimizeOperation = new Optimize(
47+
OptimizerChainFactory::create(),
48+
new Filesystem(),
49+
$logger
50+
);
51+
52+
$images = $findOperation->execute($args[0], ...$extensions);
53+
$optimizeOperation->execute(...$images);
54+
}
55+
}

src/Operations/Find.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TypistTech\ImageOptimizeCommand\Operations;
66

7+
use InvalidArgumentException;
78
use Symfony\Component\Finder\Finder;
89
use Symfony\Component\Finder\SplFileInfo;
910
use TypistTech\ImageOptimizeCommand\LoggerInterface;
@@ -47,18 +48,43 @@ public function __construct(Finder $finder, LoggerInterface $logger)
4748
*/
4849
public function execute(string $directory, string ...$extensions): array
4950
{
51+
$this->logger->section('Finding images');
52+
5053
$directory = normalize_path($directory);
5154
$pattern = sprintf(
5255
'/\.(%1$s)$/',
5356
implode('|', $extensions)
5457
);
5558

56-
$files = $this->finder->files()
57-
->in($directory)
58-
->name($pattern);
59+
$this->logger->info('Directory: ' . $directory);
60+
$this->logger->info('Pattern: ' . $pattern);
61+
62+
$files = $this->find($directory, $pattern);
63+
$this->logger->notice(count($files) . 'image(s) found.');
64+
65+
return array_values($files);
66+
}
67+
68+
/**
69+
* Find files under a directory with specific name pattern.
70+
*
71+
* @param string $directory Normalize_directory path.
72+
* @param string $pattern File name regex.
73+
*
74+
* @return string[]
75+
*/
76+
protected function find(string $directory, string $pattern): array
77+
{
78+
try {
79+
$files = $this->finder->files()->in($directory)->name($pattern);
80+
81+
return array_map(function (SplFileInfo $file): string {
82+
return $file->getRealPath();
83+
}, iterator_to_array($files));
5984

60-
return array_map(function (SplFileInfo $file): string {
61-
return $file->getRealPath();
62-
}, iterator_to_array($files));
85+
// phpcs:ignore
86+
} catch (InvalidArgumentException $exception) {
87+
return [];
88+
}
6389
}
6490
}

tests/unit/Operations/FindTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FindTest extends Unit
2828
*/
2929
protected $testDir;
3030

31-
public function testFind()
31+
public function testFindSuccess()
3232
{
3333
$finder = new Finder();
3434
$logger = Mockery::spy(LoggerInterface::class);
@@ -49,6 +49,17 @@ public function testFind()
4949
$this->assertSame($expected, $actual);
5050
}
5151

52+
public function testFindNotExistDir()
53+
{
54+
$finder = new Finder();
55+
$logger = Mockery::spy(LoggerInterface::class);
56+
$find = new Find($finder, $logger);
57+
58+
$actual = $find->execute('/not/exist', 'png', 'txt', 'original', 'xyz');
59+
60+
$this->assertSame([], $actual);
61+
}
62+
5263
protected function _before()
5364
{
5465
$this->filesystem = $this->getModule('Filesystem');
@@ -60,11 +71,11 @@ protected function _before()
6071
);
6172

6273
WP_Mock::userFunction('WP_CLI\Utils\normalize_path')
63-
->with(Mockery::type('string'))
64-
->andReturnUsing(function ($arg) {
65-
return $arg;
66-
})
67-
->zeroOrMoreTimes();
74+
->with(Mockery::type('string'))
75+
->andReturnUsing(function ($arg) {
76+
return $arg;
77+
})
78+
->zeroOrMoreTimes();
6879
}
6980

7081
protected function _after()

0 commit comments

Comments
 (0)