_schema = $this->getSchema($connection); } /** * Build metadata. * * @param string|null $name The name of the table to build cache data for. * @return array Returns a list build table caches */ public function build(?string $name = null): array { if ($name) { $tables = [$name]; } else { $tables = $this->_schema->listTables(); } foreach ($tables as $table) { $this->_schema->describe($table, ['forceRefresh' => true]); } return $tables; } /** * Clear metadata. * * @param string|null $name The name of the table to clear cache data for. * @return array Returns a list of cleared table caches */ public function clear(?string $name = null): array { if ($name) { $tables = [$name]; } else { $tables = $this->_schema->listTables(); } $cacher = $this->_schema->getCacher(); foreach ($tables as $table) { $key = $this->_schema->cacheKey($table); $cacher->delete($key); } return $tables; } /** * Helper method to get the schema collection. * * @param \Cake\Database\Connection $connection Connection object * @return \Cake\Database\Schema\CachedCollection * @throws \RuntimeException If given connection object is not compatible with schema caching */ public function getSchema(Connection $connection): CachedCollection { $config = $connection->config(); if (empty($config['cacheMetadata'])) { $connection->cacheMetadata(true); } /** @var \Cake\Database\Schema\CachedCollection $schemaCollection */ $schemaCollection = $connection->getSchemaCollection(); return $schemaCollection; } }