11<?php
22namespace phpFastCache \Bundle \Command ;
33
4+ use phpFastCache \Cache \ExtendedCacheItemPoolInterface ;
5+ use phpFastCache \Exceptions \phpFastCacheDriverCheckException ;
46use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
57use Symfony \Component \Console \Input \InputArgument ;
68use Symfony \Component \Console \Input \InputInterface ;
@@ -23,22 +25,55 @@ protected function configure()
2325 }
2426 protected function execute (InputInterface $ input , OutputInterface $ output )
2527 {
28+ $ failedInstances = [];
2629 $ io = new SymfonyStyle ($ input , $ output );
2730
2831 $ phpFastCache = $ this ->getContainer ()->get ('phpfastcache ' );
2932 $ driver = $ input ->getArgument ('driver ' );
30- if ( $ driver ) {
31- $ phpFastCache -> get ( $ driver )-> clear ( );
32- $ io -> success ( " Cache { $ driver } cleared " );
33- } else {
34- $ caches = $ this -> getContainer ()-> getParameter ( ' phpfastcache ' );
35- foreach ( $ caches [ ' drivers ' ] as $ name => $ parameters ) {
33+
34+ $ output -> writeln ( " <bg=yellow;fg=red>Clearing cache operation can take a while, please be patient...</> " );
35+
36+ $ callback = function ( $ name ) use ( $ phpFastCache , $ output , & $ failedInstances )
37+ {
38+ try {
3639 if (OutputInterface::VERBOSITY_VERBOSE <= $ output ->getVerbosity ()) {
37- $ output ->writeln ("Cache {$ name } cleared " );
40+ $ output ->writeln ("<fg=yellow>Clearing instance {$ name } cache...</> " );
3841 }
3942 $ phpFastCache ->get ($ name )->clear ();
43+ if (OutputInterface::VERBOSITY_VERBOSE <= $ output ->getVerbosity ()) {
44+ $ output ->writeln ("<fg=green>Cache instance {$ name } cleared</> " );
45+ }
46+ }catch (phpFastCacheDriverCheckException $ e ){
47+ $ failedInstances [] = $ name ;
48+ if (OutputInterface::VERBOSITY_VERBOSE <= $ output ->getVerbosity ()) {
49+ $ output ->writeln ("<fg=red>Cache instance {$ name } not cleared, got exception: " . "<bg=red;options=bold> " . $ e ->getMessage () ."</> " );
50+ }else {
51+ $ output ->writeln ("<fg=red>Cache instance {$ name } not cleared (increase verbosity to get more information).</> " );
52+ }
53+ }
54+ };
55+ $ caches = $ this ->getContainer ()->getParameter ('phpfastcache ' );
56+
57+ if ($ driver ) {
58+ if (array_key_exists ($ driver , $ caches ['drivers ' ])){
59+ $ callback ($ driver );
60+ if (!count ($ failedInstances )){
61+ $ io ->success ("Cache instance {$ driver } cleared " );
62+ }else {
63+ $ io ->error ("Cache instance {$ driver } not cleared " );
64+ }
65+ }else {
66+ $ io ->error ("Cache instance {$ driver } does not exists " );
67+ }
68+ } else {
69+ foreach ($ caches ['drivers ' ] as $ name => $ parameters ) {
70+ $ callback ($ name );
71+ }
72+ if (!count ($ failedInstances )){
73+ $ io ->success ('All caches instances got cleared ' );
74+ }else {
75+ $ io ->success ('Almost all caches instances got cleared, except these: ' . implode (', ' , $ failedInstances ));
4076 }
41- $ io ->success ('All caches cleared ' );
4277 }
4378 }
4479}
0 commit comments