OSDN Git Service

Extlibアイデアブランチのマージ (#16100)
[ethna/ethna.git] / class / Plugin / Handle / ClearCache.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  ClearCache.php
5  *
6  *  @author     ICHII Takashi <ichii386@schweetheart.jp>
7  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
8  *  @package    Ethna
9  *  @version    $Id$
10  */
11
12 require_once ETHNA_BASE . '/class/Ethna_PearWrapper.php';
13
14 // {{{ Ethna_Plugin_Handle_ClearCache
15 /**
16  *  clear-cache handler
17  *
18  *  @author     ICHII Takashi <ichii386@schweetheart.jp>
19  *  @access     public
20  *  @package    Ethna
21  */
22 class Ethna_Plugin_Handle_ClearCache extends Ethna_Plugin_Handle
23 {
24     /**
25      *  clear cache files.
26      *
27      *  @access public
28      *  @todo   implement Ethna_Renderer::clear_cache();
29      *  @todo   implement Ethna_Plugin_Cachemanager::clear_cache();
30      *  @todo   avoid echo, printf
31      */
32     function perform()
33     {
34         $r =& $this->_getopt(array('basedir=', 
35                                    'any-tmp-files', 'smarty', 'pear', 'cachemanager'));
36         if (Ethna::isError($r)) {
37             return $r;
38         }
39         list($args,) = $r;
40
41         $basedir = isset($args['basedir']) ? realpath(end($args['basedir'])) : getcwd();
42         $controller =& Ethna_Handle::getAppController($basedir);
43         if (Ethna::isError($controller)) {
44             return $controller;
45         }
46         $tmp_dir = $controller->getDirectory('tmp');
47
48         if (isset($args['smarty']) || isset($args['any-tmp-files'])) {
49             echo "cleaning smarty caches, compiled templates...";
50             $renderer =& $controller->getRenderer();
51             if (strtolower(get_class($renderer)) == "ethna_renderer_smarty") {
52                 $renderer->engine->clear_all_cache();
53                 $renderer->engine->clear_compiled_tpl();
54             }
55             echo " done\n";
56         }
57
58         if (isset($args['cachemanager']) || isset($args['any-tmp-files'])) {
59             echo "cleaning Ethna_Plugin_Cachemanager caches...";
60             $cache_dir = sprintf("%s/cache", $tmp_dir);
61             Ethna_Util::purgeDir($cache_dir);
62             echo " done\n";
63         }
64
65         if (isset($args['pear']) || isset($args['any-tmp-files'])) {
66             echo "cleaning pear caches...";
67             ob_start();
68             $pear =& new Ethna_PearWrapper();
69             $r =& $pear->init('local', $basedir); 
70             if (Ethna::isError($r)) {
71                 echo ob_get_clean();
72                 return $r;
73             }
74             $r =& $pear->doClearCache();
75             if (Ethna::isError($r)) {
76                 echo ob_get_clean();
77                 return $r;
78             }
79             ob_get_clean();
80             echo " done\n";
81         }
82
83         if (isset($args['any-tmp-files'])) {
84             echo "cleaning tmp dirs...";
85             // purge only entries in tmp.
86             if ($dh = opendir($tmp_dir)) {
87                 while (($entry = readdir($dh)) !== false) {
88                     if ($entry === '.' || $entry === '..') {
89                         continue;
90                     }
91                     Ethna_Util::purgeDir("{$tmp_dir}/{$entry}");
92                 }
93                 closedir($dh);
94             }
95             echo " done\n";
96         }
97
98         return true;
99     }
100
101     // {{{ getDescription()
102     /**
103      *  @access public
104      */
105     function getDescription()
106     {
107         return <<<EOS
108 clear project's cache files:
109     {$this->id} [-b|--basedir=dir] [-a|--any-tmp-files] [-s|--smarty] [-p|--pear] [-c|--cachemanager]
110
111 EOS;
112     }
113     // }}}
114
115     // {{{ getUsage()
116     /**
117      *  @access public
118      */
119     function getUsage()
120     {
121         return <<<EOS
122 ethna {$this->id} [-b|--basedir=dir] [-a|--any-tmp-files] [-s|--smarty] [-p|--pear] [-c|--cachemanager]
123 EOS;
124     }
125     // }}}
126 }
127 // }}}
128 ?>