-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathXCleanCommand.php
91 lines (82 loc) · 1.81 KB
/
XCleanCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* XCleanCommand class file.
*
* This command allows you to clean up various temporary data Yii and an application are generating.
*
* To use this command, first map it in protected/config/console.php as follows:
* return array(
* 'commandMap' => array(
* 'clean'=>array(
* 'class'=>'ext.commands.XCleanCommand',
* 'webroot'=>'path/to/your/application/webroot'
* )
* )
* );
*
* Now, under protected directory, you can run commands:
* yiic clean
* yiic clean cache
* yiic clean assets
* yiic clean runtime
*/
class XCleanCommand extends CConsoleCommand
{
public $webroot;
public function getHelp()
{
$out = "Clean command allows you to clean up various temporary data Yii and an application are generating.\n\n";
return $out.parent::getHelp();
}
public function actionCache()
{
$cache=Yii::app()->getComponent('cache');
if($cache!==null){
$cache->flush();
echo "Done.\n";
}
else {
echo "Please configure cache component.\n";
}
}
public function actionAssets()
{
if(empty($this->webroot))
{
echo "Please specify a path to webroot in command properties.\n";
Yii::app()->end();
}
$this->cleanDir($this->webroot.'/assets');
echo "Done.\n";
}
public function actionRuntime()
{
$this->cleanDir(Yii::app()->getRuntimePath());
echo "Done.\n";
}
private function cleanDir($dir)
{
$di = new DirectoryIterator($dir);
foreach($di as $d)
{
if(!$d->isDot())
{
echo "Removed ".$d->getPathname()."\n";
$this->removeDirRecursive($d->getPathname());
}
}
}
private function removeDirRecursive($dir)
{
$files = glob($dir.'*', GLOB_MARK);
foreach ($files as $file)
{
if (is_dir($file))
$this->removeDirRecursive($file);
else
unlink($file);
}
if (is_dir($dir))
rmdir($dir);
}
}