PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/classes/ezclusterfilehandler.php

https://github.com/eeggenberger/ezpublish
PHP | 204 lines | 110 code | 27 blank | 67 comment | 15 complexity | 61b0828ff6a3342c12c0a44f6158b810 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZClusterFileHandler class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. class eZClusterFileHandler
  11. {
  12. /**
  13. * Returns the configured instance of an eZClusterFileHandlerInterface
  14. * See ClusteringSettings.FileHandler in php.ini
  15. *
  16. * @param string|bool $filename
  17. * Optional filename the handler should be initialized with
  18. *
  19. * @return eZClusterFileHandlerInterface
  20. */
  21. static function instance( $filename = false )
  22. {
  23. if ( self::$isShutdownFunctionRegistered !== true )
  24. {
  25. eZExecution::addCleanupHandler( array( __CLASS__, 'cleanupGeneratingFiles' ) );
  26. self::$isShutdownFunctionRegistered = true;
  27. }
  28. if( $filename !== false )
  29. {
  30. $optionArray = array( 'iniFile' => 'file.ini',
  31. 'iniSection' => 'ClusteringSettings',
  32. 'iniVariable' => 'FileHandler',
  33. 'handlerParams'=> array( $filename ) );
  34. $options = new ezpExtensionOptions( $optionArray );
  35. $handler = eZExtension::getHandlerClass( $options );
  36. return $handler;
  37. }
  38. else
  39. {
  40. // return Filehandler from GLOBALS based on ini setting.
  41. if ( self::$globalHandler === null )
  42. {
  43. $optionArray = array( 'iniFile' => 'file.ini',
  44. 'iniSection' => 'ClusteringSettings',
  45. 'iniVariable' => 'FileHandler' );
  46. $options = new ezpExtensionOptions( $optionArray );
  47. $handler = eZExtension::getHandlerClass( $options );
  48. self::$globalHandler = $handler;
  49. }
  50. else
  51. $handler = self::$globalHandler;
  52. return $handler;
  53. }
  54. }
  55. /**
  56. * @deprecated 4.3 No longer used as we rely on ezpExtension & autoloads
  57. * @return array list of directories used to search cluster file handlers for
  58. */
  59. static function searchPathArray()
  60. {
  61. if ( !isset( $GLOBALS['eZClusterFileHandler_search_path_array'] ) )
  62. {
  63. $fileINI = eZINI::instance( 'file.ini' );
  64. $searchPathArray = array( 'kernel/classes/clusterfilehandlers',
  65. 'kernel/private/classes/clusterfilehandlers' );
  66. if ( $fileINI->hasVariable( 'ClusteringSettings', 'ExtensionDirectories' ) )
  67. {
  68. $extensionDirectories = $fileINI->variable( 'ClusteringSettings', 'ExtensionDirectories' );
  69. $baseDirectory = eZExtension::baseDirectory();
  70. foreach ( $extensionDirectories as $extensionDirectory )
  71. {
  72. $customSearchPath = $baseDirectory . '/' . $extensionDirectory . '/clusterfilehandlers';
  73. if ( file_exists( $customSearchPath ) )
  74. $searchPathArray[] = $customSearchPath;
  75. }
  76. }
  77. $GLOBALS['eZClusterFileHandler_search_path_array'] = $searchPathArray;
  78. }
  79. return $GLOBALS['eZClusterFileHandler_search_path_array'];
  80. }
  81. /**
  82. * Cluster shutdown handler. Terminates generation for unterminated files.
  83. * This situation doesn't happen by default, but may with custom code that doesn't follow recommendations.
  84. */
  85. static function cleanupGeneratingFiles()
  86. {
  87. if ( count( self::$generatingFiles ) === 0 )
  88. {
  89. return false;
  90. }
  91. else
  92. {
  93. eZDebug::writeWarning( "Execution was stopped while one or more files were generating. This should not happen.", __METHOD__ );
  94. foreach( self::$generatingFiles as $generatingFile )
  95. {
  96. $generatingFile->abortCacheGeneration();
  97. self::removeGeneratingFile( $generatingFile );
  98. }
  99. }
  100. }
  101. /**
  102. * Goes trough the directory path and removes empty directories, starting at
  103. * the leaf and deleting down until a non empty directory is reached.
  104. * If the path is not a directory, nothing will happen.
  105. *
  106. * @param string $path
  107. */
  108. public static function cleanupEmptyDirectories( $path )
  109. {
  110. $dirpath = eZDir::dirpath( $path );
  111. eZDebugSetting::writeDebug( 'kernel-clustering', "eZClusterFileHandler::cleanupEmptyDirectories( '{$dirpath}' )" );
  112. if ( is_dir( $dirpath ) )
  113. {
  114. eZDir::cleanupEmptyDirectories( $dirpath );
  115. }
  116. }
  117. /**
  118. * Adds a file to the generating list
  119. *
  120. * @param eZDFSFileHandler|eZDFSFileHandler $file
  121. * Cluster file handler instance
  122. * Note that this method expect a version of the handler where the filePath is the REAL one, not the .generating
  123. */
  124. public static function addGeneratingFile( $file )
  125. {
  126. if ( !( $file instanceof eZDBFileHandler ) && !( $file instanceof eZDFSFileHandler ) )
  127. return false; // @todo Exception
  128. self::$generatingFiles[$file->filePath] = $file;
  129. }
  130. /**
  131. * Removes a file from the generating list
  132. * @param eZDBFileHandler|eZDFSFileHandler $file
  133. * Cluster file handler instance
  134. * Note that this method expect a version of the handler where the filePath is the REAL one, not the .generating
  135. *
  136. * @todo Clustering: apply the eZClusterFileHandlerInterface to all cluster handlers
  137. */
  138. public static function removeGeneratingFile( $file )
  139. {
  140. if ( !( $file instanceof eZDBFileHandler ) && !( $file instanceof eZDFSFileHandler ) )
  141. return false; // @todo Exception
  142. if ( isset( self::$generatingFiles[$file->filePath] ) )
  143. unset( self::$generatingFiles[$file->filePath] );
  144. }
  145. /**
  146. * Performs required operations before forking a process
  147. *
  148. * - disconnects DB based cluster handlers from the database
  149. */
  150. public static function preFork()
  151. {
  152. $clusterHandler = self::instance();
  153. // disconnect DB based cluster handlers from the database
  154. if ( $clusterHandler instanceof ezpDatabaseBasedClusterFileHandler )
  155. {
  156. $clusterHandler->disconnect();
  157. // destroy the current handler so that it reconnects when instanciated again
  158. self::$globalHandler = null;
  159. }
  160. }
  161. /**
  162. * Global list of currently generating files. Used by handlers that support stalecache.
  163. * @var array(filename => eZClusterFileHandlerInterface)
  164. */
  165. private static $generatingFiles = array();
  166. /**
  167. * Shutdown registration check variable
  168. * @var bool
  169. */
  170. private static $isShutdownFunctionRegistered = false;
  171. /**
  172. * Global, generic (e.g. not linked to a file) cluster handler, used for caching
  173. * @var eZClusterFileHandlerInterface
  174. */
  175. public static $globalHandler;
  176. }
  177. ?>