foldershare-8.x-1.2/src/Entity/FolderShareTraits/ManageLocksTrait.php
src/Entity/FolderShareTraits/ManageLocksTrait.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; use Drupal\foldershare\Settings; use Drupal\foldershare\Utilities\FormatUtilities; use Drupal\foldershare\FolderShareInterface; /** * Manages content locks for exclusive access to FolderShare entities. * * This trait includes internal aquire and release methods for locks on * FolderShare root folder trees and root lists. * * <B>Internal trait</B> * This trait is internal to the FolderShare module and used to define * features of the FolderShare entity class. It is a mechanism to group * functionality to improve code management. * * @ingroup foldershare */ trait ManageLocksTrait { /*--------------------------------------------------------------------- * * User root list locks. * *---------------------------------------------------------------------*/ /** * Acquires a lock on a user's root list. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * This lock is used to get exclusive edit access to the user's root list. * * @param int $uid * (optional, default = FolderShareInterface::CURRENT_USER_ID) The * user ID of the root list to lock. This defaults to the current * user's root list. * * @return bool * Returns TRUE if a lock on the current user's root list was acquired, * and FALSE otherwise. * * @see ::releaseUserRootListLock() */ public static function acquireUserRootListLock( int $uid = FolderShareInterface::CURRENT_USER_ID) { if ( $uid < 0) { $uid = (int) \Drupal::currentUser()->id(); } return \Drupal::lock()->acquire( self::CONTENT_LOCK_NAME . 'USER_ROOT_LIST_' . $uid , Settings::getContentLockDuration()); } /** * Releases a lock on the current user's root list. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * @param int $uid * (optional, default = FolderShareInterface::CURRENT_USER_ID) The * user ID of the root list to lock. This defaults to the current * user's root list. * * @see ::acquireUserRootListLock() */ public static function releaseUserRootListLock( int $uid = FolderShareInterface::CURRENT_USER_ID) { if ( $uid < 0) { $uid = (int) \Drupal::currentUser()->id(); } return \Drupal::lock()->release( self::CONTENT_LOCK_NAME . 'USER_ROOT_LIST_' . $uid ); } /*--------------------------------------------------------------------- * * Root operation locks. * *---------------------------------------------------------------------*/ /** * Acquires a lock on an entire folder tree under a root item. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * This lock is used to get exclusive edit access to the entire folder * tree anchored at the indicated root. This is used to lock out other * processes during major folder tree operations that may take some time. * * @param int $rootId * The entity ID of a root item whose folder tree is to be locked. * * @return bool * Returns TRUE if a lock on the root's folder tree was acquired, * and FALSE otherwise. * * @see ::acquireUserRootListLock() * @see ::releaseRootOperationLock() */ public static function acquireRootOperationLock(int $rootId ) { return \Drupal::lock()->acquire( self::OPERATION_LOCK_NAME . $rootId , Settings::getOperationLockDuration()); } /** * Returns TRUE if the root folder tree under a root item may be locked. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The root folder tree lock is tested and TRUE is returned if it appears * to currently be available. This is not a guarantee that a subsequent * call to acquireRootOperationLock() will succeed. Another process may * acquire the lock first. * * @param int $rootId * The entity ID of a root item whose folder tree is to be tested for * an available lock. * * @return bool * Returns TRUE if a lock on the root's folder tree may be available, * and FALSE otherwise. * * @see ::acquireUserRootListLock() * @see ::releaseRootOperationLock() */ public static function isRootOperationLockAvailable(int $rootId ) { return \Drupal::lock()->lockMayBeAvailable( self::OPERATION_LOCK_NAME . $rootId ); } /** * Releases a lock on the user's root list. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * @param int $rootId * The entity ID of a root item whose folder tree is to be locked. * * @see ::acquireRootOperationLock() * @see ::releaseUserRootListLock() */ public static function releaseRootOperationLock(int $rootId ) { return \Drupal::lock()->release( self::OPERATION_LOCK_NAME . $rootId ); } /*--------------------------------------------------------------------- * * Standard error messages. * *---------------------------------------------------------------------*/ /** * Returns a standard lock exception message. * * This method provides a generic lock exception message that may be used by * operations that are unable to acquire a needed lock. * * @param mixed $operationName * The past tense name of the operation being performed, such as * 'deleted', 'moved', or 'copied'. The value may be a string or * translated markup (perferred). * @param string $itemName * (optional, default = NULL = multiple items) The name of a single item * involved in an operation that cannot be done. If NULL, a multi-item * message is returned instead. * * @return \Drupal\Core\Render\Markup * Returns a markup object containing a formatted standard lock * exception message. */ public static function getStandardLockExceptionMessage( $operationName , string $itemName = NULL) { if ( empty ( $operationName ) === TRUE) { $operationName = t( 'updated' ); } if ( empty ( $itemName ) === TRUE) { return FormatUtilities::createFormattedMessage( t( 'The system is busy. One or more items cannot be @operation at this time.' , [ '@operation' => $operationName , ]), t( 'Please try again in a moment.' )); } return FormatUtilities::createFormattedMessage( t( 'The system is busy. The item "@name" cannot be @operation at this time.' , [ '@name' => $itemName , '@operation' => $operationName , ]), t( 'Please try again in a moment.' )); } } |