1
1
<?php
2
2
namespace phpFastCache \Bundle \Command ;
3
3
4
+ use phpFastCache \Cache \ExtendedCacheItemPoolInterface ;
5
+ use phpFastCache \Exceptions \phpFastCacheDriverCheckException ;
4
6
use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
5
7
use Symfony \Component \Console \Input \InputArgument ;
6
8
use Symfony \Component \Console \Input \InputInterface ;
@@ -23,22 +25,55 @@ protected function configure()
23
25
}
24
26
protected function execute (InputInterface $ input , OutputInterface $ output )
25
27
{
28
+ $ failedInstances = [];
26
29
$ io = new SymfonyStyle ($ input , $ output );
27
30
28
31
$ phpFastCache = $ this ->getContainer ()->get ('phpfastcache ' );
29
32
$ 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 {
36
39
if (OutputInterface::VERBOSITY_VERBOSE <= $ output ->getVerbosity ()) {
37
- $ output ->writeln ("Cache {$ name } cleared " );
40
+ $ output ->writeln ("<fg=yellow>Clearing instance {$ name } cache...</> " );
38
41
}
39
42
$ 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 ));
40
76
}
41
- $ io ->success ('All caches cleared ' );
42
77
}
43
78
}
44
79
}
0 commit comments