get('tokens') === null || $store->get('layouts') === null; } /** Il database ha contenuti Nucleo: documenti (_nucleo_doc), layout pubblicati o un modello dei contenuti. */ public static function hasData(): bool { if (is_array(get_option(ModelRepository::OPTION, null))) { return true; } global $wpdb; $found = $wpdb->get_var( "SELECT p.ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = '_nucleo_doc' LIMIT 1" ); return $found !== null; } /** Prende il lock; se è scaduto (più vecchio di LOCK_TTL) lo riprende una volta. */ private static function acquireLock(): bool { if (add_option(self::LOCK_OPTION, time(), '', false)) { return true; } $since = (int) get_option(self::LOCK_OPTION, 0); if (time() - $since <= self::LOCK_TTL) { return false; } delete_option(self::LOCK_OPTION); return (bool) add_option(self::LOCK_OPTION, time(), '', false); } /** File degli artefatti nella cartella base: spostati nella generazione se il database ha dati, altrimenti cancellati. */ private static function adoptBase(string $base, string $dir): void { $keep = self::hasData(); foreach (glob(rtrim($base, '/\\') . '/*.php') ?: [] as $file) { if (preg_match(self::FILE_PATTERN, basename($file)) !== 1) { continue; } self::forget($file); if (!$keep) { Filesystem::direct()->delete($file); continue; } $target = $dir . '/' . basename($file); if (!is_file($target)) { Filesystem::direct()->move($file, $target); } } } /** Cancella una generazione vecchia con i suoi file. */ private static function removeDir(string $dir): void { foreach (glob($dir . '/*.php') ?: [] as $file) { self::forget($file); } Filesystem::direct()->delete($dir, true); } /** Invalida la cache opcode prima di spostare o cancellare un artefatto. */ private static function forget(string $file): void { if (function_exists('opcache_invalidate')) { opcache_invalidate($file, true); } } }