From: <txm...@us...> - 2015-05-03 07:22:26
|
Revision: 13053 http://sourceforge.net/p/xoops/svn/13053 Author: txmodxoops Date: 2015-05-03 07:22:23 +0000 (Sun, 03 May 2015) Log Message: ----------- Autoloader for all classes Now is not needed to add link in common.php file to the new classes Modified Paths: -------------- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/TDMCreateAutoload.php XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/fields.php XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateArchitecture.php XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/language/LanguageAdmin.php XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/include/common.php XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/templates/admin/tdmcreate_fields.tpl Added Paths: ----------- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateAutoload.php Modified: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/TDMCreateAutoload.php =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/TDMCreateAutoload.php 2015-05-02 13:42:44 UTC (rev 13052) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/TDMCreateAutoload.php 2015-05-03 07:22:23 UTC (rev 13053) @@ -16,196 +16,42 @@ * @package tdmcreate * @since 2.5.0 * @author Txmod Xoops http://www.txmodxoops.org - * @version $Id: autoloader.php 12258 2014-01-02 09:33:29Z timgno $ + * @version $Id: TDMCreateAutoload.php 12258 2014-01-02 09:33:29Z timgno $ */ defined('XOOPS_ROOT_PATH') or die('Restricted access'); /** * @since 1.91 */ -class TDMCreateAutoload -{ - /** - * File where classes index is stored - */ - const INDEX_FILE = 'cache/class_index.php'; +// Autoload Function +ini_set('display_errors',1); +error_reporting(E_ALL|E_STRICT); - /** - * @var Autoload - */ - protected static $instance; +function application_autoloader($class) { + $classFilename = $class.'.php'; + $cacheFile = __DIR__ . '/cache/classpaths.cache'; + $pathCache = (file_exists($cacheFile)) ? unserialize(file_get_contents($cacheFile)) : array(); + if (!is_array($pathCache)) { $pathCache = array(); } + + if (array_key_exists($class, $pathCache)) { + /* Load class using path from cache file (if the file still exists) */ + if (file_exists($pathCache[$class])) { require_once $pathCache[$class]; } - /** - * @var string module directory - */ - protected $mod_dir; + } else { + /* Determine the location of the file within the $class_root and, if found, load and cache it */ + $directories = new RecursiveDirectoryIterator(__DIR__); + foreach(new RecursiveIteratorIterator($directories) as $file) { + if ($file->getFilename() == $classFilename) { + $fullPath = $file->getRealPath(); + $pathCache[$class] = $fullPath; + require_once $fullPath; + break; + } + } + } - /** - * @var array array('classname' => 'path/to/filename') - */ - public $index = array(); - - protected static $class_aliases = array('Autoload' => 'TDMCreateAutoload'); - - /** - * - */ - public function __construct() - { - $this->mod_dir = TDMC_PATH . '/'; - $file = TDMC_CLASSES_PATH . TDMCreateAutoload::INDEX_FILE; - if (@filemtime($file) && is_readable($file)) { - $this->index = include($file); - } else { - $this->generateIndex(); - } - } - - /** - * Get instance of autoload - * - * @return TDMCreateAutoload - */ - public static function getInstance() - { - if (!TDMCreateAutoload::$instance) { - TDMCreateAutoload::$instance = new TDMCreateAutoload(); - } - - return TDMCreateAutoload::$instance; - } - - /** - * Retrieve informations about a class in classes index and load it - * - * @param string $classname - * @return mixed - */ - public function load($classname) - { - // Retrocompatibility - if (isset(TDMCreateAutoload::$class_aliases[$classname]) && !interface_exists($classname, false) && !class_exists($classname, false)) { - return eval('class ' . $classname . ' extends ' . TDMCreateAutoload::$class_aliases[$classname] . ' {}'); - } - // regenerate the class index if the requested file doesn't exists - if ((isset($this->index[$classname]) && $this->index[$classname]['path'] && !is_file($this->mod_dir . $this->index[$classname]['path']))) { - $this->generateIndex(); - } - // Call directly class - if (isset($this->index[$classname]['path']) && $this->index[$classname]['path']) { - require($this->mod_dir . $this->index[$classname]['path']); - } - return null; - } - - /** - * Generate classes index - */ - public function generateIndex() - { - $classes = array_merge($this->getClassesFromDir('class/'), - $this->getClassesFromDir('class/files/'), - $this->getClassesFromDir('class/files/admin/'), - $this->getClassesFromDir('class/files/blocks/'), - $this->getClassesFromDir('class/files/classes/'), - $this->getClassesFromDir('class/files/css/'), - $this->getClassesFromDir('class/files/docs/'), - $this->getClassesFromDir('class/files/include/'), - $this->getClassesFromDir('class/files/language/'), - $this->getClassesFromDir('class/files/sql/'), - $this->getClassesFromDir('class/files/templates/'), - $this->getClassesFromDir('class/files/user/')); - ksort($classes); - $content = '<?php return ' . var_export($classes, true) . '; ?>'; - - // Write classes index on disc to cache it - $filename = TDMC_CLASSES_PATH . TDMCreateAutoload::INDEX_FILE; - $filename_tmp = tempnam(dirname($filename), basename($filename . '.')); - if ($filename_tmp !== false && file_put_contents($filename_tmp, $content) !== false) { - if (!@rename($filename_tmp, $filename)) { - unlink($filename_tmp); - } else { - @chmod($filename, 0644); - } - } - // $filename_tmp couldn't be written . $filename should be there anyway (even if outdated), no need to die. - else { - error_log('Cannot write temporary file ' . $filename_tmp); - } - $this->index = $classes; - } - - /** - * Retrieve recursively all classes in a directory and its subdirectories - * - * @param string $path Relative path from root to the directory - * @return array - */ - protected function getClassesFromDir($path) - { - $classes = array(); - $mod_dir = $this->mod_dir; - - foreach (scandir($mod_dir . $path) as $file) { - if ($file[0] != '.') { - if (is_dir($mod_dir . $path . $file)) { - $classes = array_merge($classes, $this->getClassesFromDir($path . $file . '/')); - } else { - if (substr($file, -4) == '.php') { - $content = file_get_contents($mod_dir . $path . $file); - $pattern = '#\W((abstract\s+)?class|interface)\s+(?P<classname>' . basename($file, '.php') . '?)' . '(?:\s+extends\s+[a-z][a-z0-9_]*)?(?:\s+implements\s+[a-z][a-z0-9_]*(?:\s*,\s*[a-z][a-z0-9_]*)*)?\s*\{#i'; - if (preg_match($pattern, $content, $m)) { - $classes[$m['classname']] = array('path' => $path . $file); - } - } - } - } - } - - return $classes; - } - - /** - * @param $classname - * @return null - */ - public function getClassPath($classname) - { - return (isset($this->index[$classname]) && isset($this->index[$classname]['path'])) ? $this->index[$classname]['path'] : null; - } + $serialized_paths = serialize($pathCache); + if ($serialized_paths != $pathCache) { file_put_contents($cacheFile, serialize($pathCache)); } } -/* - function autoLoader($className) { - // Directories - $directories = array( - '', - 'files/', - 'files/admin/', - 'files/blocks/', - 'files/classes/', - 'files/css/', - 'files/docs/', - 'files/include/', - 'files/language/', - 'files/sql/', - 'files/templates/user/', - 'files/templates/admin/', - 'files/templates/blocks/', - 'files/user/' - ); - // File naming format - $fileNameFormats = array( '%s.php' ); - - foreach($directories as $directory) { - foreach($fileNameFormats as $fileNameFormat) { - $path = $directory.sprintf($fileNameFormat, $className); - if(file_exists($path)) { - include_once $path; - return true; - } - } - } - return false; -}*/ -//spl_autoload_register('TDMCreateAutoload'); +spl_autoload_register('application_autoloader'); \ No newline at end of file Modified: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/fields.php =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/fields.php 2015-05-02 13:42:44 UTC (rev 13052) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/fields.php 2015-05-03 07:22:23 UTC (rev 13053) @@ -19,9 +19,10 @@ * @version $Id: 1.91 fields.php 12258 2014-01-02 09:33:29Z timgno $ */ defined('XOOPS_ROOT_PATH') or die('Restricted access'); -require_once 'html/TDMCreateFormLabel.php'; +/*require_once 'html/TDMCreateFormLabel.php'; require_once 'form/TDMCreateFormRadio.php'; -require_once 'form/TDMCreateThemeForm.php'; +require_once 'form/TDMCreateThemeForm.php';*/ +include __DIR__ . '/TDMCreateAutoload.php'; /* * @Class TDMCreateFields Modified: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateArchitecture.php =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateArchitecture.php 2015-05-02 13:42:44 UTC (rev 13052) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateArchitecture.php 2015-05-03 07:22:23 UTC (rev 13053) @@ -19,7 +19,8 @@ * @version $Id: TDMCreateArchitecture.php 12258 2014-01-02 09:33:29Z timgno $ */ defined('XOOPS_ROOT_PATH') or die('Restricted access'); -require_once 'TDMCreateStructure.php'; +// Autoloader Classes +include __DIR__ . '/TDMCreateAutoload.php'; /** * Class TDMCreateArchitecture @@ -357,7 +358,7 @@ $ret[] = $adminTemplatesHeader->render(); // Language Admin File $languageAdmin = LanguageAdmin::getInstance(); - $languageAdmin->write($module, $tables, 'admin.php'); + $languageAdmin->write($module, $table, $tables, 'admin.php'); $ret[] = $languageAdmin->render(); } // Class Helper File @@ -516,4 +517,4 @@ // Return Array return $ret; } -} +} \ No newline at end of file Added: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateAutoload.php =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateAutoload.php (rev 0) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/TDMCreateAutoload.php 2015-05-03 07:22:23 UTC (rev 13053) @@ -0,0 +1,57 @@ +<?php +/* + You may not change or alter any portion of this comment or credits + of supporting developers from this source code or any supporting source code + which is considered copyrighted (c) material of the original comment or credit authors. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +/** + * tdmcreate module + * + * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/ + * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) + * @package tdmcreate + * @since 2.5.0 + * @author Txmod Xoops http://www.txmodxoops.org + * @version $Id: TDMCreateAutoload.php 12258 2014-01-02 09:33:29Z timgno $ + */ +defined('XOOPS_ROOT_PATH') or die('Restricted access'); + +/** + * @since 1.91 + */ +// Autoload Function +ini_set('display_errors',1); +error_reporting(E_ALL|E_STRICT); + +function applicationAutoloader($class) { + $classFilename = $class.'.php'; + $cacheFile = __DIR__ . '/cache/classpaths.cache'; + $pathCache = (file_exists($cacheFile)) ? unserialize(file_get_contents($cacheFile)) : array(); + if (!is_array($pathCache)) { $pathCache = array(); } + + if (array_key_exists($class, $pathCache)) { + /* Load class using path from cache file (if the file still exists) */ + if (file_exists($pathCache[$class])) { require_once $pathCache[$class]; } + + } else { + /* Determine the location of the file within the $class_root and, if found, load and cache it */ + $directories = new RecursiveDirectoryIterator(__DIR__); + foreach(new RecursiveIteratorIterator($directories) as $file) { + if ($file->getFilename() == $classFilename) { + $fullPath = $file->getRealPath(); + $pathCache[$class] = $fullPath; + require_once $fullPath; + break; + } + } + } + + $serialized_paths = serialize($pathCache); + if ($serialized_paths != $pathCache) { file_put_contents($cacheFile, serialize($pathCache)); } +} + +spl_autoload_register('applicationAutoloader'); \ No newline at end of file Modified: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/language/LanguageAdmin.php =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/language/LanguageAdmin.php 2015-05-02 13:42:44 UTC (rev 13052) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/class/files/language/LanguageAdmin.php 2015-05-03 07:22:23 UTC (rev 13053) @@ -69,10 +69,11 @@ * @param string $filename * @param $filename */ - public function write($module, $tables, $filename) + public function write($module, $table, $tables, $filename) { $this->setModule($module); - $this->setTables($tables); + $this->setTable($table); + $this->setTables($tables); $this->setFileName($filename); } @@ -252,19 +253,17 @@ public function render() { $module = $this->getModule(); - $tables = $this->getTables(); + $table = $this->getTable(); + $tables = $this->getTables(); $filename = $this->getFileName(); $moduleDirname = $module->getVar('mod_dirname'); $language = $this->getLanguage($moduleDirname, 'AM'); $content = $this->getHeaderFilesComments($module, $filename); - foreach (array_keys($tables) as $t) { - $tablePermissions = $tables[$t]->getVar('table_permissions'); - } if (is_array($tables)) { $content .= $this->getLanguageAdminIndex($language, $tables); $content .= $this->getLanguageAdminPages($language, $tables); $content .= $this->getLanguageAdminClass($language, $tables); - if (1 == $tablePermissions) { + if (1 == $table->getVar('table_permissions')) { $content .= $this->getLanguageAdminPermissions($language); } } Modified: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/include/common.php =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/include/common.php 2015-05-02 13:42:44 UTC (rev 13052) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/include/common.php 2015-05-03 07:22:23 UTC (rev 13053) @@ -49,68 +49,4 @@ // Xoops Request include_once XOOPS_ROOT_PATH . '/class/xoopsrequest.php'; include_once TDMC_PATH . '/include/functions.php'; -//require_once(TDMC_CLASSES_PATH.'/TDMCreateAutoload.php'); -//spl_autoload_register(array(TDMCreateAutoload::getInstance(), 'load')); -// Include files -$cf = '/class/files/'; -$cfa = '/class/files/admin/'; -$cfb = '/class/files/blocks/'; -$cfcl = '/class/files/classes/'; -$cfcs = '/class/files/css/'; -$cfd = '/class/files/docs/'; -$cfi = '/class/files/include/'; -$cfl = '/class/files/language/'; -$cfs = '/class/files/sql/'; -$cftu = '/class/files/templates/user/'; -$cfta = '/class/files/templates/admin/'; -$cftb = '/class/files/templates/blocks/'; -$cfu = '/class/files/user/'; -include_once TDMC_PATH . '/class/TDMCreateHelper.php'; -include_once TDMC_PATH . '/class/TDMCreateSession.php'; -require_once TDMC_PATH . $cf . 'TDMCreateFile.php'; -include_once TDMC_PATH . $cfa . 'AdminAbout.php'; -include_once TDMC_PATH . $cfa . 'AdminFooter.php'; -include_once TDMC_PATH . $cfa . 'AdminHeader.php'; -include_once TDMC_PATH . $cfa . 'AdminIndex.php'; -include_once TDMC_PATH . $cfa . 'AdminMenu.php'; -include_once TDMC_PATH . $cfa . 'AdminPages.php'; -include_once TDMC_PATH . $cfa . 'AdminPermissions.php'; -include_once TDMC_PATH . $cfb . 'BlocksFiles.php'; -include_once TDMC_PATH . $cfcl . 'ClassFiles.php'; -include_once TDMC_PATH . $cfcl . 'ClassHelper.php'; -include_once TDMC_PATH . $cfcs . 'CssStyles.php'; -include_once TDMC_PATH . $cfd . 'DocsChangelog.php'; -include_once TDMC_PATH . $cfd . 'DocsFiles.php'; -include_once TDMC_PATH . $cfi . 'IncludeComments.php'; -include_once TDMC_PATH . $cfi . 'IncludeCommentFunctions.php'; -include_once TDMC_PATH . $cfi . 'IncludeCommon.php'; -include_once TDMC_PATH . $cfi . 'IncludeFunctions.php'; -include_once TDMC_PATH . $cfi . 'IncludeInstall.php'; -include_once TDMC_PATH . $cfi . 'IncludeJquery.php'; -include_once TDMC_PATH . $cfi . 'IncludeNotifications.php'; -include_once TDMC_PATH . $cfi . 'IncludeSearch.php'; -include_once TDMC_PATH . $cfi . 'IncludeUpdate.php'; -include_once TDMC_PATH . $cfl . 'LanguageAdmin.php'; -include_once TDMC_PATH . $cfl . 'LanguageBlocks.php'; -include_once TDMC_PATH . $cfl . 'LanguageHelp.php'; -include_once TDMC_PATH . $cfl . 'LanguageMailTpl.php'; -include_once TDMC_PATH . $cfl . 'LanguageMain.php'; -include_once TDMC_PATH . $cfl . 'LanguageModinfo.php'; -include_once TDMC_PATH . $cfs . 'SqlFile.php'; -include_once TDMC_PATH . $cfta . 'TemplatesAdminAbout.php'; -include_once TDMC_PATH . $cfta . 'TemplatesAdminHeader.php'; -include_once TDMC_PATH . $cfta . 'TemplatesAdminIndex.php'; -include_once TDMC_PATH . $cfta . 'TemplatesAdminFooter.php'; -include_once TDMC_PATH . $cfta . 'TemplatesAdminPages.php'; -include_once TDMC_PATH . $cfta . 'TemplatesAdminPermissions.php'; -include_once TDMC_PATH . $cftb . 'TemplatesBlocks.php'; -include_once TDMC_PATH . $cftu . 'TemplatesUserHeader.php'; -include_once TDMC_PATH . $cftu . 'TemplatesUserIndex.php'; -include_once TDMC_PATH . $cftu . 'TemplatesUserFooter.php'; -include_once TDMC_PATH . $cftu . 'TemplatesUserPages.php'; -include_once TDMC_PATH . $cfu . 'UserFooter.php'; -include_once TDMC_PATH . $cfu . 'UserHeader.php'; -include_once TDMC_PATH . $cfu . 'UserIndex.php'; -include_once TDMC_PATH . $cfu . 'UserPages.php'; -include_once TDMC_PATH . $cfu . 'UserNotificationUpdate.php'; -include_once TDMC_PATH . $cfu . 'UserXoopsVersion.php'; +include_once TDMC_PATH . '/class/TDMCreateHelper.php'; \ No newline at end of file Modified: XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/templates/admin/tdmcreate_fields.tpl =================================================================== --- XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/templates/admin/tdmcreate_fields.tpl 2015-05-02 13:42:44 UTC (rev 13052) +++ XoopsModules/TDMCreate/branches/timgno/1.91a2/tdmcreate/templates/admin/tdmcreate_fields.tpl 2015-05-03 07:22:23 UTC (rev 13053) @@ -73,4 +73,4 @@ <div class="spacer"><{$form}></div> <{/if}> <!-- Footer --> -<{includeq file="db:tdmcreate_footer.tpl"}> +<{includeq file="db:tdmcreate_footer.tpl"}> \ No newline at end of file |