Completed
Push — upgrade-2.0-from-1.0 ( 918602 )
by David
13:04
created

ResolveCacheCommand::doCacheResolve()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 8
nop 3
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Command;
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class ResolveCacheCommand extends AbstractCacheCommand
20
{
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('liip:imagine:cache:resolve')
25
            ->setDescription('Resolves asset caches for the passed asset paths(s) and filter set name(s)')
26
            ->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY,
27
                'Asset paths to resolve caches for')
28
            ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
29
                'Filter name to resolve caches for (passing none will use all registered filters)')
30
            ->addOption('force', 'F', InputOption::VALUE_NONE,
31
                'Force asset cache resolution (ignoring whether it already cached)')
32
            ->addOption('machine-readable', 'm', InputOption::VALUE_NONE,
33
                'Enable machine parseable output (removing extraneous output and all text styles)')
34
            ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
35
                'Deprecated in 1.9.0 and removed in 2.0.0 (use the --filter option instead)')
36
            ->setHelp(<<<'EOF'
37
The <comment>%command.name%</comment> command resolves asset cache for the passed image(s) and filter(s).
38
39
For an application that has only the two files "foo.ext" and "bar.ext", and only the filter sets
40
named "thumb_sm" and "thumb_lg", the following examples will behave as shown.
41
42
<comment># bin/console %command.name% foo.ext bar.ext</comment>
43
Resolves caches for <options=bold>both</> <options=bold>foo.ext</> and <options=bold>bar.ext</> using <options=bold>all configured filters</>, outputting:
44
  <info>- foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext</>
45
  <info>- bar.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/bar.ext</>
46
  <info>- foo.ext[thumb_lg] resolved: http://localhost/media/cache/thumb_lg/foo.ext</>
47
  <info>- bar.ext[thumb_lg] resolved: http://localhost/media/cache/thumb_lg/bar.ext</>
48
49
<comment># bin/console %command.name% --filter=thumb_sm foo.ext</comment>
50
Resolves caches for <options=bold>foo.ext</> using <options=bold>only</> <options=bold>thumb_sm</> filter, outputting:
51
  <info>- foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext</>
52
53
<comment># bin/console %command.name% --filter=thumb_sm --filter=thumb_lg foo.ext</comment>
54
Resolves caches for <options=bold>foo.ext</> using <options=bold>both</> <options=bold>thumb_sm</> and <options=bold>thumb_lg</> filters, outputting:
55
  <info>- foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext</>
56
  <info>- foo.ext[thumb_lg] resolved: http://localhost/media/cache/thumb_lg/foo.ext</>
57
58
<comment># bin/console %command.name% --force --filter=thumb_sm foo.ext</comment>
59
Resolving caches for <options=bold>foo.ext</> using <options=bold>thumb_sm</> filter when <options=bold>already cached</> will caused <options=bold>skipped</>, outputting:
60
  <info>- foo.ext[thumb_sm] skipped: http://localhost/media/cache/thumb_sm/foo.ext</>
61
62
<comment># bin/console %command.name% --force --filter=thumb_sm foo.ext</comment>
63
Resolving caches for <options=bold>foo.ext</> using <options=bold>thumb_sm</> filter when <options=bold>already cached</> with <options=bold>force</> option <options=bold>re-resolves</> (ignoring cache), outputting:
64
  <info>- foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext</>
65
66
<comment># bin/console %command.name% --filter=does_not_exist --filter=thumb_sm foo.ext</comment>
67
Resolves caches for <options=bold>foo.ext</> using <options=bold>thumb_sm</> while <options=bold>failing inline</> on invalid filter (or other errors), outputting:
68
  <info>- foo.ext[does_not_exist] failed: Could not find configuration for a filter: does_not_exist</>
69
  <info>- foo.ext[thumb_sm] removed: http://localhost/media/cache/thumb_sm/foo.ext</>
70
71
EOF
72
            );
73
    }
74
75
    /**
76
     * @param InputInterface  $input
77
     * @param OutputInterface $output
78
     *
79
     * @return int
80
     */
81
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        $this->initializeInstState($input, $output);
84
        $this->writeCommandHeading('resolve');
85
86
        $filters = $this->resolveFilters($input);
87
        $targets = $input->getArgument('paths');
88
        $doForce = $input->getOption('force');
89
90
        foreach ($targets as $t) {
91
            foreach ($filters as $f) {
92
                $this->doCacheResolve($t, $f, $doForce);
93
            }
94
        }
95
96
        $this->writeResultSummary($filters, $targets);
97
98
        return $this->getReturnCode();
99
    }
100
101
    /**
102
     * @param string $target
103
     * @param string $filter
104
     * @param bool   $forced
105
     */
106
    private function doCacheResolve($target, $filter, $forced)
107
    {
108
        $this->writeActionStart($filter, $target);
109
110
        try {
111
            if ($forced || !$this->getCacheManager()->isStored($target, $filter)) {
112
                $this->getCacheManager()->store($this->getFilterManager()->applyFilter($this->getDataManager()->find($filter, $target), $filter), $target, $filter);
113
                $this->writeActionResult('resolved');
114
            } else {
115
                $this->writeActionResult('skipped');
116
            }
117
118
            $this->writeActionDetail($this->getCacheManager()->resolve($target, $filter));
119
        } catch (\Exception $e) {
120
            $this->writeActionException($e);
121
        }
122
    }
123
}
124