diff --git a/src/implementations/autoloading/RemoteAutoloader.php b/src/implementations/autoloading/RemoteAutoloader.php index 2f6ddf8..ac741ce 100644 --- a/src/implementations/autoloading/RemoteAutoloader.php +++ b/src/implementations/autoloading/RemoteAutoloader.php @@ -208,646 +208,12 @@ public function str_parse_vars($string,$start = '[',$end = '/]', $variableDelimi return $out; } - /* -print_r(g('Sample text, [/text to extract/] Rest of sample text [/WEB=http://google.com/] bla bla bla. ','[/','/]')); -?> -results: -Array -( - [0] => text to extract - [WEB] => Array - ( - [0] => http://google.com - ) - -) -*/ - public function getUrl($class, $server, $salt = null, $parseVars = false){ - if(!is_string($salt))$salt=mt_rand(1000,9999); - $url = false; - - // if(is_string($server) && '\\' === substr($server, -1) ){ - // return $this->loadClass( $server.ltrim(str_replace($server, '', $class), '/\\') ); - // } - - if(is_string($server) ){ - if(substr($server, 0, strlen('http://')) === 'http://' || substr($server, 0, strlen('https://')) === 'https://'){ - // $url = str_replace(['${salt}', '${class}', '${version}'], [$salt, $class, $this->version], $server); - $url = $server; - }elseif('~' === substr($server, 0, 1) || is_string($server) && '.' === substr($server, 0, 1) || substr($server, 0, strlen('file://')) === 'file://'){ - $url = \DIRECTORY_SEPARATOR.str_replace('\\', \DIRECTORY_SEPARATOR, - getcwd() .str_replace(['file://', '~'/*, '${salt}', '${class}', '${version}'*/], - ['', (!empty(getenv('FRDL_HOME'))) ? getenv('FRDL_HOME') : getenv('HOME')/*, $salt, $class, $this->version*/], - $server). '.php'); - }elseif(preg_match("/^([a-z0-9]+)\.webfan\.de$/", $server, $m) && false !== strpos($server, '.') ){ - $url = 'https://'.$m[1].'.webfan.de/install/?salt=${salt}&source=${class}&version=${version}'; - }elseif(preg_match("/^([\w\.^\/]+)(\/[.*]+)?$/", $server, $m) && false !== strpos($server, '.') ){ - $url = 'https://'.$m[1].((isset($m[2])) ? $m[2] : '/'); - }//else{ - // $url = 'https://'.$server.'/install/?salt='.$salt.'&source='. $class.'&version='.$this->version; - - //$url = 'https://'.$server.'/install/?salt=${salt}&source=${class}&version=${version}'; - // } - if(!$this->str_contains($url, '${class}', false) && '.php' !== substr(explode('?', $url)[0], -4)){ - $url = rtrim($url, '/').'/${class}'; - } - - if(!$this->str_contains($url, '${salt}', false)){ - $url .= (( $this->str_contains($url, '?', false) ) ? '&' : '?').'salt=${salt}'; - } - - }elseif(is_callable($server)){ - $url = call_user_func_array($server, [$class, $this->version, $salt]); - }elseif(is_object($server) && is_callable([$server, 'has']) && is_callable([$server, 'get']) && true === call_user_func_array([$server, 'has'], [$class]) ){ - $url = call_user_func_array([$server, 'get'], [$class, $this->version, $salt]); - }elseif(is_object($server) && is_callable([$server, 'get']) ){ - $url = call_user_func_array([$server, 'get'], [$class, $this->version, $salt]); - } - return (true === $parseVars && is_string($url)) ? $this->replaceUrlVars($url, $salt, $class, $version) : $url; - } - - - - public function replaceUrlVars($url, $salt, $class, $version){ - return str_replace(['${salt}', '${class}', '${version}'], [$salt, $class, $version], $url); - } - - - /** - * Loads the class file for a given class name. - * - * @param string $class The fully-qualified class name. - * @return mixed The mapped file name on success, or boolean false on - * failure. - */ - public function loadClass($class, $salt = null) - { - - // the current namespace prefix - $prefix = $class; - - - // work backwards through the namespace names of the fully-qualified - // class name to find a mapped file name - while (false !== $pos = strrpos($prefix, '\\')) { - - - // retain the trailing namespace separator in the prefix - $prefix = substr($class, 0, $pos + 1); - - // the rest is the relative class name - $relative_class = substr($class, $pos + 1); - - - // try to load a mapped file for the prefix and relative class - $mapped_file = $this->loadMappedSource($prefix, $relative_class, $salt); - if ($mapped_file) { - return $mapped_file; - } - - // remove the trailing namespace separator for the next iteration - // of strrpos() - $prefix = rtrim($prefix, '\\'); - } - - - // never found a mapped file - return $this->loadMappedSource('', $class, $salt); - } - - /** - * Load the mapped file for a namespace prefix and relative class. - * - * @param string $prefix The namespace prefix. - * @param string $relative_class The relative class name. - * @return mixed Boolean false if no mapped file can be loaded, or the - * name of the mapped file that was loaded. - */ - protected function loadMappedSource($prefix, $relative_class, $salt = null) - { - - $url = false; - $class = $prefix.$relative_class; - - //if(isset($this->alias[$class]) ){ - // \webfan\hps\Format\DataUri - // die(__LINE__.$class.' Alias: '.$this->alias[$class]); - // } - $pfx = !isset($this->alias[$prefix]) && substr($prefix,-1) === '\\' ? substr($prefix, 0, -1) : $prefix; - - - - if(isset($this->alias[$pfx]) ){ - // \webfan\hps\Format\DataUri - $originalClass = substr($this->alias[$pfx],-1) === '\\' ? substr($this->alias[$pfx], 0, -1) : $this->alias[$pfx]; - $originalClass .= '\\'.$relative_class; - $alias = $class; - - // die($classOrInterfaceExists.'
'.$alias.'
rc: '.$originalClass.'
'.$datUri); - $classOrInterfaceExistsAndNotEqualsAlias =( - class_exists($originalClass, $originalClass !== $alias) - || interface_exists($originalClass, $originalClass !== $alias) - || (function_exists('trait_exists') && trait_exists($originalClass, $originalClass !== $alias)) - ) && $originalClass !== $alias; - - - if($classOrInterfaceExistsAndNotEqualsAlias){ - \class_alias($originalClass, $alias); - } - - return true; - //return $classOrInterfaceExistsAndNotEqualsAlias; - } - - - if(isset(self::$classmap[$class]) && is_string(self::$classmap[$class]) && '\\' !== substr($class, -1) && '\\' !== substr(self::$classmap[$class], -1) ){ - return $this->getUrl($class, self::$classmap[$class], $salt); - // return self::$classmap[$class]; - } - - if (isset($this->prefixes[$prefix]) ) { - // look through base directories for this namespace prefix - foreach ($this->prefixes[$prefix] as $server) { - - $url = $this->getUrl($relative_class, $server, $salt); - - if(is_string($url) - && $this->exists($url) - ){ - return $url; - } - } - } - // never found it - return $this->getUrl($class, $this->server, $salt); - } - - /** - * If a file exists, require it from the file system. - * - * @param string $file The file to require. - * @return bool True if the file exists, false if not. - */ - protected function requireFile($file){ - if (file_exists($file)) { - try{ - require $file; - }catch(\Exception $e){ - trigger_error($e->getMessage(), \E_USER_ERROR); - return false; - } - return true; - } - return false; - } - public function withClassmap(array $classMap = null){ - if(null !== $classMap){ - foreach($classMap as $class => $server){ - if('@' === substr($class, 0, 1) && is_string($server)){ - $this->withAlias($class, $server); - }elseif('\\' === substr($class, -1)){ - $this->withNamespace($class, $server, is_string($server)); - }else{ - self::$classmap[$class] = $server; - } - - } - } - - return self::$classmap; - } - public function withAlias(string $alias, string $rewrite){ - $this->alias[ltrim($alias, '@')] = $rewrite; - } - - public function withSalt(bool $salted = null){ - if(null !== $salted){ - $this->salted = $salted; - } - - return $this->salted; - } - - - public static function getInstance($server = 'frdl.webfan.de', $register = false, $version = 'latest', $allowFromSelfOrigin = false, $salted = false, - $classMap = null, $cacheDir = null, $cacheLimit = null){ - if(is_array($server)){ - // $arr = []; - foreach($server as $s){ - // $arr[]= - self::getInstance($s['server'], $s['register'], $s['version'], $s['allowFromSelfOrigin'], $s['salted'], $s['classmap'], $s['cacheDir'], $s['cacheLimit']); - } - - if(2 > count(func_get_args()) ){ - return self::$instances[count(self::$instances)-1]; - } - - $server = 'file://'.getcwd().\DIRECTORY_SEPARATOR; - $key = sha1(getcwd()).'.localhost'; - }elseif(is_callable($server)){ - $key = \spl_object_id($server); - }elseif(is_string($server)){ - $key = $server; - } - - if(!isset(self::$instances[$key])){ - self::$instances[$key] = new self($server, $register, $version, $allowFromSelfOrigin, $salted, $classMap, $cacheDir, $cacheLimit); - } - - return self::$instances[$key]; - } - - public static function __callStatic($name, $arguments){ - $me = (count(self::$instances)) ? self::$instances[0] : self::getInstance(); - return call_user_func_array([$me, $name], $arguments); - } - - public function __call($name, $arguments){ - if(!in_array($name, ['fetch', 'fetchCode', '__invoke', 'register', 'getLoader', 'Autoload'])){ - throw new \Exception('Method '.$name.' not allowed in '.__METHOD__); - } - return call_user_func_array([$this, $name], $arguments); - } - - protected function fetch(){ - return call_user_func_array([$this, 'fetchCode'], func_get_args()); - } - - public function exists($source){ - if(is_file($source) && file_exists($source) && is_readable($source)){ - return true; - } - - $options = [ - 'https' => [ - 'method' => 'HEAD', - 'ignore_errors' => true, - - ] - ]; - $context = stream_context_create($options); - $res = @file_get_contents($source, false, $context); - return false !== $res; - } - - protected function fetchCode($class, $salt = null){ - /* - $server = (isset(self::$classmap[$class])) - ? self::$classmap[$class] - : $this->server; - */ - if(!is_string($salt) - //&& true === $this->withSalt() - ){ - $salt = mt_rand(10000000,99999999); - } - - - $url = $this->loadClass($class, $salt); - - if(is_bool($url)){ - return $url; - } - - $withSaltedUrl = (true === $this->str_contains($url, '${salt}', false)) ? true : false; - $url = $this->replaceUrlVars($url, $salt, $class, $this->version); - - - $options = [ - 'https' => [ - 'method' => 'GET', - 'ignore_errors' => true, - - ] - ]; - $context = stream_context_create($options); - $code = @file_get_contents($url, false, $context); - //$code = file_get_contents($url); - foreach($http_response_header as $i => $header){ - $h = explode(':', $header); - if('x-content-hash' === strtolower(trim($h[0]))){ - $hash = trim($h[1]); - } - if('x-user-hash' === strtolower(trim($h[0]))){ - $userHash = trim($h[1]); - } - } - - - - if(false===$code - || !is_string($code) - || (true === $withSaltedUrl && true === $this->withSalt() && (!isset($hash) || !isset($userHash))) - - ){ - throw new \Exception('Missing checksums while fetching source code for '.$class.' from '.$url); - return false; - } - - - $oCode =$code; - - if(is_string($salt) && true === $withSaltedUrl && true === $this->withSalt() ){ - $hash_check = strlen($oCode).'.'.sha1($oCode); - $userHash_check = sha1($salt .$hash_check); - - if($hash_check !== $hash || $userHash_check !== $userHash){ - throw new \Exception('Invalid checksums while fetching source code for '.$class.' from '.$url); - } - } - - $code = trim($code); - - if(!$this->str_contains($code, ' '); - $codeWithStartTags = "getLoader(), func_get_args()); - } - - protected function register($throw = true, $prepend = false){ - - $res = false; - - - if(!$this->allowFromSelfOrigin && $this->domain === $this->selfDomain){ - throw new \Exception('You should not autoload from remote where you have local access to the source (remote server = host)'); - } - - $aFuncs = \spl_autoload_functions(); - if(!is_array($aFuncs) || !in_array($this->getLoader(), $aFuncs) ){ - $res = spl_autoload_register($this->getLoader(), $throw, $prepend); - } - - - if( true === $res ){ - $this->pruneCache(); - }else{ - throw new \Exception(sprintf('Cannot register Autoloader of "%s" with cachedir "%s"', __METHOD__, $this->cacheDir)); - } - - - - return $res; - } - - protected function getLoader(){ - return [$this, 'Autoload']; - } - - - public function pruneCache(){ - $valCacheDir = function($CacheDir){ - return is_dir($CacheDir) - && is_writable($CacheDir) - && is_readable($CacheDir) - && rtrim($CacheDir, \DIRECTORY_SEPARATOR.'/\\ ') !== rtrim(\sys_get_temp_dir(), \DIRECTORY_SEPARATOR.'/\\ ') - && 'tmp' !== basename($CacheDir) - && 'tmp' !== basename(dirname($CacheDir)) - ? true - : false - ; - }; - - - if( true === $valCacheDir($this->cacheDir) ){ - - $ShutdownTasks = \frdlweb\Thread\ShutdownTasks::mutex(); - $ShutdownTasks(function($CacheDir, $maxCacheTime, $valCacheDir){ - if(true === $valCacheDir($CacheDir)){ - \webfan\hps\patch\Fs::pruneDir($CacheDir, $maxCacheTime, true, 'psr4' === basename($CacheDir) && 'tmp' !== basename($CacheDir)); - } - }, $this->cacheDir, $this->cacheLimit, $valCacheDir); - }else{ - throw new \Exception(sprintf('Cannot register of %s ("%s") with cachedir "%s"', __FUNCTION__, __METHOD__, $this->cacheDir)); - } - } - - -class RemoteAutoloader -{ - const CLASSMAP_DEFAULTS = [ - \Wehowski\Gist\Http\Response\Helper::class => 'https://gist.githubusercontent.com/wehowski/d762cc34d5aa2b388f3ebbfe7c87d822/raw/5c3acdab92e9c149082caee3714f0cf6a7a9fe0b/Wehowski%255CGist%255CHttp%255CResponse%255CHelper.php?cache_bust=${salt}', - \webfan\hps\Format\DataUri::class => 'https://frdl.webfan.de/install/?salt=${salt}&source=webfan\hps\Format\DataUri', - 'frdl\\Proxy\\' => 'https://raw.githubusercontent.com/frdl/proxy/master/src/${class}.php?cache_bust=${salt}', - \frdlweb\Thread\ShutdownTasks::class => 'https://raw.githubusercontent.com/frdl/shutdown-helper/master/src/ShutdownTasks.php', - - // NAMESPACES - // Zend Framework components - '@Zend\\AuraDi\\Config' => 'Laminas\\AuraDi\\Config', - '@Zend\\Authentication' => 'Laminas\\Authentication', - '@Zend\\Barcode' => 'Laminas\\Barcode', - '@Zend\\Cache' => 'Laminas\\Cache', - '@Zend\\Captcha' => 'Laminas\\Captcha', - '@Zend\\Code' => 'Laminas\\Code', - '@ZendCodingStandard\\Sniffs' => 'LaminasCodingStandard\\Sniffs', - '@ZendCodingStandard\\Utils' => 'LaminasCodingStandard\\Utils', - '@Zend\\ComponentInstaller' => 'Laminas\\ComponentInstaller', - '@Zend\\Config' => 'Laminas\\Config', - '@Zend\\ConfigAggregator' => 'Laminas\\ConfigAggregator', - '@Zend\\ConfigAggregatorModuleManager' => 'Laminas\\ConfigAggregatorModuleManager', - '@Zend\\ConfigAggregatorParameters' => 'Laminas\\ConfigAggregatorParameters', - '@Zend\\Console' => 'Laminas\\Console', - '@Zend\\ContainerConfigTest' => 'Laminas\\ContainerConfigTest', - '@Zend\\Crypt' => 'Laminas\\Crypt', - '@Zend\\Db' => 'Laminas\\Db', - '@ZendDeveloperTools' => 'Laminas\\DeveloperTools', - '@Zend\\Di' => 'Laminas\\Di', - '@Zend\\Diactoros' => 'Laminas\\Diactoros', - '@ZendDiagnostics\\Check' => 'Laminas\\Diagnostics\\Check', - '@ZendDiagnostics\\Result' => 'Laminas\\Diagnostics\\Result', - '@ZendDiagnostics\\Runner' => 'Laminas\\Diagnostics\\Runner', - '@Zend\\Dom' => 'Laminas\\Dom', - '@Zend\\Escaper' => 'Laminas\\Escaper', - '@Zend\\EventManager' => 'Laminas\\EventManager', - '@Zend\\Feed' => 'Laminas\\Feed', - '@Zend\\File' => 'Laminas\\File', - '@Zend\\Filter' => 'Laminas\\Filter', - '@Zend\\Form' => 'Laminas\\Form', - '@Zend\\Http' => 'Laminas\\Http', - '@Zend\\HttpHandlerRunner' => 'Laminas\\HttpHandlerRunner', - '@Zend\\Hydrator' => 'Laminas\\Hydrator', - '@Zend\\I18n' => 'Laminas\\I18n', - '@Zend\\InputFilter' => 'Laminas\\InputFilter', - '@Zend\\Json' => 'Laminas\\Json', - '@Zend\\Ldap' => 'Laminas\\Ldap', - '@Zend\\Loader' => 'Laminas\\Loader', - '@Zend\\Log' => 'Laminas\\Log', - '@Zend\\Mail' => 'Laminas\\Mail', - '@Zend\\Math' => 'Laminas\\Math', - '@Zend\\Memory' => 'Laminas\\Memory', - '@Zend\\Mime' => 'Laminas\\Mime', - '@Zend\\ModuleManager' => 'Laminas\\ModuleManager', - '@Zend\\Mvc' => 'Laminas\\Mvc', - '@Zend\\Navigation' => 'Laminas\\Navigation', - '@Zend\\Paginator' => 'Laminas\\Paginator', - '@Zend\\Permissions' => 'Laminas\\Permissions', - '@Zend\\Pimple\\Config' => 'Laminas\\Pimple\\Config', - '@Zend\\ProblemDetails' => 'Mezzio\\ProblemDetails', - '@Zend\\ProgressBar' => 'Laminas\\ProgressBar', - '@Zend\\Psr7Bridge' => 'Laminas\\Psr7Bridge', - '@Zend\\Router' => 'Laminas\\Router', - '@Zend\\Serializer' => 'Laminas\\Serializer', - '@Zend\\Server' => 'Laminas\\Server', - '@Zend\\ServiceManager' => 'Laminas\\ServiceManager', - '@ZendService\\ReCaptcha' => 'Laminas\\ReCaptcha', - '@ZendService\\Twitter' => 'Laminas\\Twitter', - '@Zend\\Session' => 'Laminas\\Session', - '@Zend\\SkeletonInstaller' => 'Laminas\\SkeletonInstaller', - '@Zend\\Soap' => 'Laminas\\Soap', - '@Zend\\Stdlib' => 'Laminas\\Stdlib', - '@Zend\\Stratigility' => 'Laminas\\Stratigility', - '@Zend\\Tag' => 'Laminas\\Tag', - '@Zend\\Test' => 'Laminas\\Test', - '@Zend\\Text' => 'Laminas\\Text', - '@Zend\\Uri' => 'Laminas\\Uri', - '@Zend\\Validator' => 'Laminas\\Validator', - '@Zend\\View' => 'Laminas\\View', - '@ZendXml' => 'Laminas\\Xml', - '@Zend\\Xml2Json' => 'Laminas\\Xml2Json', - '@Zend\\XmlRpc' => 'Laminas\\XmlRpc', - '@ZendOAuth' => 'Laminas\\OAuth', - ]; - - - protected $salted = false; - - protected $selfDomain; - protected $server; - protected $domain; - protected $version; - protected $allowFromSelfOrigin = false; - - protected $prefixes = []; - protected $cacheDir; - protected $cacheLimit = 0; - protected static $instances = []; - protected $alias = []; - protected static $classmap = []; - - - - public function __construct($server = 'frdl.webfan.de', $register = true, $version = 'latest', $allowFromSelfOrigin = false, $salted = false, $classMap = null, $cacheDir = null, $cacheLimit = null){ - - if(null === $classMap || true === $classMap){ - $classMap = self::CLASSMAP_DEFAULTS; - } - - $this->withSalt($salted); - $this->withClassmap($classMap); - $this->allowFromSelfOrigin = $allowFromSelfOrigin; - $this->version=$version; - $this->server = $server; - $_self = (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']; - $h = explode('.', $_self); - $dns = array_reverse($h); - $this->selfDomain = $dns[1].'.'.$dns[0]; - - $h = explode('.', $this->server); - $dns = array_reverse($h); - $this->domain = $dns[1].'.'.$dns[0]; - - - if(!$this->allowFromSelfOrigin && $this->domain === $this->selfDomain){ - $register = false; - } - - $this->cacheLimit = (is_int($cacheLimit)) ? $cacheLimit : ((isset($_ENV['FRDL_HPS_PSR4_CACHE_LIMIT']))? $_ENV['FRDL_HPS_PSR4_CACHE_LIMIT'] : 31 * 24 * 60 * 60); - $this->cacheDir = (is_string($cacheDir)) ? $cacheDir - : ((isset($_ENV['FRDL_HPS_PSR4_CACHE_DIR'])) ? $_ENV['FRDL_HPS_PSR4_CACHE_DIR'] : - \sys_get_temp_dir() . \DIRECTORY_SEPARATOR. \get_current_user () . \DIRECTORY_SEPARATOR. 'psr4'. \DIRECTORY_SEPARATOR - ); - - - if(true === $register){ - $this->register(); - } - } - - public function withNamespace($prefix, $server, $prepend = false) - { - // normalize namespace prefix - $prefix = trim($prefix, '\\') . '\\'; - - // normalize the base directory with a trailing separator - // $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; - - // initialize the namespace prefix array - if (isset($this->prefixes[$prefix]) === false) { - $this->prefixes[$prefix] = []; - } - - // retain the base directory for the namespace prefix - if ($prepend) { - array_unshift($this->prefixes[$prefix], $server); - } else { - array_push($this->prefixes[$prefix], $server); - } - } - - - public function str_contains($haystack, $needle, $ignoreCase = false) { - if ($ignoreCase) { - $haystack = strtolower($haystack); - $needle = strtolower($needle); - } - $needlePos = strpos($haystack, $needle); - return ($needlePos === false ? false : ($needlePos+1)); - } - - - public function str_parse_vars($string,$start = '[',$end = '/]', $variableDelimiter = '='){ - preg_match_all('/' . preg_quote($start, '/') . '(.*?)'. preg_quote($end, '/').'/i', $string, $m); - $out = []; - foreach($m[1] as $key => $value){ - $type = explode($variableDelimiter,$value); - if(sizeof($type)>1){ - if(!is_array($out[$type[0]])) - $out[$type[0]] = []; - $out[$type[0]][] = $type[1]; - } else { - $out[] = $value; - } - } - - return $out; - } - /* -print_r(g('Sample text, [/text to extract/] Rest of sample text [/WEB=http://google.com/] bla bla bla. ','[/','/]')); -?> - -results: -Array -( - [0] => text to extract - [WEB] => Array - ( - [0] => http://google.com - ) - -) -*/ public function getUrl($class, $server, $salt = null, $parseVars = false){ if(!is_string($salt))$salt=mt_rand(1000,9999); $url = false; - // if(is_string($server) && '\\' === substr($server, -1) ){ - // return $this->loadClass( $server.ltrim(str_replace($server, '', $class), '/\\') ); - // } + if(is_string($server) ){ if(substr($server, 0, strlen('http://')) === 'http://' || substr($server, 0, strlen('https://')) === 'https://'){ @@ -1208,7 +574,7 @@ protected function register($throw = true, $prepend = false){ $aFuncs = \spl_autoload_functions(); if(!is_array($aFuncs) || !in_array($this->getLoader(), $aFuncs) ){ - $res = spl_autoload_register($this->getLoader(), $throw, $prepend); + $res = \spl_autoload_register($this->getLoader(), $throw, $prepend); } @@ -1301,95 +667,6 @@ public function Autoload($class){ - if(file_exists($cacheFile) ){ - if(false === ($this->requireFile($cacheFile)) ){ - if(file_exists($cacheFile)){ - unlink($cacheFile); - } - return false; - } - //return true; - return class_exists($class, false); - }elseif(isset($code) && is_string($code)){ - /* - $code =ltrim($code, ''); - eval($code); - */ - //$tmpfile = tmpfile(); - $tmpfile = tempnam($this->cacheDir, 'autoloaded-file.'.sha1($code)); - - - $ShutdownTasks = \frdlweb\Thread\ShutdownTasks::mutex(); - $ShutdownTasks(function($tmpfile){ - if(file_exists($tmpfile)){ - unlink($tmpfile); - } - }, $tmpfile); - - - if(false === ($this->requireFile($tmpfile)) ){ - if(file_exists($tmpfile)){ - unlink($tmpfile); - } - return false; - }else{ - unlink($tmpfile); - return class_exists($class, false); - } - }else{ - throw new \Exception('Cannot write/load source for class '.$class.' in '.$cacheFile); - } - - } - -} - public function Autoload($class){ - - $cacheFile = rtrim($this->cacheDir, \DIRECTORY_SEPARATOR.'/\\ '). \DIRECTORY_SEPARATOR. str_replace('\\', \DIRECTORY_SEPARATOR, $class). '.php'; - //$cacheFile = realpath($cacheFile); - - if(file_exists($cacheFile) - && ($this->cacheLimit !== 0 - && $this->cacheLimit !== -1 - && (filemtime($cacheFile) < time() - $this->cacheLimit) - )){ - unlink($cacheFile); - clearstatcache(true, $cacheFile); - } - - - if(!file_exists($cacheFile) - || ($this->cacheLimit === 0 - || $this->cacheLimit === -1 - || (filemtime($cacheFile) < time() - $this->cacheLimit) - ) - ){ - - - - $code = $this->fetchCode($class, null); - if(true === $code){ - return true; - }elseif(false !==$code){ - if(!is_dir(dirname($cacheFile))){ - mkdir(dirname($cacheFile), 0755, true); - } - - if(!file_put_contents($cacheFile, $code)){ - throw new \Exception('Cannot write source for class '.$class.' to '.$cacheFile); - } - - }elseif(false ===$code && !file_exists($cacheFile)){ - // die($cacheFile); - return false; - } - - } - - - - if(file_exists($cacheFile) ){ if(false === ($this->requireFile($cacheFile)) ){ if(file_exists($cacheFile)){