iDwAutoLoad->setDebug( $iDwLogger ); } private function __construct( iDwAutoLoad $iDwAutoLoad ) { try { $this->iDwAutoLoad = $iDwAutoLoad; $this->log =$this->iDwAutoLoad->getLogger( $this ); parent::__construct( $this ); /* //self::require_class_once( 'bakuba\kernel\extInc\log4php\LoggerAutoloader' ); //spl_autoload_register(array('bakuba\kernel\extInc\log4php\LoggerAutoloader', 'autoload')); self::require_class_once( 'bakuba\kernel\extInc\log4php\Logger' ); //self::require_class_once( "bakuba/kernel/DwObject" ); self::require_class_once( 'darkphp\logger\DwLogger' ); self::require_class_once( 'bakuba\kernel\agartha\DwBakubaRequest' ); self::require_class_once( 'bakuba\kernel\agartha\DwBakubaResponse' ); self::require_class_once( 'bakuba\kernel\process\DwProcessResponse' ); //*/ if ( $this->iDwAutoLoad->disableAutoload() ) { \spl_autoload_register( array( &$this, 'autoload' ) ,true ,true ); ///* if ( isset( $this->log ) && \is_object( $this->log ) ) $this->log->info( "register handler error_handle/exception_handler/fatal_handler" ); \set_error_handler( array( &$this ,'error_handle' ) ); \set_exception_handler( array( &$this ,'exception_handler' ) ); \register_shutdown_function( array( &$this ,"fatal_handler" ) ); //*/ } //} catch(\Exception | \Error $throwable) { } catch(\Exception $exception ) { $this->throwError( $exception ); throw $exception; } catch(\Error $error ) { $this->throwError( $error ); throw $error; } } private function _switchLogger( iDwLogger $logger ) : void { $this->setDebug( $logger ); } private function _getLogger( $forThisObject ) : iDwLogger { return $this->log->getLogger( $forThisObject ); } //static private $rootPid = null; public function _getFile( string $filePath ) : ?string { $dataFilecontent = null; //if (is_null( self::$rootPid ) ) // self::$rootPid = getmypid(); //echo "getFile:$filePath\r\n"; if ( isset( self::$sourcesFile[ $filePath ] ) ) $dataFilecontent = self::$sourcesFile[ $filePath ]; elseif ( \is_object( $this->iDwAutoLoad ) ) $dataFilecontent = $this->iDwAutoLoad->getFile( $filePath ); elseif ( defined("logByEcho") ) echo \PHP_EOL." ==No autoload found".\PHP_EOL; /* { if ( self::$rootPid == getmypid() ) $dataFilecontent = $this->iDwAutoLoad->getFile( $filePath ); else { $tStart = microtime( true ); $dataFilecontent = self::parentGetSource( $filePath ); $tStop = microtime( true ); $dbgMsg="time(".( round( $tStop - $tStart ,3 ) )." s) "; $dbgMsg.="size(".strlen( "".self::$sourcesFile[ $filePath ] ).")"; if ( is_string( $dataFilecontent ) ) self::$sourcesFile[ $filePath ] = $dataFilecontent; else self::$sourcesFile[ $filePath ] = null; } } */ return $dataFilecontent; } public function _scandir( string $folder ) : ?array { return $this->iDwAutoLoad->scandir( $folder ); } public function _isFile( string $path ) : bool { return $this->iDwAutoLoad->isFile( $path ); } public function _isDir( string $path ) : bool { return $this->iDwAutoLoad->isDir( $path ); } public function _getLogDir() : string { return $this->iDwAutoLoad->getLogDir(); } public function _register( string $namespace ,string $path ) : void { $this->iDwAutoLoad->register( $namespace ,$path ); } /** * Loads a class. * @param string $className The name of the class to load. */ public function autoload( string $className ) { try { $dbgMsg = "autoload:$className"; if ( isset( $this->log ) && \is_object( $this->log ) ) $this->log->info( $dbgMsg ); elseif ( defined("logByEcho") ) echo $dbgMsg.\PHP_EOL; self::include_class( $className ); //} catch(\Exception | \Error $throwable) { } catch(\Exception $exception ) { $this->throwError( $exception ); throw $exception; } catch(\Error $error ) { $this->throwError( $error ); throw $error; } } public function fatal_handler():void { //echo " fatal_handler".\PHP_EOL; $errfile = "unknown file"; $errstr = "shutdown"; $errno = \E_CORE_ERROR; $errline = 0; $error = \error_get_last(); if( $error !== NULL ) { $errcontext = \debug_backtrace( false ); $errcontext[] = DwObject::listInstancesCreated(); $this->error_handle( $error["type"] ,$error["message"] ,$error["file"] ,$error["line"] ,$errcontext ); } } // with Perplexity Production : private static $ignorePatterns = array( // '/msg_send\\(.+Resource temporarily unavailable/' '/socket_recvfrom\\(.+unable to recvfrom \\[10054\\]/' ); public function error_handle( int $errno,string $errstr ,string $errfile = "file?" ,int $errline = -1, array $errcontext = null ) : bool { $shouldIgnore=false; foreach (self::$ignorePatterns as $pattern) if ( ( $shouldIgnore|=preg_match($pattern, $errstr) ) == true ) break; // ignore this error if ( !$shouldIgnore ) { $fileContent = self::decodeFile( $errfile ); if ( \is_string( $fileContent ) ) $errObjStr = " object '".( DwSource::codeToNs( $fileContent ). "\\" . DwSource::codeToClass( $fileContent ) )."'"; if ( \darkphp\string\DwString::startWith( $errfile ,"data://" ) ) $errFileStr = "'data...'"; else $errFileStr = $errfile; $dbgMsg="=================== DwAutoLoadBoot->error_handle".\PHP_EOL; $dbgMsg.=" file:{$errFileStr} l:{$errline} [NoErr:{$errno}]".\PHP_EOL; $dbgMsg.=$errObjStr.\PHP_EOL.\PHP_EOL; $dbgMsg.=" msg:{$errstr}".\PHP_EOL; $dbgMsg.="=================== context".\PHP_EOL; $dbgMsg.=DwObject::print_r( $errcontext ).\PHP_EOL; $dbgMsg.="=================== END DwAutoLoadBoot->error_handle".\PHP_EOL; //$dbgMsg = "autoload:$className"; if ( isset( $this->log ) && \is_object( $this->log ) ) $this->log->fatal( \PHP_EOL.$dbgMsg ); else//if ( defined("logByEcho") ) echo \PHP_EOL.__CLASS__.__METHOD__.":!no log obj!".\PHP_EOL.$dbgMsg.\PHP_EOL; //} } return false; } public function exception_handler( $exception ) : void { $this->throwError( $exception ); } protected function throwError( $throwable ) { if ( isset( $this->log ) && \is_object( $this->log ) ) $this->log->fatal( DwException::exceptionToString( $throwable ) ); else \fwrite( \STDERR ,DwException::exceptionToString( $throwable ) ); /* \fwrite( \STDERR ,"[".__CLASS__.":".__METHOD__."(pid:".\getmypid().")]\r\n\t Exception autoload" ."\r\n\t Line:".$throwable->getLine() ."\r\n\t File:".$throwable->getFile() ."\r\n\t Message:\r\n\t\t".$throwable->getMessage()// ."\r\n\t Code:".$throwable->getCode() // ."\r\n\t Trace:".$throwable->getTrace()."\r\n\t" // ."\r\n\t Trace:".$throwable->getTraceAsString()."\r\n\t" // ."\r\n\t Previous:".$throwable->getPrevious()."\r\n\t" ."\r\n" ); //*/ } public function getClassName( bool $fqcn=null ) : string { return self::getStaticClassName( $fqcn ); } public static function getStaticClassName( bool $fqcn=null ) : string { return $fqcn ? parent::getStaticClassName( $fqcn ).self::CLASS_SEPARATOR.__CLASS__ : __CLASS__ ; //return __CLASS__; } static private $classIncluded = array(); public static function require_class_once( string $strFullQualifiedClassName ) { if ( ! isset( self::$classIncluded[ $strFullQualifiedClassName ] ) ) { $dwSource = self::getSource( $strFullQualifiedClassName ); if ( is_null( $dwSource ) ) { $dbgMsg = "no source for $strFullQualifiedClassName"; if ( isset( self::$instance->log ) && \is_object( self::$instance->log ) ) self::$instance->log->error( $dbgMsg ); elseif ( defined("logByEcho") ) echo $dbgMsg.\PHP_EOL; } else { require_once( "data://text/plain,".urlencode( $dwSource->getSource() ) ); self::$classIncluded[ $strFullQualifiedClassName ] = 1; } } } private static function include_class( string $strFullQualifiedClassName ) { if ( ! isset( self::$classIncluded[ $strFullQualifiedClassName ] ) ) { $dwSource=self::getSource( $strFullQualifiedClassName ); if ( \is_null( $dwSource ) ) { $dbgMsg="no source for '$strFullQualifiedClassName'."; if ( isset( self::$instance->log ) && \is_object( self::$instance->log ) ) self::$instance->log->error( $dbgMsg ); elseif ( defined("logByEcho") ) echo $dbgMsg.\PHP_EOL; } else { include( "data://text/plain,".\urlencode( $dwSource->getSource() ) ); self::$classIncluded[ $strFullQualifiedClassName ] = 1; } } } private static function classNameToFileName( string $strFullQualifiedClassName ) : string { return str_replace ( '\\', '/', $strFullQualifiedClassName ).".php"; } public static function classExist( string $strFullQualifiedClassName) : bool { $classExist = false; if ( isset( self::$sourcesCode[ $strFullQualifiedClassName ] ) ) $classExist = true; else $classExist = is_string( self::getFile( "file://".self::classNameToFileName( $strFullQualifiedClassName ) ) ); return $classExist; } // \darkphp\autoLoad\DwAutoLoad::decodeFile public static function decodeFile( $file ) : string { // \darkphp\autoload\DwAutoLoadBoot::decodeFile return DwAutoLoadBoot::decodeFile( $file ); } static private $rootPid = null; public static function getSource( string $strFullQualifiedClassName ) : ?DwSource { $dwSource = null; if ( isset( self::$sourcesCode[ $strFullQualifiedClassName ] ) ) $dwSource = self::$sourcesCode[ $strFullQualifiedClassName ]; else { $contentFile = self::getFile( "file://".DwSource::classNameToFileName( $strFullQualifiedClassName ) ); if ( \is_string( $contentFile ) ) { $dwSource = new DwSource( $strFullQualifiedClassName ,$contentFile ); self::$sourcesCode[ $strFullQualifiedClassName ] = $dwSource; }/* else { //print_r( debug_backtrace(false) ); $this->log->error( "no source file :{$strFullQualifiedClassName}" ); } //*/ } return $dwSource; } // \darkphp\autoLoad\DwAutoLoad::scandir( $folder ) public static function scandir( string $folder ) : ?array { return self::$instance->_scandir( $folder ); } // \darkphp\autoLoad\DwAutoLoad::isDir( $path ) public static function isDir( string $path ) : bool { return self::$instance->_isDir( $path ); } // \darkphp\autoLoad\DwAutoLoad::isFile( $path ) public static function isFile( string $path ) : bool { return self::$instance->_isFile( $path ); } // \darkphp\autoLoad\DwAutoLoad::getFile( $filePath ); public static function getFile( string $filePath ) : ?string { return self::$instance->_getFile( $filePath ); } // \darkphp\autoload\DwAutoLoad::getDarkPhpFolder(); public static function getDarkPhpFolder() : string { return \darkphp\autoload\DwAutoLoadBoot::getDarkPhpFolder(); } // \darkphp\autoLoad\DwAutoLoad::getLogDir() public static function getLogDir() : string { return self::$instance->_getLogDir(); } // \darkphp\autoLoad\DwAutoLoad::register( $namespace ,$path ); public static function register( string $namespace ,string $path ) : void { self::$instance->_register( $namespace ,$path ); } // \darkphp\autoLoad\DwAutoLoad::getLogger() public static function getLogger( $forThisObject ) : iDwLogger { return self::$instance->_getLogger( $forThisObject ); } // \darkphp\autoLoad\DwAutoLoad::switchLogger() public static function switchLogger( iDwLogger $logger ) : void { self::$instance->_switchLogger( $logger ); } /* public static function setLogger( iDwLogger $iDwLogger ) { self::$instance->setDebug( $iDwLogger ); } //*/ } ?>