Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
ff809683
Commit
ff809683
authored
Aug 31, 2014
by
Mark
Committed by
Carsten Brandt
Sep 01, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cache controller adjustments and improvements
related to #4792, close #4874
parent
92b958cd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
280 additions
and
24 deletions
+280
-24
CacheController.php
framework/console/controllers/CacheController.php
+182
-24
CacheControllerTest.php
...nit/framework/console/controllers/CacheControllerTest.php
+98
-0
No files found.
framework/console/controllers/CacheController.php
View file @
ff809683
...
...
@@ -9,13 +9,26 @@ namespace yii\console\controllers;
use
Yii
;
use
yii\console\Controller
;
use
yii\console\Exception
;
use
yii\caching\Cache
;
use
yii\helpers\Console
;
use
yii\console\Exception
;
/**
* Allows you to flush cache.
*
* ~~~
* #see list of available components to flush
* yii cache
*
* #flush particular components specified by their names
* yii cache/flush first second third
*
* #flush all cache components that can be found in the system
* yii cache/flush-all
* ~~~
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0
*/
class
CacheController
extends
Controller
...
...
@@ -25,43 +38,188 @@ class CacheController extends Controller
*/
public
function
actionIndex
()
{
$caches
=
[];
$components
=
Yii
::
$app
->
getComponents
();
foreach
(
$components
as
$name
=>
$component
)
{
if
(
$component
instanceof
Cache
)
{
$caches
[
$name
]
=
get_class
(
$component
);
}
elseif
(
is_array
(
$component
)
&&
isset
(
$component
[
'class'
])
&&
strpos
(
$component
[
'class'
],
'Cache'
)
!==
false
)
{
$caches
[
$name
]
=
$component
[
'class'
];
$caches
=
$this
->
findCaches
();
if
(
!
empty
(
$caches
))
{
$this
->
notifyCachesCanBeFlushed
(
$caches
);
}
else
{
$this
->
notifyNoCachesFound
();
}
}
if
(
!
empty
(
$caches
))
{
echo
"The following caches can be flushed:
\n\n
"
;
/**
* Flushes given cache components.
* For example,
*
* ~~~
* # flushes caches specified by their id: "first", "second", "third"
* yii cache/flush first second third
* ~~~
*
*/
public
function
actionFlush
()
{
$cachesInput
=
func_get_args
();
if
(
empty
(
$cachesInput
))
{
throw
new
Exception
(
"You should specify cache components names"
);
}
$caches
=
$this
->
findCaches
(
$cachesInput
);
$cachesInfo
=
[];
$foundCaches
=
array_keys
(
$caches
);
$notFoundCaches
=
array_diff
(
$cachesInput
,
array_keys
(
$caches
));
if
(
$notFoundCaches
)
{
$this
->
notifyNotFoundCaches
(
$notFoundCaches
);
}
if
(
!
$foundCaches
)
{
$this
->
notifyNoCachesFound
();
return
static
::
EXIT_CODE_NORMAL
;
}
if
(
!
$this
->
confirmFlush
(
$foundCaches
))
{
return
static
::
EXIT_CODE_NORMAL
;
}
foreach
(
$caches
as
$name
=>
$class
)
{
echo
" *
$name
:
$class
\n
"
;
$cachesInfo
[]
=
[
'name'
=>
$name
,
'class'
=>
$class
,
'is_flushed'
=>
Yii
::
$app
->
get
(
$name
)
->
flush
(),
];
}
}
else
{
echo
"No cache is used.
\n
"
;
$this
->
notifyFlushed
(
$cachesInfo
);
}
/**
* Flushes all caches registered in the system.
*/
public
function
actionFlushAll
()
{
$caches
=
$this
->
findCaches
();
$cachesInfo
=
[];
if
(
empty
(
$caches
))
{
$this
->
notifyNoCachesFound
();
return
static
::
EXIT_CODE_NORMAL
;
}
foreach
(
$caches
as
$name
=>
$class
)
{
$cachesInfo
[]
=
[
'name'
=>
$name
,
'class'
=>
$class
,
'is_flushed'
=>
Yii
::
$app
->
get
(
$name
)
->
flush
(),
];
}
$this
->
notifyFlushed
(
$cachesInfo
);
}
/**
* Notifies user that given caches are found and can be flushed.
* @param array $caches array of cache component classes
*/
private
function
notifyCachesCanBeFlushed
(
$caches
)
{
$this
->
stdout
(
"The following caches were found in the system:
\n\n
"
,
Console
::
FG_YELLOW
);
foreach
(
$caches
as
$name
=>
$class
)
{
$this
->
stdout
(
"
\t
*
$name
(
$class
)
\n
"
,
Console
::
FG_GREEN
);
}
$this
->
stdout
(
"
\n
"
);
}
/**
* Notifies user that there was not found any cache in the system.
*/
private
function
notifyNoCachesFound
()
{
$this
->
stdout
(
"No cache components were found in the system.
\n
"
,
Console
::
FG_RED
);
}
/**
* Notifies user that given cache components were not found in the system.
* @param array $cachesNames
*/
private
function
notifyNotFoundCaches
(
$cachesNames
)
{
$this
->
stdout
(
"The following cache components were NOT found:
\n\n
"
,
Console
::
FG_RED
);
foreach
(
$cachesNames
as
$name
)
{
$this
->
stdout
(
"
\t
*
$name
\n
"
,
Console
::
FG_GREEN
);
}
$this
->
stdout
(
"
\n
"
);
}
/**
* Flushes cache.
* @param string $component Name of the cache application component to use.
*
* @throws \yii\console\Exception
* @param array $caches
*/
private
function
notifyFlushed
(
$caches
)
{
$this
->
stdout
(
"The following cache components were processed:
\n\n
"
,
Console
::
FG_YELLOW
);
foreach
(
$caches
as
$cache
)
{
$this
->
stdout
(
"
\t
* "
.
$cache
[
'name'
]
.
" ("
.
$cache
[
'class'
]
.
")"
,
Console
::
FG_GREEN
);
if
(
!
$cache
[
'is_flushed'
])
{
$this
->
stdout
(
" - not flushed
\n
"
,
Console
::
FG_RED
);
}
else
{
$this
->
stdout
(
"
\n
"
);
}
}
$this
->
stdout
(
"
\n
"
);
}
/**
* Prompts user with confirmation if caches should be flushed.
* @param array $cachesNames
* @return boolean
*/
private
function
confirmFlush
(
$cachesNames
)
{
$this
->
stdout
(
"The following cache components will be flushed:
\n\n
"
,
Console
::
FG_YELLOW
);
foreach
(
$cachesNames
as
$name
)
{
$this
->
stdout
(
"
\t
*
$name
\n
"
,
Console
::
FG_GREEN
);
}
return
$this
->
confirm
(
"
\n
Flush above cache components?"
);
}
/**
* Returns array of caches in the system, keys are cache components names, values are class names.
* @param array $cachesNames caches to be found
* @return array
*/
p
ublic
function
actionFlush
(
$component
=
'cache'
)
p
rivate
function
findCaches
(
array
$cachesNames
=
[]
)
{
/* @var $cache Cache */
$cache
=
Yii
::
$app
->
get
(
$component
,
false
);
if
(
!
$cache
||
!
$cache
instanceof
Cache
)
{
throw
new
Exception
(
'Application component "'
.
$component
.
'" is not defined or not a cache.'
);
$caches
=
[];
$components
=
Yii
::
$app
->
getComponents
();
$findAll
=
(
$cachesNames
==
[]);
foreach
(
$components
as
$name
=>
$component
)
{
if
(
!
$findAll
&&
!
in_array
(
$name
,
$cachesNames
))
{
continue
;
}
if
(
!
$cache
->
flush
())
{
throw
new
Exception
(
'Unable to flush cache.'
);
if
(
$component
instanceof
Cache
)
{
$caches
[
$name
]
=
get_class
(
$component
);
}
elseif
(
is_array
(
$component
)
&&
isset
(
$component
[
'class'
])
&&
strpos
(
$component
[
'class'
],
'Cache'
)
!==
false
)
{
$caches
[
$name
]
=
$component
[
'class'
];
}
elseif
(
is_string
(
$component
)
&&
strpos
(
$component
,
'Cache'
)
!==
false
)
{
$caches
[
$name
]
=
$component
;
}
}
echo
"
\n
Done.
\n
"
;
return
$caches
;
}
}
tests/unit/framework/console/controllers/CacheControllerTest.php
0 → 100644
View file @
ff809683
<?php
namespace
yiiunit\framework\console\controllers
;
use
Yii
;
use
yiiunit\TestCase
;
use
yii\console\controllers\CacheController
;
class
CacheControllerTest
extends
TestCase
{
/**
* @var \yiiunit\framework\console\controllers\CacheConsoledController
*/
private
$_cacheController
;
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
_cacheController
=
Yii
::
createObject
([
'class'
=>
'yiiunit\framework\console\controllers\CacheConsoledController'
,
'interactive'
=>
false
,
],[
null
,
null
]);
//id and module are null
$this
->
mockApplication
([
'components'
=>
[
'firstCache'
=>
'yii\caching\ArrayCache'
,
'secondCache'
=>
'yii\caching\ArrayCache'
,
],
]);
}
public
function
testFlushOne
()
{
Yii
::
$app
->
firstCache
->
set
(
'firstKey'
,
'firstValue'
);
Yii
::
$app
->
firstCache
->
set
(
'secondKey'
,
'secondValue'
);
Yii
::
$app
->
secondCache
->
set
(
'thirdKey'
,
'thirdValue'
);
$this
->
_cacheController
->
actionFlush
(
'firstCache'
);
$this
->
assertFalse
(
Yii
::
$app
->
firstCache
->
get
(
'firstKey'
),
'first cache data should be flushed'
);
$this
->
assertFalse
(
Yii
::
$app
->
firstCache
->
get
(
'secondKey'
),
'first cache data should be flushed'
);
$this
->
assertEquals
(
'thirdValue'
,
Yii
::
$app
->
secondCache
->
get
(
'thirdKey'
),
'second cache data should not be flushed'
);
}
public
function
testFlushBoth
()
{
Yii
::
$app
->
firstCache
->
set
(
'firstKey'
,
'firstValue'
);
Yii
::
$app
->
firstCache
->
set
(
'secondKey'
,
'secondValue'
);
Yii
::
$app
->
secondCache
->
set
(
'thirdKey'
,
'secondValue'
);
$this
->
_cacheController
->
actionFlush
(
'firstCache'
,
'secondCache'
);
$this
->
assertFalse
(
Yii
::
$app
->
firstCache
->
get
(
'firstKey'
),
'first cache data should be flushed'
);
$this
->
assertFalse
(
Yii
::
$app
->
firstCache
->
get
(
'secondKey'
),
'first cache data should be flushed'
);
$this
->
assertFalse
(
Yii
::
$app
->
secondCache
->
get
(
'thirdKey'
),
'second cache data should be flushed'
);
}
public
function
testNotFoundFlush
()
{
Yii
::
$app
->
firstCache
->
set
(
'firstKey'
,
'firstValue'
);
$this
->
_cacheController
->
actionFlush
(
'notExistingCache'
);
$this
->
assertEquals
(
'firstValue'
,
Yii
::
$app
->
firstCache
->
get
(
'firstKey'
),
'first cache data should not be flushed'
);
}
/**
* @expectedException yii\console\Exception
*/
public
function
testNothingToFlushException
()
{
$this
->
_cacheController
->
actionFlush
();
}
public
function
testFlushAll
()
{
Yii
::
$app
->
firstCache
->
set
(
'firstKey'
,
'firstValue'
);
Yii
::
$app
->
secondCache
->
set
(
'thirdKey'
,
'secondValue'
);
$this
->
_cacheController
->
actionFlushAll
();
$this
->
assertFalse
(
Yii
::
$app
->
firstCache
->
get
(
'firstKey'
),
'first cache data should be flushed'
);
$this
->
assertFalse
(
Yii
::
$app
->
secondCache
->
get
(
'thirdKey'
),
'second cache data should be flushed'
);
}
}
class
CacheConsoledController
extends
CacheController
{
public
function
stdout
(
$string
)
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment