20 lines
2.9 KiB
PHP
20 lines
2.9 KiB
PHP
<?php
|
|
ini_set('zend.exception_ignore_args','1');
|
|
set_exception_handler(function(Throwable $e){fwrite(STDERR,'Data cache verification stopped: '.$e::class.' line '.$e->getLine().PHP_EOL);exit(1);});
|
|
$root=dirname(__DIR__,2);$phase=$argv[1]??'';
|
|
if(!in_array($phase,['before','after'],true)||!is_file($root.'/storage/framework/down'))throw new RuntimeException('Maintenance snapshot required');
|
|
require $root.'/vendor/autoload.php';$app=require $root.'/bootstrap/app.php';(new Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables)->bootstrap($app);(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app);
|
|
$store=config('cache.default');$driver=config('cache.stores.'.$store.'.driver');$snapshot=['store'=>$store,'driver'=>$driver];
|
|
if($driver==='database'){
|
|
$connection=config('cache.stores.'.$store.'.connection')?:config('database.default');$c=config('database.connections.'.$connection);
|
|
if($c['driver']!=='pgsql'||$c['database']!=='investemoz'||(int)$c['port']!==5432||!in_array($c['host'],['127.0.0.1','localhost'])||!empty($c['url']))throw new RuntimeException('Expected local data cache connection');
|
|
$db=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$db->exec('BEGIN READ ONLY');
|
|
$table=config('cache.stores.'.$store.'.table');if(!preg_match('/^[a-z_][a-z0-9_]*$/D',$table))throw new RuntimeException('Cache table guard');
|
|
$rows=$db->query('SELECT row_to_json(t)::text FROM "'.$table.'" t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN);$snapshot['rows']=count($rows);$snapshot['sha256']=hash('sha256',implode("\n",$rows));$db->exec('ROLLBACK');
|
|
}elseif($driver==='file'){
|
|
$path=realpath(config('cache.stores.'.$store.'.path'));if(!$path||str_replace('\\','/',$path)!==$root.'/storage/framework/cache/data')throw new RuntimeException('Expected data cache path');$hashes=[];$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path,FilesystemIterator::SKIP_DOTS));foreach($it as$f)if($f->isFile())$hashes[str_replace('\\','/',substr($f->getPathname(),strlen($path)+1))]=hash_file('sha256',$f->getPathname());ksort($hashes);$snapshot['files']=$hashes;$snapshot['file_count']=count($hashes);
|
|
}else{throw new RuntimeException('Cache backend requires targeted reconciliation');}
|
|
$file=__DIR__.'/CACHE-DADOS-'.strtoupper($phase).'.json';if(is_file($file))throw new RuntimeException('Preserve cache snapshot');
|
|
if($phase==='after'){$before=json_decode(file_get_contents(__DIR__.'/CACHE-DADOS-BEFORE.json'),true,512,JSON_THROW_ON_ERROR);if($snapshot!==$before)throw new RuntimeException('Data cache changed; do not claim preservation');}
|
|
file_put_contents($file,json_encode($snapshot,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL);echo json_encode(['phase'=>$phase,'store'=>$store,'unchanged'=>$phase==='after','entries'=>$snapshot['rows']??$snapshot['file_count']]).PHP_EOL;
|