1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Shell;
16:
17: use Cake\Console\Shell;
18: use Cake\Database\SchemaCache;
19: use Cake\Datasource\ConnectionManager;
20: use RuntimeException;
21:
22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class SchemaCacheShell extends Shell
32: {
33:
34: 35: 36: 37: 38: 39:
40: public function build($name = null)
41: {
42: $cache = $this->_getSchemaCache();
43: $tables = $cache->build($name);
44:
45: foreach ($tables as $table) {
46: $this->verbose(sprintf('Cached "%s"', $table));
47: }
48:
49: $this->out('<success>Cache build complete</success>');
50:
51: return true;
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function clear($name = null)
61: {
62: $cache = $this->_getSchemaCache();
63: $tables = $cache->clear($name);
64:
65: foreach ($tables as $table) {
66: $this->verbose(sprintf('Cleared "%s"', $table));
67: }
68:
69: $this->out('<success>Cache clear complete</success>');
70:
71: return true;
72: }
73:
74: 75: 76: 77: 78:
79: protected function _getSchemaCache()
80: {
81: try {
82: $connection = ConnectionManager::get($this->params['connection']);
83:
84: return new SchemaCache($connection);
85: } catch (RuntimeException $e) {
86: $this->abort($e->getMessage());
87: }
88: }
89:
90: 91: 92: 93: 94:
95: public function getOptionParser()
96: {
97: $parser = parent::getOptionParser();
98: $parser->addSubcommand('clear', [
99: 'help' => 'Clear all metadata caches for the connection. If a ' .
100: 'table name is provided, only that table will be removed.',
101: ])->addSubcommand('build', [
102: 'help' => 'Build all metadata caches for the connection. If a ' .
103: 'table name is provided, only that table will be cached.',
104: ])->addOption('connection', [
105: 'help' => 'The connection to build/clear metadata cache data for.',
106: 'short' => 'c',
107: 'default' => 'default',
108: ])->addArgument('name', [
109: 'help' => 'A specific table you want to clear/refresh cached data for.',
110: 'optional' => true,
111: ]);
112:
113: return $parser;
114: }
115: }
116: