| 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 RemoveCacheCommand extends AbstractCacheCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ->setName('liip:imagine:cache:remove') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             ->setDescription('Remove asset caches for the passed asset paths(s) and filter name(s)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->addArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'Asset paths to remove caches of (passing none will use all paths).') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 'Filter name to remove caches of (passing none will use all registered filters)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->addOption('machine-readable', 'm', InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'Enable machine parseable output (removing extraneous output and all text styles)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 'Deprecated in 1.9.0 and removed in 2.0.0 (use the --filter option instead)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ->setHelp(<<<'EOF' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | The <comment>%command.name%</comment> command removes asset cache for the passed image(s) and filter(s). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | For an application that has only the two files "foo.ext" and "bar.ext", and only the filter sets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | named "thumb_sm" and "thumb_lg", the following examples will behave as shown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | <comment># bin/console %command.name% foo.ext</comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | Removes caches for <options=bold>foo.ext</> using <options=bold>all configured filters</>, outputting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |   <info>- foo.ext[thumb_sm] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |   <info>- foo.ext[thumb_lg] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | <comment># bin/console %command.name% --filter=thumb_sm --filter=thumb_lg foo.ext bar.ext</comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | Removes caches for both <options=bold>foo.ext</> and <options=bold>bar.ext</> using <options=bold>thumb_sm</> and <options=bold>thumb_lg</> filters, outputting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |   <info>- foo.ext[thumb_sm] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |   <info>- foo.ext[thumb_lg] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |   <info>- bar.ext[thumb_sm] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |   <info>- bar.ext[thumb_lg] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | <comment># bin/console %command.name% --filter=thumb_sm</comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | Removes <options=bold>all caches</> for <options=bold>thumb_sm</> filter, outputting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |   <info>- *[thumb_sm] glob-removal</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | <comment># bin/console %command.name%</comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | Removes <options=bold>all caches</> for <options=bold>all configured filters</>, removing all cached assets, outputting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |   <info>- *[thumb_sm] glob-removal</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |   <info>- *[thumb_lg] glob-removal</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | <comment># bin/console %command.name% --force --filter=thumb_sm foo.ext</comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | Removing caches for <options=bold>foo.ext</> using <options=bold>thumb_sm</> filter when <options=bold>already removed</> will caused <options=bold>skipping</>, outputting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |   <info>- foo.ext[thumb_sm] skipped</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | <comment># bin/console %command.name% --filter=does_not_exist --filter=thumb_sm foo.ext</comment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | Removes caches for <options=bold>foo.ext</> for <options=bold>thumb_sm</> while <options=bold>failing inline</> on invalid filter (or other errors), outputting: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |   <info>- foo.ext[does_not_exist] failure: Could not find configuration for a filter: does_not_exist</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |   <info>- foo.ext[thumb_sm] removed</> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | EOF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param InputInterface  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->initializeInstState($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->writeCommandHeading('remove'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $filters = $this->resolveFilters($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $targets = $input->getArgument('paths'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if (0 === count($targets)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->doCacheRemoveAsGlobbedFilterName($filters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->doCacheRemoveAsFiltersAndTargets($filters, $targets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $this->getReturnCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string[] $filters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     private function doCacheRemoveAsGlobbedFilterName(array $filters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         foreach ($filters as $f) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->doCacheRemove($f); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->writeResultSummary($filters, array(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param string[] $filters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param string[] $targets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     private function doCacheRemoveAsFiltersAndTargets(array $filters, array $targets) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         foreach ($targets as $t) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             foreach ($filters as $f) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $this->doCacheRemove($f, $t); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->writeResultSummary($filters, $targets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param string      $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param string|null $target | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |     private function doCacheRemove($filter, $target = null) | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |         $this->writeActionStart($filter, $target); | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |         try { | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |             if (null === $target) { | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |                 $this->getCacheManager()->remove(null, $filter); | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |                 $this->writeActionResult('glob-removal', false); | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |             } elseif ($this->getCacheManager()->isStored($target, $filter)) { | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |                 $this->getCacheManager()->remove($target, $filter); | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |                 $this->writeActionResult('removed', false); | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |             } else { | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |                 $this->writeActionResult('skipped', false); | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |         } catch (\Exception $e) { | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |             $this->writeActionException($e); | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |