12 private static function getAutoloader()
14 return DirectoriesAutoloader::instance(
DEFAULT_PUBDIR,
'.autoloader.cache');
18 return $autoloader->
addDirectory(
'./')->addCustomFilter(
'\Dcp\autoloaderIgnoreDotD');
23 public static function register()
25 require_once
'WHAT/classAutoloader.php';
26 require_once
'WHAT/classAutoloaderIgnoreDotD.php';
27 include_once
'WHAT/Lib.Prefix.php';
29 self::configure(self::getAutoloader())->register();
38 return self::getAutoloader()->classExists($pClassName);
45 require_once
'WHAT/Lib.Prefix.php';
46 require_once
'WHAT/classAutoloaderIgnoreDotD.php';
48 $autoloader = self::configure(self::getAutoloader());
50 if (is_file($cacheFile)) {
70 if (substr($this->current() , -1 * strlen($this->_ext)) === $this->_ext) {
71 return is_readable($this->current());
102 public function find($pFileName);
121 public function find($pFileName)
123 if (!defined(
"T_TRAIT")) define(
"T_TRAIT",
"T_TRAIT");
125 $tokens = token_get_all(file_get_contents($pFileName,
false));
126 $currentNamespace =
'';
127 $namespaceHunt =
false;
128 $validatedNamespaceHunt =
false;
130 $whitespaceCount = 0;
131 foreach ($tokens as $token) {
132 if (is_array($token)) {
133 if ($token[0] === T_INTERFACE || $token[0] === T_CLASS || $token[0] === T_TRAIT) {
136 } elseif ($token[0] === T_NAMESPACE) {
137 $namespaceHunt =
true;
140 if ($classHunt && $token[0] === T_STRING) {
141 $toReturn[(strlen($currentNamespace) > 0 ? $currentNamespace .
'\\' :
'') . $token[1]] = $pFileName;
143 } elseif ($namespaceHunt && $validatedNamespaceHunt && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
144 $currentNamespace.= $token[1];
145 } elseif ($namespaceHunt && !$validatedNamespaceHunt && $token[0] === T_WHITESPACE) {
146 $currentNamespace =
'';
147 $validatedNamespaceHunt =
true;
148 } elseif ($namespaceHunt && !$validatedNamespaceHunt && $token[0] !== T_WHITESPACE) {
149 $namespaceHunt =
false;
152 if ($token ===
';' || $token ===
'{') {
154 if ($namespaceHunt && !$validatedNamespaceHunt && $token ===
'{') {
155 $currentNamespace =
'';
158 $namespaceHunt =
false;
159 $validatedNamespaceHunt =
false;
175 private $_classHunterStrategy;
176 private $_lockfd =
false;
184 private function __construct()
191 private static $_instance =
false;
200 public static function instance($pTmpPath, $pTmpFileName =
'directoriesautoloader.cache.php')
202 if (self::$_instance ===
false) {
204 self::$_instance->setCachePath($pTmpPath);
205 self::$_instance->setCacheFileName($pTmpFileName);
206 self::$_instance->_classHunterStrategy = ClassHunterFactory::create(PHP_VERSION);
208 return self::$_instance;
217 public function register()
219 return spl_autoload_register(array(
231 $this->_canRegenerate =
true;
232 $this->_classes = array();
234 $this->_saveIncache();
236 return self::$_instance;
241 private $_dryRun =
false;
245 return self::$_instance;
249 private $_cacheFileName =
'directoriesautoloader.cache.php';
261 if (!is_writable($pTmp)) {
264 $this->_cachePath = $pTmp;
275 $this->_cacheFileName = $ptmpFileName;
284 return $this->_cachePath .
'/' . $this->_cacheFileName;
291 private function _getCacheLockFilePath()
293 return $this->getCacheFilePath() .
'.lock';
297 private $_customFilterClasses = null;
309 $filterClassParents = class_parents($pCustomFilterClass);
310 if (isset($filterClassParents[
'FilterIterator'])) {
311 if (!is_array($this->_customFilterClasses)) {
312 $this->_customFilterClasses = array(
318 $this->_customFilterClasses[] = $pCustomFilterClass;
323 return self::$_instance;
331 private function _lock()
333 if ($this->_dryRun) {
336 if ($this->_lockfd !==
false) {
337 throw new DirectoriesAutoloaderException(sprintf(
"Cache lock is already opened."));
339 $lockfile = $this->_getCacheLockFilePath();
340 $this->_lockfd = fopen($lockfile,
'a');
341 if ($this->_lockfd ===
false) {
342 throw new DirectoriesAutoloaderException(sprintf(
"Error opening cache lock file '%s'.", $lockfile));
344 if (flock($this->_lockfd, LOCK_EX) ===
false) {
345 throw new DirectoriesAutoloaderException(sprintf(
"Error locking cache lock file '%s'.", $lockfile));
351 private function _unlock()
353 if ($this->_dryRun) {
356 if ($this->_lockfd ===
false) {
357 throw new DirectoriesAutoloaderException(sprintf(
"Cache lock not opened."));
359 if (flock($this->_lockfd, LOCK_UN) ===
false) {
360 fclose($this->_lockfd);
361 $this->_lockfd =
false;
362 throw new DirectoriesAutoloaderException(sprintf(
"Error unlocking cache lock."));
364 fclose($this->_lockfd);
365 $this->_lockfd =
false;
372 private function _regenerate($pClassName)
379 $this->_classes = array();
380 if ($this->_loadClass($pClassName)) {
388 $this->_classes = array();
393 $this->_includesAll();
394 $this->addFamilies(
'FDLGEN');
395 $this->_saveInCache();
397 catch(\Exception $e) {
414 if ($this->_loadClass($pClassName)) {
418 if ($this->_canRegenerate) {
419 $this->_regenerate($pClassName);
420 $this->_canRegenerate =
false;
421 return $this->autoload($pClassName);
426 private $_canRegenerate =
true;
434 private function _includesAll()
439 foreach ($this->_directories as $directory => $recursive) {
444 if (strpos($directory,
'/') !== 0) {
445 $ret = chdir($this->_cachePath . DIRECTORY_SEPARATOR . $directory);
446 if (
$ret ===
false) {
452 $directories = new \AppendIterator();
455 $directories->append(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory) , \RecursiveIteratorIterator::LEAVES_ONLY, \RecursiveIteratorIterator::CATCH_GET_CHILD));
457 $directories->append(
new \DirectoryIterator($directory));
459 if (is_array($this->_customFilterClasses)) {
460 foreach ($this->_customFilterClasses as $customFilterClass) {
463 $directories =
new $customFilterClass($directories);
469 $files =
new ExtensionFilterIteratorDecorator($directories);
470 $files->setExtension(
'.php');
472 foreach ($files as $fileName) {
473 $classes = $this->_classHunterStrategy->find((
string)$fileName);
474 foreach ($classes as
$className => $fileName2) {
475 $fileName1 = (isset($this->_classes[strtolower(
$className) ])) ? $this->_classes[strtolower(
$className) ] : null;
476 if ($fileName1 !== null && realpath($fileName1) !==
false && realpath($fileName2) !== realpath($fileName1)) {
477 $err.= (
$err ?
"\n" :
'') . sprintf(_(
"Class %s from file %s already declared in autoload with file %s") ,
$className, $fileName2, $this->_classes[strtolower(
$className) ]);
479 $this->_classes[strtolower(
$className) ] = $fileName2;
488 require_once
"WHAT/Class.Log.php";
490 $log->setLogLevel(
"WEI");
492 throw new DirectoriesAutoloaderException(
$err);
502 private $_classes = array();
510 return isset($this->_classes[strtolower(
$className) ]) ? $this->_classes[strtolower(
$className) ] : null;
522 private function _saveIncache()
524 if ($this->_dryRun) {
527 foreach ($this->_classes as
$className => & $fileName) {
528 if (substr($fileName, 0, 2) ==
'./') {
529 $fileName = substr($fileName, 2);
534 $toSave =
'<?php' . PHP_EOL;
535 $toSave.=
'// Cache generated at: ' . date(DATE_W3C) . PHP_EOL;
536 $toSave.=
'$classes = ' . var_export($this->_classes,
true);
541 $cacheFileName = $this->getCacheFilePath();
542 $cacheDirName = dirname($cacheFileName);
543 $tmpFile = tempnam($cacheDirName,
'.autoloader.cache.tmp');
544 if ($tmpFile ===
false) {
545 throw new DirectoriesAutoloaderException(
"Error creating temporary autoloader cache file.");
547 if (file_put_contents($tmpFile, $toSave) ===
false) {
549 throw new DirectoriesAutoloaderException(sprintf(
"Error writing cache content to temporary file '%s'", $tmpFile));
551 if (rename($tmpFile, $cacheFileName) ===
false) {
553 throw new DirectoriesAutoloaderException(sprintf(
"Error renaming temporary cache file '%s' to '%s'.", $tmpFile, $cacheFileName));
555 chmod($cacheFileName, (0666 ^ umask()));
564 private function _loadClass($pClassName)
568 include_once (
'WHAT/Lib.Prefix.php');
573 if (!class_exists($className,
false) && !interface_exists($className,
false)) {
574 if (function_exists(
"trait_exists") && trait_exists($className,
false)) {
591 $className = ltrim(strtolower($pClassName),
"\\");
592 if (count($this->_classes) === 0) {
593 if (is_readable($this->getCacheFilePath())) {
595 require $this->getCacheFilePath();
596 $this->_classes = $classes;
599 return isset($this->_classes[$className]);
613 $absoluteDirectory = $pDirectory;
614 if (strpos($pDirectory,
'/') !== 0) {
615 $absoluteDirectory = $this->_cachePath . DIRECTORY_SEPARATOR . $pDirectory;
617 if (!is_readable($absoluteDirectory)) {
620 $this->_directories[$pDirectory] = $pRecursive ?
true :
false;
621 return self::$_instance;
631 include_once (
"Lib.Common.php");
632 $sql =
"select * from pg_tables where tablename = 'docfam'";
634 if (count($exists) > 0) {
635 $sql =
'select id, "name" from docfam where name is not null order by id';
640 foreach ($famNames as $aFam) {
641 $aFamName = $aFam[
"name"];
642 $aFamId = $aFam[
"id"];
643 $this->_classes[
'_' . strtolower($aFamName) ] = sprintf(
"%s/Class.Doc%d.php", $genDirectory, $aFamId);
644 $this->_classes[
'dcp\\family\\' . strtolower($aFamName) ] = sprintf(
"%s/Class.Doc%d.php", $genDirectory, $aFamId);
645 $this->_classes[
'dcp\\attributeidentifiers\\' . strtolower($aFamName) ] = sprintf(
"%s/Class.Attrid%d.php", $genDirectory, $aFamId);
648 return self::$_instance;
650 private $_directories = array();
addCustomFilter($pCustomFilterClass)
Exception class use exceptionCode to identifiy correctly exception.
addFamilies($genDirectory)
static instance($pTmpPath, $pTmpFileName= 'directoriesautoloader.cache.php')
forceRegenerate($className)
static classExists($pClassName)
if($_REQUEST['submitted']) else $cwd
addDirectory($pDirectory, $pRecursive=true)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
setCacheFileName($ptmpFileName)
if($file) if($subject==""&&$file) if($subject=="") $err