From: <gem...@li...> - 2013-01-04 16:20:30
|
Revision: 1088 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1088&view=rev Author: matijsdejong Date: 2013-01-04 16:20:22 +0000 (Fri, 04 Jan 2013) Log Message: ----------- Snippets should not use translate functions before call to afterRegistry() CachedLoader is now default loader Modified Paths: -------------- trunk/library/classes/Gems/Form/AutoLoadFormAbstract.php trunk/library/classes/Gems/Loader/LoaderAbstract.php trunk/library/classes/Gems/Snippets/AutosearchFormSnippet.php trunk/library/classes/Gems/Task/Db/CreateNewTable.php trunk/library/classes/Gems/Task/Db/CreateNewTables.php trunk/library/classes/GemsEscort.php trunk/library/classes/MUtil/Loader/CachedLoader.php trunk/library/classes/MUtil/Registry/Source.php trunk/library/snippets/AddTracksSnippet.php Modified: trunk/library/classes/Gems/Form/AutoLoadFormAbstract.php =================================================================== --- trunk/library/classes/Gems/Form/AutoLoadFormAbstract.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/Gems/Form/AutoLoadFormAbstract.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -67,18 +67,18 @@ protected $translate; /** - * Should be called after answering the request to allow the Target - * to check if all required registry values have been set correctly. + * Called after the check that all required registry values + * have been set correctly has run. * - * @return boolean False if required values are missing. + * @return void */ - public function checkRegistryRequestsAnswers() + public function afterRegistry() { + parent::afterRegistry(); + if ($this->loadDefault) { $this->loadDefaultElements(); } - - return true; } /** Modified: trunk/library/classes/Gems/Loader/LoaderAbstract.php =================================================================== --- trunk/library/classes/Gems/Loader/LoaderAbstract.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/Gems/Loader/LoaderAbstract.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -84,6 +84,12 @@ private $_loaded = array(); /** + * + * @var MUtil_Loader_CachedLoader + */ + private $_loader; + + /** * Allows sub classes of Gems_Loader_LoaderAbstract to specify the subdirectory where to look for. * * @var string $cascade An optional subdirectory where this subclass always loads from. @@ -108,6 +114,9 @@ } $this->_dirs = $newdirs; } + $this->_loader = MUtil_Loader_CachedLoader::getInstance(); + + if (MUtil_Registry_Source::$verbose) { MUtil_Echo::r($this->_dirs, '$this->_dirs in ' . get_class($this) . '->' . __FUNCTION__ . '():'); } @@ -162,8 +171,6 @@ $cname = trim(str_replace('/', '_', ucfirst($name)), '_'); $cfile = str_replace('_', '/', $cname) . '.php'; - // MUtil_Loader_CachedLoader::getInstance(GEMS_ROOT_DIR . '\var\cache'); - $found = false; /** @@ -187,18 +194,44 @@ $paths = array($paths); } foreach ($paths as $path) { + //* + $className = $prefix . $cname; + + if ($this->_loader->loadClass($className, $path . $fprefix . '/' . $cfile)) { + if (is_subclass_of($className, __CLASS__)) { + $create = true; + $arguments = array($this->_containers[0], $this->_dirs); + + } elseif (is_subclass_of($className, 'MUtil_Registry_TargetInterface')) { + $create = true; + } + + + if ($create) { + $obj = $this->_loader->createClass($className, $arguments); + } else { + $obj = new MUtil_Lazy_StaticCall($className); + } + + $found = true; + $this->_loaded[$cname] = get_class($obj); + break 2; + } // */ + + /* if ($obj = $this->_loadClassPath($path . $fprefix . '/' . $cfile, $prefix . $cname, $create, $arguments)) { $found = true; $this->_loaded[$cname] = get_class($obj); break 2; - } + } // */ } } } if ($found) { if ($obj instanceof MUtil_Registry_TargetInterface) { - if ((!$this->applySource($obj)) && parent::$verbose) { + // error_log(get_class($obj)); + if ((! $this->applySource($obj)) && parent::$verbose) { MUtil_Echo::track("Source apply to object of type $name failed."); } } Modified: trunk/library/classes/Gems/Snippets/AutosearchFormSnippet.php =================================================================== --- trunk/library/classes/Gems/Snippets/AutosearchFormSnippet.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/Gems/Snippets/AutosearchFormSnippet.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -120,13 +120,15 @@ } /** - * Should be called after answering the request to allow the Target - * to check if all required registry values have been set correctly. + * Called after the check that all required registry values + * have been set correctly has run. * - * @return boolean False if required are missing. + * @return void */ - public function checkRegistryRequestsAnswers() + public function afterRegistry() { + parent::afterRegistry(); + if ($this->util && (! $this->requestCache)) { $this->requestCache = $this->util->getRequestCache(); } @@ -134,8 +136,6 @@ // Do not store searchButtonId $this->requestCache->removeParams($this->searchButtonId); } - - return parent::checkRegistryRequestsAnswers(); } /** Modified: trunk/library/classes/Gems/Task/Db/CreateNewTable.php =================================================================== --- trunk/library/classes/Gems/Task/Db/CreateNewTable.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/Gems/Task/Db/CreateNewTable.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -68,7 +68,7 @@ public function execute($tableData = array()) { $this->_batch->addToCounter('createTableStep'); - + $result = $this->dbaModel->runScript($tableData); $result[] = sprintf($this->translate->_('Finished %s creation script for object %d of %d'), $this->translate->_(strtolower($tableData['type'])), $this->_batch->getCounter('createTableStep'), $this->_batch->getCounter('NewTableCount')) . '<br/>'; @@ -87,7 +87,8 @@ * * @return boolean */ - public function checkRegistryRequestsAnswers() { + public function checkRegistryRequestsAnswers() + { $this->escort = GemsEscort::getInstance(); //Load the dbaModel Modified: trunk/library/classes/Gems/Task/Db/CreateNewTables.php =================================================================== --- trunk/library/classes/Gems/Task/Db/CreateNewTables.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/Gems/Task/Db/CreateNewTables.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -79,7 +79,8 @@ * * @return boolean */ - public function checkRegistryRequestsAnswers() { + public function checkRegistryRequestsAnswers() + { $this->escort = GemsEscort::getInstance(); //Load the dbaModel Modified: trunk/library/classes/GemsEscort.php =================================================================== --- trunk/library/classes/GemsEscort.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/GemsEscort.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -38,6 +38,7 @@ // $autoloader->registerNamespace has not yet run!! include_once('MUtil/Application/Escort.php'); +include_once('MUtil/Loader/CachedLoader.php'); /** * Project Application Core code @@ -94,6 +95,10 @@ { parent::__construct($application); + $autoloader = Zend_Loader_Autoloader::getInstance(); + $cachedloader = MUtil_Loader_CachedLoader::getInstance(GEMS_ROOT_DIR . '\var\cache'); + $autoloader->setDefaultAutoloader(array($cachedloader, 'loadClassByPaths')); + self::$_instanceOfSelf = $this; $firebug = $application->getOption('firebug'); @@ -534,7 +539,7 @@ * Add ZFDebug info to the page output. * * @return void - * / + */ protected function _initZFDebug() { // if ((APPLICATION_ENV === 'development') && Modified: trunk/library/classes/MUtil/Loader/CachedLoader.php =================================================================== --- trunk/library/classes/MUtil/Loader/CachedLoader.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/MUtil/Loader/CachedLoader.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -65,7 +65,14 @@ protected $_cacheFile = 'cached.loader.mutil.php'; /** + * An array containg include dir pathNames * + * @var array + */ + protected $_includeDirs; + + /** + * * @param string $dir */ protected function __construct($dir = null) @@ -94,52 +101,164 @@ $this->_loadCache(); } + + $this->_loadIncludePath(); // MUtil_Echo::track($this->_cacheFile, $this->_cacheDir, file_exists($this->_cacheDir)); } /** - * Append a new found file to the cache + * Check for file existence and append status to the cache * - * @param string $class The name of the class to load * @param mixed $file String path to file or false if does not exist + * @return boolean True if the file exists */ - protected function _appendToCache($class, $file) + protected function _checkFile($file) { - $this->_cacheArray[$class] = $file; + if (array_key_exists($file, $this->_cacheArray)) { + return $this->_cacheArray[$file]; + } + // MUtil_Echo::track($file); + $this->_cacheArray[$file] = file_exists($file); + if (! file_exists($this->_cacheFile)) { $this->_saveCache(); } else { - if (false === $file) { - $content = "\$this->_cacheArray['$class'] = false;\n"; + if (false === $this->_cacheArray[$file]) { + $content = "\$this->_cacheArray['$file'] = false;\n"; } else { - $content = "\$this->_cacheArray['$class'] = '$file';\n"; + $content = "\$this->_cacheArray['$file'] = true;\n"; } file_put_contents($this->_cacheFile, $content, FILE_APPEND | LOCK_EX ); } + + return $this->_cacheArray[$file]; } + /** + * Loads the class from file with a check on changes to the include path + */ protected function _loadCache() { include $this->_cacheFile; + + // Include path has changed + if (!isset($include) || (get_include_path() != $include)) { + $this->_cacheArray = array(); + } } + /** + * Initialize the _includeDirs variable + */ + protected function _loadIncludePath() + { + $dirs = Zend_Loader::explodeIncludePath(); + + foreach ($dirs as $dir) { + if (('.' != $dir) && is_dir($dir)) { + $this->_includeDirs[] = realpath($dir) . DIRECTORY_SEPARATOR; +; + } + } + } + + /** + * Save the cache to file + */ protected function _saveCache() { - $content = "<?php\n"; + $content = "<?php\n\n"; + $content .= "\$include = '" . get_include_path() . "';\n\n"; - foreach ($this->_cacheArray as $class => $file) { + foreach ($this->_cacheArray as $file => $exists) { if (false === $file) { - $content .= "\$this->_cacheArray['$class'] = false;\n"; + $content .= "\$this->_cacheArray['$file'] = false;\n"; } else { - $content .= "\$this->_cacheArray['$class'] = '$file';\n"; + $content .= "\$this->_cacheArray['$file'] = true;\n"; } } file_put_contents($this->_cacheFile, $content, LOCK_EX); } /** + * Create a new instance of a class * + * @param string $className The name of the class + * @param array $arguments Class initialization arguments. + * @return boolean True if the class exists. + */ + public function createClass($className, array $arguments) + { + switch (count($arguments)) { + case 0: + return new $className(); + + case 1: + return new $className( + $arguments[0] + ); + + case 2: + return new $className( + $arguments[0], $arguments[1] + ); + + case 3: + return new $className( + $arguments[0], $arguments[1], $arguments[2] + ); + + case 4: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3] + ); + + case 5: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4] + ); + + case 6: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], + $arguments[5] + ); + + case 7: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], + $arguments[5], $arguments[6] + ); + + case 8: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], + $arguments[5], $arguments[6], $arguments[7] + ); + + case 9: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], + $arguments[5], $arguments[6], $arguments[7], $arguments[8] + ); + + case 10: + return new $className( + $arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], + $arguments[5], $arguments[6], $arguments[7], $arguments[8], $arguments[9] + ); + + default: + throw new Zend_Exception( + __CLASS__ . '->' . __FUNCTION__ . ' cannot create class with ' . + count($arguments) . ' parameters.' + ); + } + } + + /** + * * @static MUtil_Loader_CachedLoader $instance * @param stirng $dir * @return MUtil_Loader_CachedLoader @@ -155,11 +274,123 @@ return $instance;; } - protected function isClass($className, $paths) + /** + * Include a file with cached existence check + * + * @param string $file The full path to the file + */ + public function includeFile($file) { - // Zend_Loader::standardiseFile($file) - //if (file_exists($path . $className)) { + if ($this->_checkFile($file)) { + $result = include $file; -// /} + return $result ? $result : true; + } + + return false; } + + /** + * Load a class, but do not create it + * + * @param string $className The name of the class + * @param string $file The full path to the file + * @return boolean True if the class exists. + */ + public function loadClass($className, $file) + { + if (class_exists($className, false) || interface_exists($className, false)) { + return true; + } + + if ($this->includeFile($file)) { + if (class_exists($className, false) || interface_exists($className, false)) { + return true; + } + + throw new Zend_Exception("The file '$file' does not contain the class '$class'."); + } + + return false; + } + + /** + * Loads a class from a PHP file. The filename must be formatted + * as "$class.php". + * + * If $dirs is a string or an array, it will search the directories + * in the order supplied, and attempt to load the first matching file. + * + * If $dirs is null, it will split the class name at underscores to + * generate a path hierarchy (e.g., "Zend_Example_Class" will map + * to "Zend/Example/Class.php"). + * + * If the file was not found in the $dirs, or if no $dirs were specified, + * it will attempt to load it from PHP's include_path. + * + * @param string $class - The full class name of a Zend component. + * @param string|array $dirs - OPTIONAL Either a path or an array of paths + * to search. + * @return void + * @throws Zend_Exception + */ + public function loadClassByPaths($class, $dirs = null) + { + if (class_exists($class, false) || interface_exists($class, false)) { + return; + } + + if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) { + require_once 'Zend/Exception.php'; + throw new Zend_Exception('Directory argument must be a string or an array'); + } + + $className = ltrim($class, '\\'); + $file = ''; + $namespace = ''; + if ($lastNsPos = strripos($className, '\\')) { + $namespace = substr($className, 0, $lastNsPos); + $className = substr($className, $lastNsPos + 1); + $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; + } + $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; + + if (null === $dirs) { + $dirs = $this->_includeDirs; + } else { + $dirs = array_merge($dirs, $this->_includeDirs); + } + + foreach ($dirs as $dir) { + // error_log($dir . $file); + if ($this->includeFile($dir . $file)) { + break; + } + } + /* + if (!empty($dirs)) { + // use the autodiscovered path + $dirPath = dirname($file); + if (is_string($dirs)) { + $dirs = explode(PATH_SEPARATOR, $dirs); + } + foreach ($dirs as $key => $dir) { + if ($dir == '.') { + $dirs[$key] = $dirPath; + } else { + $dir = rtrim($dir, '\\/'); + $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath; + } + } + $file = basename($file); + self::loadFile($file, $dirs, true); + } else { + self::loadFile($file, null, true); + } // */ + + if (!class_exists($class, false) && !interface_exists($class, false)) { + require_once 'Zend/Exception.php'; + throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file"); + } + } } Modified: trunk/library/classes/MUtil/Registry/Source.php =================================================================== --- trunk/library/classes/MUtil/Registry/Source.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/classes/MUtil/Registry/Source.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -144,13 +144,12 @@ } // */ } } - if ($target->checkRegistryRequestsAnswers()) { - $target->afterRegistry(); - return true; - } else { - return false; - } + $result = $target->checkRegistryRequestsAnswers(); + + $target->afterRegistry(); + + return $result; } /** Modified: trunk/library/snippets/AddTracksSnippet.php =================================================================== --- trunk/library/snippets/AddTracksSnippet.php 2013-01-03 17:23:59 UTC (rev 1087) +++ trunk/library/snippets/AddTracksSnippet.php 2013-01-04 16:20:22 UTC (rev 1088) @@ -219,13 +219,15 @@ } /** - * Should be called after answering the request to allow the Target - * to check if all required registry values have been set correctly. + * Called after the check that all required registry values + * have been set correctly has run. * - * @return boolean False if required are missing. + * @return void */ - public function checkRegistryRequestsAnswers() + public function afterRegistry() { + parent::afterRegistry(); + if ($this->showForRespondents && is_bool($this->showForRespondents)) { $this->showForRespondents = $this->_('by Respondents'); } @@ -238,8 +240,6 @@ if ($this->showTitle && is_bool($this->showTitle)) { $this->showTitle = $this->_('Add'); } - - return parent::checkRegistryRequestsAnswers(); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |