<?php
declare(strict_types=1);
$lane=getenv('FOOD_LANE');$root=getenv('FOOD_ROOT');if(!in_array($lane,['B','C'],true)||__DIR__!=='/tmp/cdx-store-read7-fast-20261011')throw new RuntimeException('Exact scope before PDO');
$plan=json_decode(file_get_contents(__DIR__.'/plan.json'),true);if(($plan['approval_status']??'')!=='ROOT_APPROVED_READ7_FRESH_THEN_CONDITIONAL_HTTP'||$plan['ports']!==['B'=>19051,'C'=>19052])throw new RuntimeException('Concrete primary phase approval');
$map=json_decode(file_get_contents(__DIR__.'/source-lock.json'),true);if(hash_file('sha256',__DIR__.'/source-lock.json')!=='f26f6dd15e86f3b34e23cde8413b660683cc5656f542b85973581c441c747738')throw new RuntimeException('Root mapping');$actual=[];$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__.'/source/application',FilesystemIterator::SKIP_DOTS));foreach($it as $f){if($f->isLink())throw new RuntimeException('No source symlink');if($f->isFile()){$rel=substr($f->getPathname(),strlen(__DIR__.'/source/application/'));if(strpos(basename($rel),'._')===0)throw new RuntimeException('AppleDouble');$actual[$rel]=hash_file('sha256',$f->getPathname());}}ksort($actual);ksort($map);if(count($actual)!==959||$actual!==$map)throw new RuntimeException('APP959 exact');
defined('TP_ENV')||define('TP_ENV','local');defined('BUSINESS')||define('BUSINESS','');defined('APP_PATH')||define('APP_PATH',__DIR__.'/source/application/');defined('ROOT_PATH')||define('ROOT_PATH',$root.'/');$cfg=(static function($f){return require $f;})($root.'/application/database.php');$ai=$cfg;foreach(['hostname','hostport','database','username','password','prefix']as $k)$ai[$k]=$cfg['ai_zhct'][$k]??$cfg[$k];if($cfg['prefix']!=='ydy_'||$ai['prefix']!=='yoshop_')throw new RuntimeException('Dual prefixes');
if($lane==='B'){$local=(static function($f){return require $f;})('/workspace/zhct/zhctproject/store/application/config/local/database.php');foreach(['hostname','database','username','password']as $k)if($cfg[$k]!==$local[$k]||$ai[$k]!==$local['ai_zhct'][$k])throw new RuntimeException('Exact code-local pair');}else foreach([[$cfg,5237,'ZHCT_APP'],[$ai,5238,'AIZHCT_APP']]as $c)if($c[0]['hostname']!=='127.0.0.1'||(int)$c[0]['hostport']!==$c[1]||$c[0]['database']!==$c[2]||$c[0]['username']!==$c[2])throw new RuntimeException('Exact loopback DM pair');
$options=[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_CASE=>PDO::CASE_LOWER,PDO::ATTR_ORACLE_NULLS=>PDO::NULL_NATURAL,PDO::ATTR_STRINGIFY_FETCHES=>false,PDO::ATTR_EMULATE_PREPARES=>false];$dsn=static function($c)use($lane){return $lane==='B'?'mysql:host='.$c['hostname'].';port='.($c['hostport']?:3306).';dbname='.$c['database'].';charset='.$c['charset']:'dm:host=127.0.0.1;port='.$c['hostport'].';schema='.$c['database'];};
require_once __DIR__.'/Gate.php';Read7Gate::$lane=$lane;Read7Gate::$phase='preflight';$GLOBALS['READ7_OBSERVER']=[];
$save=static function($name,$value){$f=__DIR__.'/private/'.$name;$h=fopen($f,'xb');if(!$h)throw new RuntimeException('No raw overwrite');$s=json_encode($value,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION);if($s===false)throw new RuntimeException('Native JSON finite');fwrite($h,$s);fclose($h);chmod($f,0600);};$GLOBALS['save']=$save;
$options[PDO::ATTR_STATEMENT_CLASS]=['Read7Statement',['store']];$db=new Read7PDO($dsn($cfg),$cfg['username'],$cfg['password'],$options);$db->role='store';if($lane==='B')$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,false);
$q=static function($sql,$bind=[],$digest=false)use($db){$s=$db->prepare($sql);foreach($bind as $i=>$v)$s->bindValue($i+1,$v,is_int($v)?1:($v===null?0:2));$s->execute();$rows=[];$hash=hash_init('sha256');$count=0;while(($r=$s->fetch(PDO::FETCH_ASSOC))!==false){foreach($r as &$v)if(is_resource($v))$v=stream_get_contents($v);unset($v);$r=array_change_key_case($r,CASE_LOWER);hash_update($hash,serialize($r));$count++;if(!$digest)$rows[]=$r;}$out=$digest?['rows'=>$count,'typed_sha256'=>hash_final($hash)]:$rows;$e=['SQL'=>$s->queryString,'binds'=>$bind,'result'=>$out,'rowCount'=>$s->rowCount(),'errorInfo'=>$s->errorInfo()];$GLOBALS['READ7_OBSERVER'][]=$e;Read7Gate::append(['event'=>'original-observer-result','record'=>$e]);return $out;};
